One way serial communication from esp32 to arduino uno

Hi everyone, I'm trying to turn on an led circuit connected to my arduino uno as it runs on 5v through my esp32 via one way serial communication. I have both pins connected to ground and the tx pin of the esp connected to the rx pin of the uno, but nothing seems to be communicating. I tested to see how the on/ off lines print when detected and its quite sporadic, doesnt line up with when an object is actually detected.

here is my code for my esp32:

#include <Wire.h>
#include <RTClib.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define BLYNK_TEMPLATE_ID           "--"
#define BLYNK_TEMPLATE_NAME         "-"
#define BLYNK_AUTH_TOKEN            "--"
#define ESP_TX_PIN 35

RTC_DS3231 rtc; // Initialize the RTC object
const int SensorPin = 4; // Digital input pin connected to the obstacle sensor
bool objectDetected = false;
unsigned long detectionStartTimeMillis = 0;
unsigned long detectionDurationSeconds = 0;
unsigned long detectionEndTimeMillis = 0;

char auth[] = "-"; // Blynk authentication token
char ssid[] = "-";
char pass[] = "-";

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600, SERIAL_8N1, ESP_TX_PIN); // Initialize Serial communication with Arduino Uno

  Wire.begin();
  rtc.begin();

  // Set the initial time of the RTC module (if required)
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
  pinMode(SensorPin, INPUT_PULLUP);

  // Connect to Wi-Fi
  WiFi.begin("-", "-");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("\nConnected to the WiFi network");
  Serial.print("Local ESP32 IP: ");
  Serial.println(WiFi.localIP());

  // Connect to Blynk
  Blynk.config(auth);
}

void loop() {
  Blynk.run();
  DateTime now = rtc.now();
  unsigned long currentTimeMillis = millis(); // Current time in milliseconds
  unsigned long currentTimeSeconds = now.unixtime(); // Current time in seconds
  
  // Check if an object is detected
  if (digitalRead(SensorPin) == LOW && !objectDetected) {
    objectDetected = true;
    Serial1.print("ON"); // Send command to Arduino Uno to turn on the LED function
    detectionStartTimeMillis = currentTimeMillis;
  }
  
  // Check if object detection has ended
  if (digitalRead(SensorPin) == HIGH && objectDetected) {
    objectDetected = false;
    Serial1.print("OFF"); // Send command to Arduino Uno to turn off the LED function
    detectionEndTimeMillis = currentTimeMillis;
    unsigned long detectionDurationMillis = detectionEndTimeMillis - detectionStartTimeMillis; // Duration in milliseconds
    unsigned long detectionDurationSeconds = detectionDurationMillis / 1000; // Convert milliseconds to seconds
     if (detectionDurationSeconds > 0) {
      Serial.print("Object detection ended. Duration: ");
      Serial.print(detectionDurationSeconds);
      Serial.println(" seconds.");
      Blynk.virtualWrite(V1, "\nCompleted meditation for " + String(detectionDurationSeconds) + " seconds"); // Send duration to Blynk app

    String strValue = String(detectionDurationSeconds); // Convert duration to string
      Serial.println("Sending: " + strValue);
      Serial1.println(strValue); // Send string to ESP32
      Blynk.virtualWrite(V2, detectionDurationSeconds); // Send duration to Blynk app
    }
  }
}

and here is the code for my uno:

#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>

#define UNO_RX_PIN 0
#define PIXEL_PIN 6
#define PIXEL_COUNT 24
#define ESP_TX_PIN 35

SoftwareSerial espSerial(UNO_RX_PIN, ESP_TX_PIN);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool ledCycleActive = false;

void setup() {
  pixels.begin();
  Serial.begin(9600);
  espSerial.begin(9600);
}

void loop() {
  if (espSerial.available()) {
    String command = espSerial.readStringUntil('\n');

    if (command == "ON\r") {
      ledCycleActive = true;
    } else if (command == "OFF\r") {
      ledCycleActive = false;
      // Turn off each pixel individually
      for (int i = pixels.numPixels() - 1; i >= 0; i--) {
        pixels.setPixelColor(i, 0, 0, 0, 0);
        pixels.show();
        delay(250);
      }
    }
  }

  if (ledCycleActive) {
    // Light up each pixel individually
    for (int i = 0; i < pixels.numPixels(); i++) {
      pixels.setPixelColor(i, pixels.Color(255, 200, 100));
      pixels.show();
      delay(166.67);
    }

    // Stay on for 2 seconds
    delay(2000);

    // Turn off each pixel individually
    for (int i = pixels.numPixels() - 1; i >= 0; i--) {
      pixels.setPixelColor(i, 0, 0, 0, 0);
      pixels.show();
      delay(250);
    }

    // Wait for 2 seconds before repeating the process
    delay(2000);
  }
}

