Serial communication between ESP32 and Arduino Pro Mini

Hi,

I’ve got a project where I’m automating an automotive air con unit, and have an Arduino pro mini doing most of the grunt work (checking temps, opening valves etc) but want an ESP32 to be the controller so I can display on a screen and control over bluetooth or wifi.

So… I’m looking for a relatively easy way to connect the two. I was going to use I2C but I’m using that on the Arduino to control some slave devices so didn’t want to complicate things by using it for the ESP32->Arduino comms.

I’ve been looking around for some worked examples of using serial comms between the Arduino and ESP32 but there’s not much out there, and what I have tried is not working. The topics using HardwareSerial I’m not sure about because I’m not sure if the protocols are the same between the 2 devices. I’d like to use SoftwareSerial on the Arduino and there is a library on the ESP for software serial but it’s not very well documented.

Does anyone have a suggestion on where I can find a worked example connecting these devices reliably?

Thanks

Tim

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

Thanks. That's a nice explanation of the serial comms.

But I'm more specifically looking for how I set up an ESP32 so it talks the same protocols etc

profanegardener:
But I'm more specifically looking for how I set up an ESP32 so it talks the same protocols etc

I have not tried but I assume my code will work on an ESP32 ?

You will need a voltage divider (or equivalent) to convert the 5v from the Arduino's Tx pin to the 3.3v required by the ESP32. The ESP32's 3.3v Tx should work fine with the Arduino's Rx

...R

You're right, but there are the HardwareSerial libraries you can use in the ESP32, and there's a porting of the SoftwareSerial to the platform.

But I've not been able to figure out how to configure either of them to talk the same protocol as the arduino native serial.

btw I'm using a logic converter between the 2 devices.

Cheers,

Tim

profanegardener:
But I've not been able to figure out how to configure either of them to talk the same protocol as the arduino native serial.

The Arduino hardware serial defaults to 8 bits, no parity, 1 stop bit but that can be changed.

https://www.arduino.cc/reference/en/language/functions/communication/serial/begin?from=Reference.BeginSerial

You would need to configure the ESP32's library to be the same.

.

After verifying both boards are configured with the same bits/parity/stop bit settings, you can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 254 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  char buff[] = "hi";

  myTransfer.txObj(buff, sizeof(buff));
  myTransfer.sendData(sizeof(buff));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    char buff[40];
    
    myTransfer.rxObj(buff, sizeof(buff));
    
    Serial.println("New Data: ");
    Serial.write(buff, sizeof(buff));
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");

    if(myTransfer.status == -1)
      Serial.println(F("CRC_ERROR"));
    else if(myTransfer.status == -2)
      Serial.println(F("PAYLOAD_ERROR"));
    else if(myTransfer.status == -3)
      Serial.println(F("STOP_BYTE_ERROR"));
  }
}

For theory behind robust serial communication, check out the tutorials Serial Input Basics and Serial Input Advanced.

profanegardener:
You're right, but there are the HardwareSerial libraries you can use in the ESP32, and there's a porting of the SoftwareSerial to the platform.

I don't understand that as a response to my Reply #3. I have been assuming you would use HardwareSerial on the ESP32.

...R

Thanks all for the responses.

I ended up sorting the issue -I was getting what I thought were junk values but turns out it was a stupid parsing error and my failure to look for the obvious :frowning:

And then I was looking at alternatives (ESP Software serial) because the first thing wouldn't work properly.

But thanks Mr PB for the tip on the SerialTransfer library -that will certainly make things easier!

Cheers,

Tim