Receiving Random Characters when Transferring Data from Arduino Uno to ESP32

Hello, I'm trying to send data from Arduino Uno to ESP32 with the goal of inserting that data into a database. However, I'm encountering an issue when receiving data from Arduino Uno, where the received data contains random characters. What could be causing these random characters? Please help me to resolve this issue.

This is the Arduino Serial Monitor.
image

This is the ESP Serial Monitor.
image

Arduino Program

#include<SoftwareSerial.h>
SoftwareSerial PIN(2,3);



//TDS
#include <EEPROM.h>
#include "GravityTDS.h"
#define TdsSensorPin A1
GravityTDS gravityTds;
float temperature = 28.4, tdsValue = 0;

//pH
#define SensorPin A0           //pH meter Analog output to Arduino Analog Input 0
#define Offset -29       //deviation compensate
#define LED 13
#define samplingInterval 50
#define printInterval 1000
#define ArrayLenth  100    //times of collection
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex = 0;

int FloatSensorAtas = 2;
int FloatSensorBawah = 4;

//Turbidity
int pinTurb = A3;
float V;
float result;
float kekeruhan;
float VRata2;
float VHasil;

//Relay
const int ABMix = 13; // 4
const int pHuP = 12; //  2
const int pHdown = 7; // 3

void setup() {
  //TDS
  Serial.begin(230400);
  PIN.begin(230400);
  gravityTds.setPin(TdsSensorPin);
  gravityTds.setAref(5.0);  //reference voltage on ADC, default 5.0V on Arduino UNO
  gravityTds.setAdcRange(1024);  //1024 for 10bit ADC;4096 for 12bit ADC
  gravityTds.begin();  //initialization

  kran.attach(3);

  pinMode(ABMix, OUTPUT);
  pinMode(pHuP, OUTPUT);
  pinMode(pHdown, OUTPUT);

}

void loop() {

  //TDS Read
  gravityTds.setTemperature(temperature);  // set the temperature and execute temperature compensation
  gravityTds.update();  //sample and calculate
  tdsValue = gravityTds.getTdsValue();  // then get the value
  Serial.print(tdsValue, 0);
  Serial.println("ppm");

  //pH Read
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
  if (millis() - samplingTime > samplingInterval) {
    pHArray[pHArrayIndex++] = analogRead(SensorPin);
    if (pHArrayIndex == ArrayLenth) pHArrayIndex = 0;
    voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
    pHValue = 20 * voltage + Offset;
    samplingTime = millis();
  }

  //Turbidity Read
  V = 0;
  for (int i = 0; i < 800; i++) {
    V += ((float)analogRead(pinTurb) / 1023) * 5;
  }

  VRata2 = V / 800;
  VHasil = roundf(VRata2 * 10.0f) / 10.0f;

  if (VHasil < 2.5) {
    kekeruhan = 3000;
  }
  else {
    kekeruhan = -1120.4 * square(VHasil) + 5742.3 * VHasil - 4353.8;
  }

  result = kekeruhan / 10;

  if (result < 69.0) {
    Serial.print("Air Sangat Jernih  ");
    Serial.print("   Val: ");
    Serial.println(result);
  }
  else if (result > 70.0 && result < 154.0) {
    Serial.print("Air Jernih ");
    Serial.print("   Val: ");
    Serial.println(result);
  }
  else {
    Serial.print("Air Keruh ");
    Serial.print("   Val: ");
    Serial.println(result);
  }

  Serial.print("Voltage:");
  Serial.print(voltage, 2);
  Serial.print("    pH value: ");
  Serial.println(pHValue, 2);
  Serial.println("  ");

  String datakirim = String(result) + "#" + String(tdsValue) + "#" + String(pHValue);
  PIN.println(datakirim);

  delay(1000);
 
}

double avergearray(int* arr, int number) {
  int i;
  int max, min;
  double avg;
  long amount = 0;
  if (number <= 0) {
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  if (number < 5) { //less than 5, calculated directly statistics
    for (i = 0; i < number; i++) {
      amount += arr[i];
    }
    avg = amount / number;
    return avg;
  }
  else {
    if (arr[0] < arr[1]) {
      min = arr[0]; max = arr[1];
    }
    else {
      min = arr[1]; max = arr[0];
    }
    for (i = 2; i < number; i++) {
      if (arr[i] < min) {
        amount += min;        //arr<min
        min = arr[i];
      }
      else {
        if (arr[i] > max) {
          amount += max;    //arr>max
          max = arr[i];
        }
        else {
          amount += arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount / (number - 2);
  }//if
  return avg;
}

This is ESP Program

#include <SoftwareSerial.h>
SoftwareSerial PIN(35, 34); // RX, TX

String simpantytyd[3];


int count = 0;

String ____DATS;

#define RXp0 3
#define TXp0 1

void setup() {
  Serial.begin(230400);
  PIN.begin(230400);

  
}

void loop() {

  String ____DATS = "";
  while (PIN.available() > 0)
  {
    ____DATS += char(PIN.read());
  }
  Serial.println(____DATS);
  ____DATS.trim();

    if (____DATS != ""){
      int index = 0;
      for (int i = 0 ;  i<= ____DATS.length() ; i++ ){
            char pembatas = '#';
            if(____DATS[i] != pembatas)
            simpantytyd[index] += ____DATS[i];
            else
            index++; 
      }

        if (index == 2){
          Serial.println(simpantytyd[0]);
          Serial.println(simpantytyd[1]);
          Serial.println(simpantytyd[2]);
          Serial.println("");
        }

        simpantytyd[0] = "";
        simpantytyd[1] = "";
        simpantytyd[2] = "";

  }

delay(1000);
}

Please help me, I have been stuck here for hours.

SoftwareSerial can have problems at high baudrates
try reducing the UNO <> ESP32 baudrate to say 9600 baud
if it works increase the baudrate until you determine the maximum reliable baudrate

I have tried setting the baud rate from 9600 to 115200, but nothing has changed. If I change it back to 9600, it will be like this.

just noticed you are using softwareserial on the ESP32 which has hardware serial ports, e.g.
this uses Serial2

// ESP32 serial2 hardware loop back test - jumper GPIO16 (Rx) and GPIO17 (Tx)

// see https://circuits4you.com/2018/12/31/esp32-hardware-serial2-example/
/* 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 RXD2 16
#define TXD2 17

void setup() {
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
  Serial.println("ESP32 hardware serial test on Serial2");
  Serial.println("Serial2 Txd is on pin: "+String(TXD2));
  Serial.println("Serial2 Rxd is on pin: "+String(RXD2));
}

void loop() { //Choose Serial1 or Serial2 as required
  while (Serial2.available()) {
    Serial.print(char(Serial2.read()));
  }
  while (Serial.available()) {
    Serial2.print(char(Serial.read()));
  }
}

1 Like

Finally, it worked! Thank you so much. You have saved me, and I'm incredibly grateful. Thank you once again

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