One way serial communication between esp32 and arduino uno error

Hi everyone!

I'm making a meditation device that uses an infrared obstacle sensor to detect an object along with a real time clock module to communicate to blynk how long the object is detected for (connected to my esp32). I want to use one way serial communication between my esp32 and arduino uno to turn on my LED circular board to complete a light whilst the object is detected, but i keep getting this error when uploading my code to the esp32?:

R��Sp���J�����ܜ�Đ��H����Guru Meditation Error: Core 1 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d1680: 74ccc664 76cd66dd 444454c6
Core 1 register dump:
*PC : 0x400d1687 PS : 0x00060330 A0 : 0x800d515d A1 : 0x3ffb1f70 *
*A2 : 0x3ffc1b9c A3 : 0x00002580 A4 : 0x3ffc18cc A5 : 0x00004e20 *
*A6 : 0x00000003 A7 : 0x00060a23 A8 : 0x800d07a0 A9 : 0x3ffb1f30 *
*A10 : 0x3ffc18cc A11 : 0xffffffff A12 : 0xffffffff A13 : 0x00000000 *
*A14 : 0x00000009 A15 : 0x00000000 SAR : 0x00000016 EXCCAUSE: 0x00000000 *
*EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000 *

ELF file SHA256: 0000000000000000

Backtrace: 0x400d1687:0x3ffb1f70 0x400d515a:0x3ffb1fb0 0x4008a02e:0x3ffb1fd0

this is the 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); // 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.println("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.println("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 this is the code for my arduino uno:

#include <Adafruit_NeoPixel.h>
#define UNO_RX_PIN 0
#define PIXEL_PIN 6
#define PIXEL_COUNT 24
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool ledCycleActive = false;

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

void loop() {
  if (Serial.available()) {
    String command = Serial.readStringUntil('\n');
    if (command == "ON") {
      ledCycleActive = true;
    } else if (command == "OFF") {
      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);
  }
}

I'm also finding that the serial communication is not working at all. I was advised by my tutor that i can just connect the TX pin of the esp32 to the RX pin of the arduino uno and it will work, but that has not been my experience at all (also connected ground of both boards together) :frowning:

I'm not too sure where I'm going wrong as I am completely new and have had very little guidance from tutors :frowning: Pls help

You are the correct one! You MUST have a complete circuit between the two devices and the ground of both must be connected to make that complete circuit.

i

trying commenting out the above line

i don't know about using a 2nd serial interface on an esp32

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

https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

''GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-up or pull-down resistors. They can’t be used as outputs, so use these pins only as inputs''

1 Like

that is most definitely the issue. Serial1 has as default pins 9 & 10, those pins are also in use for the flash (on nearly all boards) you can though define your own pins on an ESP32
so even

Serial1.begin(9600, SERIAL_8N1, RXD2, TXD2);  // basically any I/O can be used

for instance pins 12 & 14 are known to work properly

1 Like

Maybe, if you only hook up TX from ESP to RX on Uno..
Remember ESP32 is 3.3v and Uno is 5V..

The error is probably Serial1 did not begin as you asked it too..
Their are many different ESP32 boards..
Serial1 i think was like pins 16 and 17 be default but can be reassigned..

Looking at the sketches, you could also just use 1 pin on the ESP32 as an output, make it high to turn on and low to turn off..
Then on Uno, make it an input and just look for low and high..

good luck.. ~q

1 Like

ahhhhh thank you so much it doesnt have the error anymore but the serial communication isnt working

Be careful, println() sends a carriage return and a newline after the text. You are reading until the newline, so the carriage return will be part of the received text.

how should it be adjusted? should i be taking out println()?

Either use print() and send the newline character as either part of the text, or in a subsequent print(), or use "OFF\r" for the comparison on the UNO instead of "OFF".

so would it be link this for the esp32

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

and like this for the uno

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

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