logo elektroda
logo elektroda
X
logo elektroda

How to start i2c communication between NodeMcu and Wemos D1?

omnixcrs 2031 15
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 18474243
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    Welcome,

    colleagues I am running out of ideas for the above problem. I have been trying for a few hours to get i2c communication working between the NodeMcu and the Wemos D1 mini.
    I have connected both boards with ground and d1 to d1 and d2 to d2. Of course the lines pulled up to the power supply via 4k7. Node as master and Wemos as slave. Unfortunately after uploading the programs in the ports monitor nothing !
    I paste the codes, they are almost finished examples, maybe I have rewritten something wrong.

    Master to node code:
    Code: C / C++
    Log in, to see the code
    .

    code to slave Wemos:
    Code: C / C++
    Log in, to see the code
    .
  • ADVERTISEMENT
  • #2 18474289
    Anonymous
    Level 1  
  • #3 18474311
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    function returns value 2

    Added after 11 [minutes]:

    I've connected an arduino nano instead of wemos for testing, removing the line "Wire.begin(D2, D1);" and it works without a problem, I suspect that this is where the problem will be with this addressing or pin setting, maybe it writes wrong ???
    I have also tried writing in this form: " Wire.begin(D2,D1,8); " and also nothing.
  • ADVERTISEMENT
  • Helpful post
    #4 18474382
    Anonymous
    Level 1  
  • #5 18474388
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    As I wrote above I also used: "Wire.begin(D2, D1, 8);" and it did not do anything.
    I read somewhere that esp 12 does not have the option to work in SLAVE mode is it possible ???
    I connected another esp instead of WEMOSA, namely another NodeMcu and also nothing.
  • Helpful post
    #6 18475388
    Anonymous
    Level 1  
  • #7 18476318
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    Hmm.... I understand in that case I will give up esp as sleve and replace it with some Adriano uno or mega, I think they support slave MODE.
  • Helpful post
    #8 18476437
    Anonymous
    Level 1  
  • ADVERTISEMENT
  • #9 18477006
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    Yes, I know. Thanks for your help khoam!
  • ADVERTISEMENT
  • #10 18504131
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    Hello, I have another question, will the esp32 support slave mode?
  • #11 18504298
    Anonymous
    Level 1  
  • #12 18504875
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    Aha, so in short I can't use the ESP32 as a slave by programming it from the Arduino IDE, yes ??
  • #14 18505362
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    Ok, I'll figure it out somehow. Tell me just a little bit off the top of my head: Because I have to build a network of temp sensors on a site where distances are known. These will be DS18b20 onewire. I have read from various sources that the safest way is to build a bus with a maximum length of up to 100m. I need to deploy about 30 sensors so I will certainly exceed 100m so I thought I would create for example 3-4 buses on different pins on one esp, is this possible and will it work?
  • #15 18505375
    Anonymous
    Level 1  
  • #16 18505406
    omnixcrs
    Level 11  
    Posts: 397
    Help: 8
    Rate: 60
    Ok I am already creating.

Topic summary

✨ The discussion revolves around establishing I2C communication between a NodeMCU and a Wemos D1 mini, with the NodeMCU configured as the master and the Wemos as the slave. The user encountered issues with the communication, specifically receiving a NACK (Negative Acknowledgement) when attempting to transmit data. Various troubleshooting steps were suggested, including verifying the return value of the Wire.endTransmission() function, which indicated errors. The user tested with an Arduino Nano, which worked successfully, leading to suspicions about the Wemos D1's configuration. It was noted that the ESP8266 (NodeMCU and Wemos D1) lacks a hardware I2C controller, complicating slave mode functionality. The conversation also touched on the ESP32's limitations regarding slave mode in the Arduino HAL. Ultimately, the user considered switching to an Arduino Uno or Mega for reliable slave mode support and inquired about deploying multiple temperature sensors using DS18B20 on a long bus.
Generated by the language model.

FAQ

