logo elektroda
logo elektroda
X
logo elektroda

ESP32 with ENC28J60 Ethernet Module: SPI Pin Configuration and CS Connection

madiz08 5190 13
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18296590
    madiz08
    Level 14  
    Posts: 517
    Help: 4
    Rate: 172
    Hello
    Has any colleague perhaps managed to connect the ESP to the local network, but not over WiFi but over Ethernet? I have two enc28j60 ethernet modules. The only source I have found is the website:
    https://esp32.com/viewtopic.php?t=3994
    I downloaded the library and uploaded the code given on this page, connecting the enc28j60 module to the SPI pins (miso, mosi, sck), but unfortunately it doesn't work. I don't quite know what the CS (chip select) connector is. I tried permanently connecting this pin to ground, then to 3.3V, but that didn't do anything either.
  • ADVERTISEMENT
  • Helpful post
    #2 18296667
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #3 18297354
    madiz08
    Level 14  
    Posts: 517
    Help: 4
    Rate: 172
    Great, it works!!! Earlier I was suggesting to connect the pins at all like in the screengrab above. Everything is working, also I am working on. Thank you

    Added after 22 [minutes]: .

    The only thing I changed was as supply voltage not 5V, but 3.3V for the module. At 5V it was getting terribly hot, at 3.3V the chip is just warm, and ethernet communication works fine
  • Helpful post
    #4 18297521
    Anonymous
    Level 1  
  • #5 18300310
    madiz08
    Level 14  
    Posts: 517
    Help: 4
    Rate: 172
    And I would still have a question, would it be possible to solve the EEPROM.h compilation problem?
    Code: C / C++
    Log in, to see the code
    .
    The code above is exactly what is given on the library page for the ESP32. However, if we uncomment the first line of code, thereby inserting the EEPROM.h library, compilation errors occur.
    What I mean is that through a separate application I can load the contents of IPs, subnets and others into EEPROM, then at the start of the microcontroller all these addresses are just fetched from EEPROM.
    This works for me on the UNO with the Ethernet module, on the ESP32 using WiFi, while using the enc28j60 with the ESP32, I cannot compile the project properly.
  • #6 18300316
    Anonymous
    Level 1  
  • #7 18300319
    madiz08
    Level 14  
    Posts: 517
    Help: 4
    Rate: 172
    Below I send compilation errors caused by the addition of EEPROM.h
    Code: C / C++
    Log in, to see the code
  • ADVERTISEMENT
  • Helpful post
    #8 18300329
    Anonymous
    Level 1  
  • #9 18300336
    madiz08
    Level 14  
    Posts: 517
    Help: 4
    Rate: 172
    Exactly right. It turns out that I had to temporarily remove Wifi.h and Wifi.cpp from the location:
  • #10 19302785
    martii
    Level 12  
    Posts: 29
    Rate: 4
    Well I also decided to play with this module and I have this symptom:

    Restarting...
    ets Jun 8 2016 00:22:57

    rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:D IO, clock div:2
    load:0x3fff0018,len:4
    load:0x3fff001c,len:1044
    load:0x40078000,len:8896
    load:0x40080400,len:5828
    entry 0x400806ac
    Trying to get an IP...
    MAC: 74:69:69:2D:30:31
    Failed to initialise EEPROM
    Restarting...

    Code basically from the website - only thing is I'm using Visual Studio Code from PlatformIO - but the library is the same.
  • ADVERTISEMENT
  • #11 19302876
    Anonymous
    Level 1  
  • #12 19303037
    martii
    Level 12  
    Posts: 29
    Rate: 4
    Exactly like this (I'm using the kind of mini-test board you promote in the posts) ESP-WROOM-32 - pins connected as in the documentation to EtherCard [github 8-DK] - all in all, I don't know if I'm not doing an uphill job because the ESP32 seems to have ethernet built in somehow?

    #include <Arduino.h>
    #include <EtherCard.h>
    #define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below)

    // mac address
    static byte mymac[] = {0x74, 0x69, 0x69, 0x2D, 0x30, 0x31};
    // ethernet interface ip address
    static byte myip[] = {192, 168, 90, 200};
    // gateway ip address
    static byte gwip[] = {192, 168, 90, 1};

    // LED to control output
    int ledPin10 = 9;

    byte Ethernet::buffer[700];

    char const page[] PROGMEM =
    "HTTP/1.0 503 Service Unavailable".
    "Content-Type: text/html".
    "Retry-After: 600"
    "}"
    "<html>"
    "<head> <title>"
    "Service Temporarily Unavailable"
    "</title> </head>"
    "<body>"
    "<h3>This page is used behind the scene</h3>"
    "<p><em>"
    "Commands to control LED are transferred to Arduino.<br />"
    "The syntax: http://192.168.0.XX/?LED10=OFF or ON"
    "</em> </p>"
    "</body>"
    "</html>";

    void blinkLed()
    {
    while (true)
    {
    digitalWrite(ledPin10, HIGH);
    delay(500);
    digitalWrite(ledPin10, LOW);
    delay(500);
    }
    }

    void setup()
    {
    pinMode(ledPin10, OUTPUT);

    Serial.begin(115200);
    Serial.println("Trying to get an IP...");

    Serial.print("MAC: ");
    for (byte i = 0; i < 6; ++i)
    {
    Serial.print(mymac , HEX);
    if (i < 5)
    Serial.print(':');
    }
    Serial.println();

    if (ether.begin(sizeof Ethernet::buffer, mymac, 15) == 0)
    {
    Serial.println("Failed to access Ethernet controller;)
    }
    else
    {
    Serial.println("Ethernet controller access: OK");
    };

    #if STATIC
    Serial.println("Getting static IP.");
    if (!ether.staticSetup(myip, gwip))
    {
    Serial.println("could not get a static IP");
    blinkLed(); // blink forever to indicate a problem
    }
    #else

    Serial.println("Setting up DHCP");
    if (!ether.dhcpSetup())
    {
    Serial.println("DHCP failed");
    blinkLed(); // blink forever to indicate a problem
    }
    #endif

    ether.printIp("My IP: ", ether.myip);
    ether.printIp("Netmask: ", ether.netmask);
    ether.printIp("GW IP: ", ether.gwip);
    ether.printIp("DNS IP: ", ether.dnsip);
    }

    void loop()
    {

    word len = ether.packetReceive();
    word pos = ether.packetLoop(len);

    // IF LED10=ON turn it ON
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED10=ON") != 0)
    {
    Serial.println("Received ON command");
    digitalWrite(ledPin10, HIGH);
    }

    // IF LED10=OFF turn it OFF
    if (strstr((char *)Ethernet::buffer + pos, "GET /?LED10=OFF") != 0)
    {
    Serial.println("Received OFF command");
    digitalWrite(ledPin10, LOW);
    }

    // show some data to the user
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
    }
    .
  • #13 19303443
    Anonymous
    Level 1  
  • #14 19303601
    martii
    Level 12  
    Posts: 29
    Rate: 4
    EDIT:

    Alright - I've replaced what I wrote below and all occurrences of EEPROM.eeprom_read_byte with EEPROM.readByte and now it compiles, but something is still messed up because even the uplink doesn't pick up - if I give power alone it's OK - uplink in green - if I put code in - sneezing darkness on the port.

    ----------------------------------

    I've already tried deleting these files of course - but I don't know why it doesn't download from Core and compile spitting errors :( .
    Even replacing in 3 library files #include "EEPROM.h" with #include <EEPROM.h> didn't help :( .

    lib/EtherCard-master/src/bufferfiller.cpp: In member function 'void BufferFiller::emit_p(const char*, ...)':
    lib/EtherCard-master/src/bufferfiller.cpp:62:30: error: 'class EEPROMClass' has no member named 'eeprom_read_byte'
    while ((d=EEPROM.eeprom_read_byte(*s++)) != 0)
    ^
    lib/EtherCard-master/src/stash.cpp: In static member function 'static void Stash::prepare(const char*, ...)':
    lib/EtherCard-master/src/stash.cpp:179:36: error: 'class EEPROMClass' has no member named 'eeprom_read_byte'
    while ((d = EEPROM.eeprom_read_byte(*s++)) != 0)
    ^
    lib/EtherCard-master/src/stash.cpp: In static member function 'static void Stash::extract(uint16_t, uint16_t, void*)':
    lib/EtherCard-master/src/stash.cpp:258:24: error: 'class EEPROMClass' has no member named 'eeprom_read_byte'.
    c = EEPROM.eeprom_read_byte(*ptr++);
    ^
    lib/EtherCard-master/src/enc28j60.cpp:275:13: warning: 'void xferSPI(byte)' defined but not used [-Wunused-function].
    static void xferSPI (byte data) {.
    ^

Topic summary

✨ The discussion revolves around connecting the ESP32 microcontroller to a local network using the ENC28J60 Ethernet module via SPI. The main issue raised was the confusion regarding the Chip Select (CS) pin connection, which was resolved by referring to a GitHub resource. Users shared their experiences, noting that powering the ENC28J60 with 3.3V instead of 5V prevented overheating and allowed successful Ethernet communication. Additionally, a compilation problem related to the EEPROM library was addressed, with suggestions to remove conflicting library files to resolve multiple definition errors. The conversation highlighted the importance of using compatible libraries and proper voltage levels for successful integration.
Generated by the language model.

FAQ

TL;DR: Use 3.3V power (not 5V) for ENC28J60; "at 3.3V the chip is just warm" and Ethernet works. Assign CS to a GPIO and remove duplicate EEPROM files to compile cleanly. [Elektroda, madiz08, post #18297354]

Why it matters: This FAQ helps ESP32 builders get stable wired Ethernet with ENC28J60, fix CS pin mistakes, and resolve EEPROM compile/link errors.

Quick Facts

How do I wire ENC28J60 to ESP32 and set CS?

Connect MISO, MOSI, and SCK to ESP32 SPI. Assign CS to a free GPIO in code (example uses GPIO15 via ether.begin(..., 15)). Do not tie CS to GND or 3.3V. Provide 3.3V and GND to the module. Verify with the example sketch’s serial logs. [Elektroda, madiz08, post #18300310]

Should I power the ENC28J60 with 5V or 3.3V?

Use 3.3V. One user reported the chip "getting terribly hot" at 5V, but "at 3.3V the chip is just warm" and Ethernet works. Powering at 3.3V protects the ESP32’s 3.3V-only SPI pins too. [Elektroda, madiz08, post #18297354]

Are ESP32 SPI pins 5V tolerant?

No. An expert noted, "the SPI pins on the ESP32 do not necessarily" tolerate 5V. Keep the ENC28J60 and SPI lines at 3.3V. This avoids damage and random faults during initialization. [Elektroda, khoam, post #18297521]

Why do I get 'multiple definition' errors after including EEPROM.h?

You have two EEPROM libraries compiled: one from EtherCard and one from the ESP32 Core. The linker reports duplicates like EEPROMClass functions. Keep only one EEPROM implementation in your build. Delete or exclude the duplicate copy. [Elektroda, khoam, post #18300329]

How do I make EtherCard use the ESP32 Core EEPROM (and stop conflicts)?

EtherCard includes outdated EEPROM files. Replace them with the ESP32 Core EEPROM.
  1. Delete EtherCard/src/EEPROM.h and EEPROM.cpp from your project/lib folder.
  2. Include from the ESP32 Core in your sketch.
  3. Rebuild; the Core’s EEPROM will be linked. [Elektroda, khoam, post #19303443]

What should I replace eeprom_read_byte with on ESP32?

Replace EEPROM.eeprom_read_byte(...) with EEPROM.readByte(...). A user confirmed compilation after this change. If the link still fails, recheck wiring and CS pin selection. [Elektroda, martii, post #19303601]

Why is my link LED green with power only, but off when the code runs?

The controller likely fails to initialize when the sketch starts. Watch serial for "Failed to access Ethernet controller" from ether.begin(...). Recheck the CS pin number, SPI wiring, and 3.3V supply. Initialization must succeed for link to stay up. [Elektroda, madiz08, post #18300310]

Does the ESP32 Arduino Core support ENC28J60 directly?

No. An expert clarified there is "no support for the ENC28J60 in the Core for ESP32." Use a library like EtherCard to drive ENC28J60 over SPI. A PHY-supported internal MAC does not include ENC28J60. [Elektroda, khoam, post #19303443]

Can I use PlatformIO instead of the Arduino IDE?

Yes, but ensure libraries resolve correctly. One report showed "Failed to initialise EEPROM" under PlatformIO. Verify your lib_deps, remove duplicate EEPROM, and use the ESP32 Core EEPROM. [Elektroda, martii, post #19302785]

How do I switch between DHCP and static IP with EtherCard?

Set #define STATIC to 1 for static addresses and fill myip and gwip. For DHCP, set STATIC to 0 and call ether.dhcpSetup(). The example prints IP, netmask, gateway, and DNS after setup. [Elektroda, madiz08, post #18300310]

What MAC address and gateway should I use?

Use any unique 6-byte MAC not used on your LAN. The example uses 74:69:69:2D:30:31. For static IP, set myip to your subnet and gwip to your router’s IP. Avoid conflicts with DHCP. [Elektroda, madiz08, post #18300310]

What HTTP response does the example serve, and how big is the buffer?

The example serves a simple 503 page with LED control via /?LED10=ON or OFF. It includes 'Retry-After: 600' seconds. The Ethernet buffer is 700 bytes. Adjust sizes if you add routes. [Elektroda, madiz08, post #18300310]

Is EtherCard outdated for ESP32?

Parts of EtherCard are outdated, notably its emulated EEPROM. Prefer the ESP32 Core EEPROM and keep EtherCard for ENC28J60 only. This reduces conflicts and eases maintenance. [Elektroda, khoam, post #19303443]

What’s an edge case to watch for during setup?

If you power at 5V, the ENC28J60 can overheat and fail to link. Another failure shows as 'Failed to initialise EEPROM' at boot. Both indicate configuration or voltage issues. [Elektroda, madiz08, post #18297354]
Generated by the language model.
ADVERTISEMENT