Serial communication between Arduino pro mini and ESP32

Hello I'm having problem with serial communication between Arduino pro mini and ESP32. For receiver I'm using this code:

char mystring[20];               //Initialized variable to store receive
void setup() {
 Serial.begin(115200);             // Begin the Serial at 9600 Baud
}
void loop() {
  if(Serial.available()){
    Serial.readBytes(mystring,15);  //Read the serial data
    Serial.println(mystring);       //Print data on Serial Monitor
  }
}

for transmit:

char mystring[16] = "not working? ";     //String data which is to be sent
void setup() {                
 Serial.begin(115200);                 // Begin the Serial at 9600 Baud rate
}
void loop() {
 Serial.write(mystring,16);             //Write the serial data
 delay(1000);
}

But reciever getting this:

n��wK�i��
K�i��

��wK�i��
�i��

When i use serial communication between 2 pro minis I am getting it worked, but with ESP32 it just doesn't works.... Please i need help :frowning:

If the Pro Mini runs on 5V, a logic level shifter is required to communicate with the 3.3V ESP32.

1 Like

There are two versions of Po Mini -- 3.3V and 8 MHz; 5V and 16 MHz. Which one you own?

Who is the receiver -- ESP32?

I am using pro mini 8MHz 3.3V. And after I get is works, both will be receiver and transmitter.

did you connect the GNDs ?

I did. I even powering pro mini with ESP32.

that might be an issue, enough current available ?

also - you are connecting both the Serial monitor and the ESP to the same UART, weird things can happen

Well first think I tried was that I only connected RX, TX, gnd and 3.3V and print data from esp32 to pro mini OLED display, but still I got some weird things. After that, I tried connect to pro mini with programmer and listen to communication, but still nothing.

I will try now 2 power supplies.

ok, it is not current problem. I am still getting weird things.

And if i try print that string as int i am getting 278. "Serial.println((int)mystring);"

This is my connection. If I change esp32 to another pro mini, communication works, but with ESP32 it just not and I don't know why.


Q1. What type of MCU is being used in Pro Mini?

Q2. Is it ATmega328 as I can see in the following pinout diagram (Fig-1)?

Q3. Your Pro Mini does not have TTL/USB converter/connector -- how do you upload sketch into its flash?

Q4. How do you know that your Pro Mini is of 3.3V version?


Figure-1:

I see only 3 wires
You need Rx and Tx but also a stable power supply including GND

A1 what is MCU? :smiley:

A2 ye, it is ATmega328

A3 with CH340 programmer

A4 well it works on 3.3V and i bought 3.3V, even tested it with multimeter. And with 3.3V it works just fine

ye, my power supply is just usb cable withs is connected to pc. One cable for esp32. Pro mini is powered via CH340 programmer.

try remobing ground

try the following between an ESP32 Tx pin 17 Rx pin 16 and pro mini Tx pin 9 Rx pin 8
connect ESP32 Tx pin 17 to pro mini Rx pin 8
connect ESP32 Rx pin 16 to pro mini Tx pin 9
pro mini code

// Arduino pro mini AltSoftSerial test using pin 8 Rx and 9 Tx

#include <AltSoftSerial.h>
#include <SPI.h>
#include <Wire.h>

// pro mini RX pin 8
// pro mini TX pin 9

AltSoftSerial simSerial;

void setup() {
  Serial.begin(57600);    // on pro mini 115200 gives problems ???
  Serial.println("AltSoftSerial test pro mini pin 8 Rx and 9 Tx");
  simSerial.begin(38400);
}

void loop() {
  if (Serial.available()) {
    char command = Serial.read();
    //Serial.println(command);
    simSerial.print(command);
  }
  while (simSerial.available()) {
    char reponse = simSerial.read();
    Serial.print(reponse);
  }
}

ESP32 code

// ESP32  Serial1 test - for loopback test connect pins 16 and 17

#define RXD1 16
#define TXD1 17

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  //Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial1.begin(38400, SERIAL_8N1, RXD1, TXD1);
  Serial.println();
  Serial.println("\n\nESP32 serial1  test Rx pin 16 Tx pin 17");
  Serial.write("   for loopback test connect pin 16 to pin 17\n");
}

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);
  }
}

pro mini serial monitor output when text received from ESP32

AltSoftSerial test pro mini pin 8 Rx and 9 Tx
hello from ESP32
1234567890
abcdefgh

