3

I want to send data between an ESP-32 (NODEMCU-32S) and an Arduino Uno but I haven't found any source on how to do that, so I tried this code:

ESP-32 code:

#include <Wire.h>

void setup() {
  Serial.begin(9600);
  Wire.begin(21, 22);      /* join i2c bus with SDA=D1 and SCL=D2 of NodeMCU */
}

void loop() {
  Wire.beginTransmission(8);
  Wire.write(3);  /* sends hello string */
  Wire.endTransmission();  /* stop transmitting */
  Wire.requestFrom(8, 18); /* request & read data of size 13 from slave */
  while (Wire.available()) {
    char c = Wire.read();
    Serial.print(c);
  }
}

And Arduino code:

#include <Wire.h>

void setup() {
  Wire.begin(8);                /* join i2c bus with address 8 */
  Wire.onReceive(receiveEvent); /* register receive event */
  Wire.onRequest(requestEvent); /* register request event */
  Serial.begin(9600);           /* start serial for debug */
}

void loop() {
  delay(100);
}

// function that executes whenever data is received from master
void receiveEvent(int howMany) {
  while (0 < Wire.available()) {
    int c = Wire.read();        /* receive byte as a character */
    Serial.print(c);            /* print the character */
  }
}

// function that executes whenever data is requested from master
void requestEvent() {
  Wire.write("hello esp");
}

But I found that no data was sent.

I connected pin 21 to A4 and 22 to A5 directly.

I cannot find where the problem is. Also, I don't know whether the device address is correct or not, since when I tried an I2C scanner it didn't discover any device.

Also, I hope that there is a similar method to have two-way UART communication between the Arduino and ESP as I found none since I need both chips to send data to each other.

5
  • As long as a scan on the ESP doesn't find the Arduino, no point in going forward. Strip both pieces of code to the bare minimum (only I2C and whatever serial you need to debug). Make sure you didn't swap the wires. Add pullups (4.7k or 10k) to both I2C lines.
    – Mat
    Dec 14 '20 at 10:22
  • I made sure of all of that Dec 14 '20 at 10:25
  • also, an example code may be helpful please that prints hello on both cards Dec 14 '20 at 10:26
  • @Mat I edited the code so that it is easier now Dec 14 '20 at 10:35
  • 3
    "I hope if there is a similar method to have two way UART communication between the arduino and esp" - If you want to use UART, why not using UART? It's easier to use than I2C.
    – chrisl
    Dec 14 '20 at 10:57
1

I'm working on something similar at the moment. Not a complete answer put some thoughts:

  • Have you considered that the Uno is a 5v board and the ESP32 is 3.3v? You may need a signal level shifter (there are many to chose from) something like Pololu-2595 which is breadboard friendly.

  • There's a good ESP32 I2C explanation at Random Nerd Tutorials.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.