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