Need Help with Sending Sensor Data from Arduino Mega to ESP32

Hello everyone,

I'm new to electronics and I'm currently working on a project that involves an Arduino Mega board and an ESP32. The ESP is mounted on a custom-made MEGA 2560 board and are connected by RX and TX pins.

I'm trying to send sensor data from the Arduino Mega to the ESP32, but I'm not sure how to go about it. I've done some research and read somewhere that you send the data via serial communication but not sure how...

If anyone has experience with this or knows how to accomplish this, I would really appreciate your help. Any advice, code snippets, or resources would be extremely helpful.

Heres a code snippet of the sensor readings from the mega board

#include "PinAssign.h"
#include "MyConstants.h"
#include "Variables.h"
// #include "thingProperties.h"

void SensorReadings(){
  //CheckFloat
    //Assign float positions
    const int floatState1_1 = pcf8574.digitalRead(P0);
    const int floatState1_2 = pcf8574.digitalRead(P1);

    //Check Float level  
    if((floatState1_1 == LOW && floatState1_2 == HIGH) && !add_salt){
      lowTrig1 = true;
      EEPROM.update(lowaddress1,true);
      Serial.println("Strong solution\n");
    }    
    else if (floatState1_1 == HIGH && floatState1_2 == LOW){
      lowTrig1 = false;
      EEPROM.update(lowaddress1, false);
      Serial.println("Weak solution...Turning OFF Dehumidifier\n");
      dehumidifier_status = false;
      add_salt = true;
      
    }
    else{
      lowTrig1 = EEPROM.read(lowaddress1);
      Serial.print("Float status: ");
      Serial.println(lowTrig1);
    }
    delay(1000);

  extern float ES135xx_Temp;
  extern float ES135xx_Hum;
  //SHT reading  
    //Storage SHT
      //Storage room sensor
        select_channel(SENSOR2_CHANNEL);
        ES135xx_read_T_H();
        Serial.print("Storage Temp = " + String(ES135xx_Temp) + " C " + " Storage RH = " + String(ES135xx_Hum) + "% | ");  
      // Utility sensor
        select_channel(SENSOR2_CHANNEL);
        ES135xx_read_T_H();
        Serial.println("Utility Temp = " + String(ES135xx_Temp) + " C " + " Utility RH = " + String(ES135xx_Hum) + "%\n");
                       
  //DS18B20
    // call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
    sensors.requestTemperatures(); 
    /*float rawTempC = sensors.getTempCByIndex(0);
    float calibratedTempC = rawTempC + CALIBRATION_OFFSET;
    
    if(rawTempC == -127.00) 
    {
      Serial.println("Error: Could not read temperature data");
    }
    else 
    {
      Serial.print("Temperature: ");
      Serial.println(calibratedTempC);
    }*/
    
    Serial.print("Temperature 1: ");
    Serial.print(sensors.getTempCByIndex(0));   // 0 refers to the first IC on the wire
    Serial.print("\tTemperature 2: ");
    Serial.println(sensors.getTempCByIndex(1)); //  1 refers to the second IC on the wire
    
    delay(1000);

}

Thank you in advance for your time and assistance!

Best,
John

can you give details - which RX and TX pins?
when I connected a Mega to an ESP32 I used the following connections
image
the Mega uses 5V logic the ESP32 3.3V hence the potential divider on the Mega TX to ESP32 Rx

Do not connect 5V outputs directly to 3.3V inputs, as that can destroy both processors. At the very least they can be expected to malfunction.

You should use logic level shifters on all logic connections between the two MCUs, such as this one.

Since the converters generally degrade performance, it is much better not to mix 5V and 3.3V logic. What is the purpose of using these two boards together?

1. UART Port based connection between 5V-MEGA and 3.3V-ESP32 must be via level shifter (Fig-1).


Figure-1:

2. Do you want to acquire temperature signal from DS18B20 sensor by Ardino MEGA and then send it to ESP32 using UART2 Port?

1 Like


Well, the board was built by a third party pcb manufacturer. Im sure the connections were made in a proper way. The esp sits in the breakout section covering the crystal oscillator. All i know is the esp is connected via rx0 and tx0 to the mega board. Yes, the intention is to read sensor data from the mega and send it to the esp. I have been told the communication can be done by serial.print and serial.read both ways

Here' s another picture of the board. Im guessing the red thing on the green board is the level shifter?

Please post a schematic diagram for the entire board, or at the very least, links to the user manual and datasheet.

1 Like

Yes but to what pins on the Mega board?
It has 4 different serial ports.

1 Like

I believe its pins 0 and 1...have to confirm . Will get back to you guys asap. I'll also try to get the schematic and data sheet. How should the code be written though?

If it's 0 and 1 then the snippet you show in post 1 should work provided you initialized the serial port at the proper baud rate.

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