TL;DR: For ESP8266↔ESP8266 I2C, the default is 100 kHz; set 50 kHz because "100 kHz is too high" and prefer Arduino core 2.5.2, as 2.6.x breaks slave mode. [Elektroda, khoam, post #18475388] Why it matters: This helps makers quickly fix "NodeMCU to Wemos I2C not working" issues and pick a setup that actually works.

Quick Facts

How should I wire I2C between NodeMCU and Wemos D1 mini?

Share GND. Connect SDA to SDA and SCL to SCL (D2↔D2, D1↔D1 on these boards). Use 4.7 kΩ pull‑ups to 3.3 V on SDA and SCL. In code, call Wire.begin(D2, D1) to map SDA=D2 and SCL=D1. Use a known 7‑bit address for the slave (e.g., 0x08). [Elektroda, omnixcrs, post #18474243]

What does Wire.endTransmission() return code 2 mean, and how do I fix it?

Return 2 means the master "received NACK on transmit of address." The slave did not acknowledge that address. Confirm the slave address matches and the slave is actually on the bus. Initialize the slave with its address. How‑To: 1. Print the return code. 2. Verify the address in both sketches. 3. Initialize the slave as Wire.begin(sda, scl, address). [Elektroda, khoam, post #18474382]

Why does ESP8266 I2C slave work on core 2.5.2 but fail on 2.6.x?

Tests show master↔slave works on 2.5.2 with minor repeats, but fails on 2.6.3. The newer NonOS‑SDK changed timing, and ESP8266 lacks hardware I2C. The Wire library bit‑bangs, so timing tightens and breaks. Quote: "default speed of 100 kHz is too high." Lower the clock to 50 kHz. [Elektroda, khoam, post #18475388]

What I2C clock speed should I use between two ESP8266 boards?

Use 50 kHz. The default 100 kHz often causes lost bytes when both sides are ESP8266. Set Wire.setClock(50000UL) on the master before transmissions. This reduces timing stress from bit‑banged I2C and improves reliability across boards. [Elektroda, khoam, post #18475388]

Can ESP8266 act as an I2C slave reliably under Arduino?

Partially. On Arduino core 2.5.2 it can work but shows duplicated or garbled bytes. On core 2.6.x, slave mode breaks. The ESP8266 has no hardware I2C, so stability depends on timing of the bit‑banged Wire library. Reduce clock to 50 kHz if you must try. [Elektroda, khoam, post #18475388]

Wire.begin(D2, D1, 8) still doesn’t work—what’s wrong?

Your code may be fine; the platform is not. On ESP8266 Arduino core 2.6.x, slave mode is broken, so begin(..., address) won’t help. Downgrade to 2.5.2 and lower the clock to 50 kHz to test. Expect occasional repeats due to bit‑banging. [Elektroda, khoam, post #18475388]

Does ESP32 support I2C slave mode with the Arduino Wire library?

The ESP32 hardware supports I2C slave, but Arduino’s ESP32 Wire library does not implement slave mode. You cannot use Wire as a slave from the Arduino IDE today. Consider other transports or firmware stacks if you need slave on ESP32. [Elektroda, khoam, post #18504298]

How can I communicate ESP↔ESP without an I2C slave?

Use I2C multi‑master (master‑to‑master) techniques. Both nodes act as masters and arbitrate bus access. This avoids relying on the missing or broken slave implementation. See the referenced master‑to‑master pattern for Arduino‑style setups. [Elektroda, khoam, post #18505237]

How do I safely connect an Arduino Uno/Mega as I2C slave to an ESP8266 master?

Add a bidirectional 5V↔3.3V level shifter on SDA and SCL. Uno/Mega use 5V logic and can damage the 3.3V ESP pins. Power pull‑ups to the correct rails on each side. Keep grounds common. This prevents over‑voltage on the ESP. [Elektroda, khoam, post #18476437]

How can I quickly tell if the master actually sees the slave?

Print the result of Wire.endTransmission(). A 0 indicates success. Any value above 0 indicates failure, with 2 meaning NACK on the addressed device. This simple check narrows wiring or addressing mistakes fast. [Elektroda, khoam, post #18474289]

What pull‑up resistors should I use on I2C with ESP boards?

Use typical 4.7 kΩ pull‑ups to 3.3 V on SDA and SCL when linking ESP8266 boards. Place them near the bus and keep wires short. Ensure both boards share ground and connect SDA↔SDA, SCL↔SCL. [Elektroda, omnixcrs, post #18474243]

Which pins do I pass to Wire.begin() on NodeMCU/Wemos D1 mini?

Use Wire.begin(D2, D1). This maps SDA to D2 (GPIO4) and SCL to D1 (GPIO5) on common ESP8266 dev boards. Keep the same mapping on both boards so SDA connects to SDA and SCL to SCL. [Elektroda, omnixcrs, post #18474243]

I see duplicated or garbled bytes across the link—normal?

Yes, on ESP8266 core 2.5.2 tests showed repeating strings and occasional corrupted characters. That’s a symptom of bit‑banged I2C timing. Lower the clock to 50 kHz and expect occasional retries. Avoid core 2.6.x for slave links. [Elektroda, khoam, post #18475388]

Can I run 3–4 OneWire (DS18B20) buses on one ESP to cover ~30 sensors?

Yes. Create multiple OneWire instances on different pins and segment the network. For long runs, follow 1‑Wire cabling rules: use bus topology, short stubs, and consider strong pull‑up. Typical well‑designed segments can reach around 100 m. [Maxim, 2007]
Generated by the language model.
ADVERTISEMENT