Hi all I am hoping you can assist with the following. I am working on a project where I need to get an ESP32s3 and a ESP32 to communicate with a Arduino Mega. I tried just about all the examples I could find but with no success. Do somebody have a working example I can try. My code is quite long so will be difficult to sent. I tried RX/TX I tried I2C but none
H Jacques,
Use serial, there are ways I2C could be used but it is neither designed for, nor good at interprocessor communications.
Secondly; you need to do voltage level conversion; the esp32 is a 3.3v machine, the Mega is 5v. You will damage the esp32 if you just wire them straight through. Search online for level converter circuits; you can either build them yourself or purchase a âbi-directional level converterâ module from your favorite cheap parts supplier. Make sure it is suitable for 5v<->3v conversion.
Thirdly; Swap the RX and TX lines, RX out of one board goes to TX of the other board and vice-versa.
Then look in the Arduino IDE for the examples->04.Communications
examples, especially the SerialPassThrough
example shows how to use this in a basic form. In your code use serial.begin()
in the same way on each board. Then Serial.write()
on one and Serial.read()
on the other as appropriate.
There are a lot of videos and pages about Arduino serial comms showing up in simple searches; so look to these for more background etc.
Also; once you can send/recieve text; maybe that is enough for your needs. But if you need to send complex data you then need to get into data serialisation, and asynchronous communications. 'JSONâ is your friend here.
Hi Jaques
This a chatgpt4 response to the request for
âcode to allow an ESP32s3 and a ESP32 to communicate with a Arduino Megaâ
ChatGPT said:
ChatGPT
To allow an ESP32-S3, an ESP32, and an Arduino Mega to communicate, you can use a variety of communication protocols like UART (Serial Communication), I2C, or SPI. In this example, I will show you how to set up UART communication, which is the most common and simple form of communication for these devices.
Scenario
- ESP32-S3 will send data to Arduino Mega.
- ESP32 will send data to Arduino Mega.
- The Arduino Mega will act as a master, receiving data from both ESP32 devices.
Hardware Connections
- ESP32-S3 to Arduino Mega (using UART):
- ESP32-S3 TX â Arduino Mega RX (Pin 10)
- ESP32-S3 RX â Arduino Mega TX (Pin 11)
- ESP32 to Arduino Mega (using UART):
- ESP32 TX â Arduino Mega RX (Pin 12)
- ESP32 RX â Arduino Mega TX (Pin 13)
Ensure that both ESP32 and Arduino Mega are properly powered, and the ground (GND) pins are connected.
Code Example for Communication
1. Arduino Mega Code (Master)
This code will read data from both ESP32s via two UARTs, process it, and print it on the Serial Monitor.
cpp
Copy code
void setup() {
// Start Serial Monitor for debugging
Serial.begin(115200); // For Arduino Mega communication
Serial1.begin(115200); // For ESP32-S3 communication (using Serial1)
Serial2.begin(115200); // For ESP32 communication (using Serial2)
Serial.println("Arduino Mega Initialized");
}
void loop() {
// Check if there's data from ESP32-S3
if (Serial1.available() > 0) {
String data = Serial1.readString(); // Read data from ESP32-S3
Serial.println("Received from ESP32-S3: " + data);
}
// Check if there's data from ESP32
if (Serial2.available() > 0) {
String data = Serial2.readString(); // Read data from ESP32
Serial.println("Received from ESP32: " + data);
}
delay(100); // Small delay to avoid excessive CPU usage
}
2. ESP32 Code (Slave 1) - ESP32-S3
This code will send data to the Arduino Mega over UART.
cpp
Copy code
void setup() {
// Start serial communication with the Arduino Mega
Serial.begin(115200);
Serial1.begin(115200); // For communication with Arduino Mega via Serial1
Serial.println("ESP32-S3 Initialized");
}
void loop() {
// Send a message to Arduino Mega
Serial1.println("Hello from ESP32-S3!");
delay(1000); // Wait for 1 second before sending another message
}
3. ESP32 Code (Slave 2)
This code will also send data to the Arduino Mega over UART.
cpp
Copy code
void setup() {
// Start serial communication with the Arduino Mega
Serial.begin(115200);
Serial2.begin(115200); // For communication with Arduino Mega via Serial2
Serial.println("ESP32 Initialized");
}
void loop() {
// Send a message to Arduino Mega
Serial2.println("Hello from ESP32!");
delay(1000); // Wait for 1 second before sending another message
}
Key Notes:
- Serial1 and Serial2 are used for communication with the Arduino Mega, leaving Serial (which is the USB serial port) for debugging purposes.
- Both ESP32 and ESP32-S3 are sending messages every second, and the Arduino Mega will receive and print these messages.
- The baud rate (
115200
) should be consistent across the devices for proper communication.
Conclusion
This setup allows two ESP32 devices (ESP32-S3 and ESP32) to communicate with the Arduino Mega using UART. You can extend this code to support other protocols like I2C or SPI depending on your needs, but UART is often the easiest to implement for simple communication.
4o mini
ChatGPT âforgotâ about the voltage differences. Please use a level converter between 5v ATMega powered devices and 3.3v ESP32âs (and pretty much all modern MCUâs)
But otherwise a good answer, just ignore all the 2nd ESP32 code and references to test comms from only one esp32 device.
There are many reference online to both ESP8266 and ESP32 being 5V tolerant on GPIOs. Hereâs one:
https://www.letscontrolit.com/forum/viewtopic.php?t=8845
The referenced ridiculously-simple forum appears to have been abandoned, so itâs good that archive.org has returned from being attackedâŚ
You could also use a voltage divider on the ESP32 RX, and the 3.3V should be sensed as high in the other direction on the arduino RX.
A level shifter is cheap and easy and at 115200bps wonât cause problems. You can decide whether to go that route.
Because you missed the detail that the ATMega is a 5V I/O device and the ESP32s are 3.3V devices you really should get the ESP32 devices talking over their UARTs to each other first. They are both 3.3v devices after all. As was mentioned, youâve possibly already damaged the UART ports on the ESP32s by connecting them to the ATMega board so best to just validate they work.
And at this point you should also have a better understanding of how to send msgs back and forth between the ESP32s since itâll be the same for the ATMega when you have your level shifters or resistor dividers in place.
Thanks Michael, another day, another new thing learnedâŚ
I guess the âno 5V on 3V chipsâ mantra is too deeply ingrained, never occurred to me to check, but I shouldnât be surprised. Especially since the esp pins will be high-impedence when in RX mode. And it makes sense for Expressif to make the pins 5v tolerant just to maximize compatibilty.
Edit: Interesting quote:
TLDR; According to Swee-Ann, the info is not in the data sheet. Confused users think â5V Tolerantâ means you can power the chip with 5V. So the wording is scrubbed from Espressif docs to keep the ESP chips out of harms way.
Yeah, itâs not really that hard to make digital pins 5V tolerant on a 3V3 design, because of the high impedance. ADCs are of course not 5V tolerant because of their 3V3 reference.
Note that the ESP32 GPIOs are also 1V8 tolerant, in that 1.8V will be read as high
@dougl take a look at those posts I linked to. The ESP32 is 5V tolerant on its GPIOs including RX.
I was figuring it was only some of the newer ESP32 devices but that first link talks about the original ESP2866 so wow, really good to know all the GPIO on the ESP32s are 5v tolerant.
Yeah, in the early days of the ESP32 there were a lot of folks stress-testing the ESP32 at 5V to see whether it would work, and AFAIK they all said that they saw no long-term damage at 5V, before Espressif explained why it was working.
Thanks for that. I assume as what easytarget say I need to install a voltage convertor with this circuit as well?
Thanks a lot for the info. I will get a convertor and try again. I wasted too much time here without knowing.
Thanks again, will do that.
Hi Duncan. The code seems to work, but the data I get from ESP32 only show in square blocks not data.
You should have the wrong display rate on your terminal. Be sure it is 115200 bauds.
Regarding the level shifting, I have tested it, you can safely input 5V logic on the ESP32âs GPIOs.
On the output side, stay at 3.3V and your arduino should see it as a âhighâ signal. So it should work !
But NEVER put 5V on the 3.3V power pinâŚ