Transfer data from ESP32 to Arduino Uno using UART

Hello everybody. I'm developing an electronic lock program using Arduino and ESP32. ESP32 checks the state of a document in Firebase, and if it's true, it releases the lock.

I saw this tutorial (Arduino and ESP32 Serial Communication || UART Communication - YouTube), but in the video, it uses the ESP32 as an Arduino receiver, and I need it to be the other way around. Could you help me?

I'm using the following codes:

// ESP32 code that sends the message to the arduino uno

#include <HardwareSerial.h>

void setup() {
  Serial.begin(9600, SERIAL_8N1, 16, 17);
  delay(100);
}

void loop()
{
    String mssage = "test";

    Serial.println(mssage);
    delay(500);
}
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

void setup(){
  mySerial.begin(9600);
  Serial.begin(115200);
  delay(100);
}

// Arduino Uno code to receive the message
void loop(){
  if(mySerial.available()){
    String received = "";
    received = Serial.readStringUntil('\n');
    Serial.println(received);
  }
}

Note that I had to use the serial software because the Arduino Uno only has one serial.

what is the role of the UNO ? the ESP32 is far more powerful - cannot it do the whole task?
this thread arduino-mega-2560-serial-communication-to-esp8266-nodemcu should give to some ideas for connecting the UNO to an ESP32
note:

  1. use softwareserial on the UNO
  2. use hardware serial on the ESP32

What does the UNO do?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hi Horace, thanks for the reply

I use Arduino for two reasons:

1st - It's a college project that aims to use Arduino
2nd - The lock works with 5v, the ESP32 only works with 3.3v

Do you think it is possible to use Arduino as receiver and ESP32 as transfer?

Heey Tom, thanks for the reply.

So, the UNO serves to receive data from ESP32 via UART, and activate the lock by sending a 5v to a relay that connects the lock.

Is your problem solved?

I would have thought a level converter would enable the ESP32 3.3V to drive a 5V lock

however, it is possible to send serial data from an ESP32 to a UNO, e.g. the ESP32 can use Serial1 hardware port

// ESP32-CAM  Serial1 test

#define RXD1 14
#define TXD1 15

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("serial1  test Rx pin 14 Tx pin 15");
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);
    Serial1.write(inByte);
  }
}

note that the UNO uses 5V logic and the ESP32 3.3V logic therefore if you are sending sending from UNO to ESP32 you require a level converter on the UNO Tx signal

Is the only purpose of the UNO so you can have a 5v pin available to drive the relay? you definitely don't need to do that. You may just need a low-side transistor or mosfet switch.Gammon Forum : Electronics : Microprocessors : Driving motors, lights, etc. from an Arduino output pin.

2N7000 is an logic level N-channel mosfet which can allow a 3.3v ESP32 pin to drive a relay powered by 5v. Tom's diagram below will work.

image

Get your ESP32 to drive the gate of the MOSFET.
The MOSFET must be N-CH Logic Level.

Tom... :smiley: :+1: :coffee: :australia:

Unfortunately not, Golam :frowning:

Unfortunately for the college project I need to use an arduino UNO :frowning:

Hi,

How far are the ESP32 and UNO apart and do you have to use UART?
Is the signal sent just ESP to UNO?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Unfortunately this code did not work, horace, the arduino is not receiving information from ESP32. I put a 1k omh resistor on both the tx and the rx.

Exactly Tom, esp32 just needs to send the command for the lock to open to UNO.

Hi,
Have you got the gnds of the ESP and the UNO connected?
Does "exactly" mean you must also use UART?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, component names and pin labels.

Tom.. :smiley: :+1: :coffee: :australia:

Just have the ESP32 drive a pin HIGH/LOW as if it were directly driving the lock.
The UNO does nothing but monitor the input from that pin, and drive an output pin for the lock.
No need for serial communications at all, just a highly inefficient 3.3V to 5V signal converter.

1 Like

In fact, I chose UART because it was recommended. About the circuit by hand, I don't have one drawn, sorry, I'm extremely beginner in this arduino world. But I can post a picture of my assembled system if you want

and you also need wifi and serial communication (UART)?

Is this converter purchased?

Exactly, the ESP32 receives the lock status from Firebase (this part is already working), and sends the status to the Arduino Uno, which is responsible for opening or closing the lock.