ESP32 serial monitor output when text received from pro mini

ESP32 serial1  test Rx pin 16 Tx pin 17
   for loopback test connect pin 16 to pin 17
hello from pro mini
0987654321
abcdefghijklmnopqrstuvwxyz

got it!

sender:

double hunger = 20;
double happiness = 10;
double health = 15;
double discipline = 100;
double weight = 1;
double age = 0;

void setup() {
  Serial.begin(57600);
  hunger = 100.12345;
  happiness = 100.1234;
  health = 100.12345;
  discipline = 100.1234;
  weight = 1.1234;
  age = 0.1234567;
}

void loop() {
  // Převedi hodnoty z double na unsigned long
  unsigned long hunger_int = (unsigned long)(hunger * 100000.0);
  unsigned long happiness_int = (unsigned long)(happiness * 10000.0);
  unsigned long health_int = (unsigned long)(health * 100000.0);
  unsigned long discipline_int = (unsigned long)(discipline * 10000.0);
  unsigned long weight_int = (unsigned long)(weight * 10000.0);
  unsigned long age_int = (unsigned long)(age * 10000000.0);

  // Odešli hodnoty přes sériovou linku
  Serial.write((byte*)&hunger_int, sizeof(unsigned long));
  Serial.write((byte*)&happiness_int, sizeof(unsigned long));
  Serial.write((byte*)&health_int, sizeof(unsigned long));
  Serial.write((byte*)&discipline_int, sizeof(unsigned long));
  Serial.write((byte*)&weight_int, sizeof(unsigned long));
  Serial.write((byte*)&age_int, sizeof(unsigned long));

  delay(1000); // Případné zpoždění mezi odesláním dat
}

rec:

double hunger = 20;
double happiness = 10;
double health = 15;
double discipline = 100;
double weight = 1;
double age = 0;

unsigned long hunger_int = 1234567891;
unsigned long happiness_int = 1234567891;
unsigned long health_int = 1234567891;
unsigned long discipline_int = 1234567891;
unsigned long weight_int = 1234567891;
unsigned long age_int = 1234567891;

void setup() {
  Serial.begin(57600);
  // Nastavení rychlosti sériové komunikace na 57600 baudů (musí odpovídat rychlosti v druhém kódu)
}

void loop() {
  if (Serial.available() >= 6 * sizeof(unsigned long)) {
    // Pokud jsou dostupná data (6x unsigned long odpovídající 6 proměnným)

    // Přečti data a ulož je do proměnných
    Serial.readBytes((char*)&hunger_int, sizeof(unsigned long));
    Serial.readBytes((char*)&happiness_int, sizeof(unsigned long));
    Serial.readBytes((char*)&health_int, sizeof(unsigned long));
    Serial.readBytes((char*)&discipline_int, sizeof(unsigned long));
    Serial.readBytes((char*)&weight_int, sizeof(unsigned long));
    Serial.readBytes((char*)&age_int, sizeof(unsigned long));

    // Převedi hodnoty zpět na původní typ double
    hunger = (double)hunger_int / 100000.0;
    happiness = (double)happiness_int / 10000.0;
    health = (double)health_int / 100000.0;
    discipline = (double)discipline_int / 10000.0;
    weight = (double)weight_int / 10000.0;
    age = (double)age_int / 10000000.0;

    // Zde můžete provést další zpracování s načtenými hodnotami, pokud je to potřeba

    // Vypiš hodnoty pro kontrolu
    Serial.print("hunger: ");
    Serial.println(hunger, 6);
    Serial.print("happiness: ");
    Serial.println(happiness, 5);
    Serial.print("health: ");
    Serial.println(health,6);
    Serial.print("discipline: ");
    Serial.println(discipline, 5);
    Serial.print("weight: ");
    Serial.println(weight,5);
    Serial.print("age: ");
    Serial.println(age,8);
  }
}

there was problem with baut rate

if for some reason your receiver starts (or restarts) after the the sender and you have already some bytes in transit, you have no way to sync.

it would be more robust if you had some sort of start and/or end marker to be able to synchronise both processes

side note : if you were to store the payload in a structure, it would be easier to send and receive. (careful on types as a double might be 4 bytes only on a 8 bit µC and 8 bytes on a 32bit µC likes the ESP32)

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