"[solved]" Serial communication between arduino and 2 esp32

hello and good day everyone

im trying to communicate between 1 eps32 and 1 Arduino mega. my project is, i want the mega to read from a few sensor and send it to esp32. the esp32 is responsible to receive from the mega and send it to thing speak. how do i make the communication between the mega and esp32 because both of them works on different voltage?

i found a logic level shifter from Adafruit. also when i dig around, i found this logic shifter from Texas instrument. my question is, which shifter should i use because i want to make a custom PCB so it would look cool

or any other alternative method that i can use

a simple resistor voltage divider on the MEGA Tx line would likely be enough but any level shifter will do probably otherwise. Some come ready to use (look for "I2C level shifter" - they come usually under that name but not restricted for use on I2C)

so this module can shift from 5v to 3.3v? if that is the case, imma try it out. but the drawing?, where can i get it from?

yes this module does that, shifting both ways between a higher and lower voltage.

Most stores selling kits and electronics for DIY type of stuff will carry it. Depends where you are in the world and how long do you want to wait. (you can get some cheaply from the Far East but it takes a few weeks or use amazon and get it tomorrow - of if you happen to have an electronic store nearby....)

search level shifter

Do you know how to write a protocol for the Serial communication ?
The Mega could send a few values every second. Then you only have to read it in the ESP32.

It is easier if you can do your project with just the ESP32.
Below the Arduino software, there FreeRTOS running. You could create a few tasks to do different things.
You could even create a "Mega" task and run everything in that task that the Mega was supposed to do :wink:

A project is easier with a single board that has a single processor and when there is only one sketch to maintain.
Even if extra hardware is needed for extra pins or drivers for motors or ledstrips, it is still easier with a single processor.

Can you give us a broader view of your project ?

well, im trying to read a few sensor data from the Arduino mega and send it to my thingspeak dashboard using esp32. that is why im looking for a specific component/module to help me translate from the 5V to 3.3V because from what I understand, Arduino mega works only with 5V and esp32 works only with 3.3v

the module that given by @J-M-L looks like it should work.

you can use a potential divider in the Mega TX to ESP32 Rx line, e.g.
image

the ESP32 Tx to Mega Rx can be a direct connection

EDIT: why use the Mega at all? could the complete project be implemented on the ESP32?

ok, but what if i want to tell the mega that esp32 already receive and send the serial to thingspeak dashboard?

the IC that i found from Texas instrument seems need to have some kind of work, it also seems intersting. imma keep digging on how to use it, once i hit a wall. then i use the solution that provided by @J-M-L

i found out that the IC has OE and DIR pin, from what i understand from the datasheet, the OE pin need to be pulled to ground so it can enable the output. but i do not see any voltage level saying that it can be 5V on the DIR pin anywhere in the datasheet.

or i can change the project from using a mega, just use the esp32 and send it directly. it would make the whole thing much easier.

The ESP32 has only one spare Serial/UART port: Serial2 (The Mega has three spare Serial/UART ports).
The ESP32 has a 3.3V I2C bus, which is more convenient with 3.3V sensors and OLED displays.
There are I2C GPIO expander boards for extra pins: https://www.adafruit.com/product/5611
The ESP32 is a whole family, you have something to choose from (ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6, ESP32-H2, ESP32-P4).

the ESP32-WROOM-32 I use have two spare hardware serial ports Serial1 and Serial2

test program where text entered on Serial is sent to Serial1 Tx to Serial2 Rx to Serial2 Tx to Serial1 Rx to be displayed on serial monitor

// ESP32  Serial1 and Serial2 test
//
// text entered on Serial sent to Serial1 Tx to Serial2 Rx to Serial2 Tx to Serial1 Rx to be displayed on serial monitor

/* There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
 * 
 * U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
 * U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
 * U2UXD is unused and can be used for your projects.
*/

#define RXD1 12  // for loopback jumper these pins
#define TXD1 13

#define RXD2 16
#define TXD2 15


void setup() {
  // initialize both serial ports:
  delay(5000);
  Serial.begin(115200);
  Serial.println();
  Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.println("ESP32 hardware serial test on Serial1 and Serial2");
  Serial.println("Serial Txd is on pin: " + String(TX));
  Serial.println("Serial Rxd is on pin: " + String(RX));
  Serial.println("Serial1 Txd1 is on pin: " + String(TXD1));
  Serial.println("Serial1 Rxd1 is on pin: " + String(RXD1));
  Serial.println("Serial2 Txd2 is on pin: " + String(TXD2));
  Serial.println("Serial2 Rxd2 is on pin: " + String(RXD2));
  Serial.println("connect " + String(TXD1) + " to " + String(RXD2)
                 + " and " + String(TXD2) + "  to " + String(RXD1));
  Serial.println("text entered on Serial sent to Serial1 Tx to Serial2 Rx to Serial2 Tx to Serial1 Rx to be displayed on serial monitor");
}

void loop() {
  while (Serial2.available()) {
    String s = Serial2.readStringUntil('\n');
    Serial.println("Serial2 = '" + s + "'");
    Serial2.println("from Serial 2 '" + s + "'");
  }
  while (Serial1.available()) {
    Serial.println("Serial1 = " + Serial1.readStringUntil('\n'));
  }
  while (Serial.available()) {
    String s = Serial.readStringUntil('\n');
    Serial.println("\nTransmitting to Serial1 '" + s + "'");
    Serial1.print("from Serial 1 '" + s + "'");
  }
}

serial monitor output

ESP32 hardware serial test on Serial1 and Serial2
Serial Txd is on pin: 1
Serial Rxd is on pin: 3
Serial1 Txd1 is on pin: 13
Serial1 Rxd1 is on pin: 12
Serial2 Txd2 is on pin: 15
Serial2 Rxd2 is on pin: 16
connect 13 to 16 and 15  to 12
text entered on Serial sent to Serial1 Tx to Serial2 Rx to Serial2 Tx to Serial1 Rx to be displayed on serial monitor

Transmitting to Serial1 'test 1 hello'
Serial2 = 'from Serial 1 'test 1 hello''
Serial1 = from Serial 2 'from Serial 1 'test 1 hello''

Transmitting to Serial1 'test 2 1234567890'
Serial2 = 'from Serial 1 'test 2 1234567890''
Serial1 = from Serial 2 'from Serial 1 'test 2 1234567890''
1 Like

Thank you.
So Serial1 is working for both RX and TX on the classic ESP32 ? Good to know. There was trouble with Serial1.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.