Communication Between ESP32 and Raspberry Pi Using UART

Protonest IoT
3 min readAug 5, 2024

--

In this project, we will set up communication between an ESP32 microcontroller and a Raspberry Pi using UART (Universal Asynchronous Receiver-Transmitter). The ESP32 will send a signal to the Raspberry Pi, and the Raspberry Pi will respond with a dummy value.

Hardware Required

Software Required

  • Arduino IDE
  • Python (installed on Raspberry Pi with Raspberry Pi OS)

Circuit Diagram

ESP32 Code

#include <HardwareSerial.h>

#define RXD2 16 // GPIO16 as RX
#define TXD2 17 // GPIO17 as TX

HardwareSerial mySerial(2); // Use Serial2

void setup() {
Serial.begin(115200); // Initialize Serial Monitor for debugging
mySerial.begin(115200, SERIAL_8N1, RXD2, TXD2); // Initialize Serial2 with defined pins
mySerial.println("ESP32 Ready to communicate with Raspberry Pi");
}

void loop() {
if (mySerial.available() > 0) {
String input = mySerial.readString();
input.trim(); // Trim the input
if (input == "1") {
// Send signal to Raspberry Pi to start processing
Serial.print('1');
mySerial.println("Signal sent to Raspberry Pi to start processing");
}
}

if (Serial.available() > 0) {
String dataFromPi = Serial.readStringUntil('\n');
if (dataFromPi.length() > 0) {
mySerial.println("Data received from Raspberry Pi: " + dataFromPi);
}
}

delay(1000); // Adjust delay as needed
}

Raspberry Pi Python Code

import time
import serial

# Initialize serial communication with ESP32
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)

while True:
# Wait for signal from ESP32
signal = ser.read()
if signal == b'1':
# Dummy processing
dummy_value = 1.75 # Replace this with actual processing if needed

# Send dummy value to ESP32
print(f"Sending dummy value: {dummy_value}")
ser.write(f'{dummy_value:.2f}\n'.encode('utf-8'))
else:
time.sleep(1) # Sleep for 1 second before checking again

Steps to Implement

  1. Setup the ESP32
  • Install the ESP32 board in the Arduino IDE.
  • Upload the provided ESP32 code to the ESP32.
  • Ensure the GPIO16 and GPIO17 are connected to the RX and TX pins of the USB to TTL serial cable, respectively.

2. Setup the Raspberry Pi

  • Install Python 3 if not already installed.
  • Connect the USB to the TTL serial cable to the Raspberry Pi.
  • Ensure the serial port in the script (‘/dev/ttyUSB0’) matches the port to which the USB cable is connected.
  • Execute the Python script,
python3 your_script.py

3. Testing

  • Open the Serial Monitor in the Arduino IDE to observe the communication.
  • The ESP32 should send a signal(send ‘1’ from the serail monitor) to the Raspberry Pi to start processing.
  • The Raspberry Pi sends a dummy value back to the ESP32.
  • The dummy value is displayed in the Serial Monitor.

Results

  • You need to send “1” in the serial monitor to get the response from the raspberry pi.

Conclusion

This project demonstrates how to establish UART communication between an ESP32 and a Raspberry Pi. By using ESP32 and the processing power of the Raspberry Pi, you can create IoT projects that involve real-time data processing and communication.

Contact us for any consultations or projects related to IoT.

Email: udara@protonest.co

Protonest for more details.

Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.

Our Website: https://www.protonest.co/

If you enjoyed this article and would like to show some support, consider buying me a coffee on Ko-Fi.

Your support not only helps me keep creating content like this but also fuels me! ☕️📚

Thank you for being an amazing reader!

Here’s my Ko-Fi link: https://ko-fi.com/udarakumarasena

Cheers!

--

--

Protonest IoT
Protonest IoT

Written by Protonest IoT

We make your IoT ideas a reality

No responses yet