everything else is working in my circuit, apart from the light cycle and serial communication to the uno from esp :frowning:

Post a schematic of how the 2 devices are connected and include the level shifters used so that the 5V Uno does not destroy the 3V3 ESP32.

As a note GPIO_NUM_35 on a ESP32 is input only.

the two boards are connected just using tx of the esp32 and rx of the uno and both share ground. I was told by my tutor that I wouldn't need a level shifter as previously i had a lot of issues having it wired to communicate to each other. So this way I can just have one way communication.

this is the datasheet of the esp32 I'm using

If not for the pin labelled tx, which one should I be using instead?

Sorry I don't know a lot I'm very new to this and have no background in coding

You will be better off using Serial2 with the TX pin of GPIO 17.

You may or may not need the level shifter. The Uno may not read the 3.3v signal as High.

For the Uno, do not put the software serial instance on the hardware serial pins.

just tried doing serial2 and it didnt work :frowning:

Did you modify the uno code software serial instance?

Should it be hardwareserial.h instead?

SoftwareSerial does not work with ESP32.
I showed you an example using Serial2 (UART) in another thread/subject.

One way serial communication between esp32 and arduino uno error - Using Arduino / Programming Questions - Arduino Forum

Hi, I also tried the code you showed but it doesnt work even with the correct baud rates and wiring. I get this error:
�����������ޜ� �ᆑ}�J�a$��0N�e0�i=

}���a!m5�/�

Connect pin 14 of the ESP32 to pin 10 of the Uno. Connect the grounds.

Use this sketch on the Uno

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  delay(1000);
  Serial.println("Ready to read ESP32");
  mySerial.begin(9600); 
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.print((char)mySerial.read());
  }
}

Use this sketch from @runaway_pancake on the ESP32

const byte RXD2 = 15;
const byte TXD2 = 14;
byte idx;

void setup() 
{
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);  // basically any I/O can be used
}

void loop() {
  for(idx = 0; idx < 100; idx ++)
  {
    Serial2.print("Hello ");
    Serial2.println(idx);
    delay(1000);
  }
}

You should see this output on the monitor from the Uno ide window.

20:13:26.020 -> Hello 0
20:13:27.043 -> Hello 1
20:13:28.019 -> Hello 2
20:13:29.037 -> Hello 3
20:13:30.059 -> Hello 4
20:13:31.029 -> Hello 5
20:13:32.051 -> Hello 6
20:13:33.025 -> Hello 7
20:13:34.048 -> Hello 8
20:13:35.021 -> Hello 9
20:13:36.043 -> Hello 10
20:13:37.029 -> Hello 11
20:13:38.056 -> Hello 12
20:13:39.030 -> Hello 13
20:13:40.053 -> Hello 14
20:13:41.030 -> Hello 15
20:13:42.055 -> Hello 16
20:13:43.031 -> Hello 17
20:13:44.055 -> Hello 18
20:13:45.028 -> Hello 19
20:13:46.047 -> Hello 20
20:13:47.017 -> Hello 21
20:13:48.036 -> Hello 22
20:13:49.055 -> Hello 23
20:13:50.026 -> Hello 24
20:13:51.048 -> Hello 25
20:13:52.022 -> Hello 26
20:13:53.046 -> Hello 27

Is it direct connection or through a level shifter?

My Uno and ESP32 work with a direct connection. No level shifter required.

If @sonnymart does not see output with the code, then he indeed needs a level shifter with his devices.

1 Like

Practically, the direct connection between ESP32 and UNO seems to work; but, the web opinion is opposite.

If you`re using UNO, try connecting TX to TX and RX to RX.

this code works!!! but just not when I apply it to my code thank you :slight_smile:

Then something other than the connections and the use of Serial2 on the ESP32 and software serial on the uno is the issue.

With the simple reading code on the uno, do you see the correct message being sent?

If No, then there is an issue with the esp code.

If Yes , then there is an issue with the Uno code and the way it treats the message.

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