Issue: The issue I'm facing is that the ECG sensor readings are consistently showing 0, and the data sent to Firebase is not accurate. I have already checked the connections, and they appear to be correct.
Additional Information:
I have verified that the ECG sensor is receiving the correct power supply voltage.
I have also checked the lead-off detection pins (leadOffPin1 and leadOffPin2), and they seem to be functioning correctly.
The Wi-Fi connection to Firebase is established successfully.
Specific Questions:
What could be the possible reasons for the ECG sensor not providing valid readings, and how can I troubleshoot this issue?
How can I ensure that the data sent to Firebase accurately represents the ECG sensor readings?
Are there any common pitfalls or best practices when integrating ECG sensors with ESP32 and Firebase that I should be aware of?
here half code
#include <WiFi.h>
#include <FirebaseESP32.h>
#include <Wire.h>
#include <Arduino.h>
const int sensorPin = 14;
const int leadOffPin1 = 22;
const int leadOffPin2 = 23;
const char* ssid = "PROFESSOR";
const char* password = "star2star";
FirebaseConfig firebaseConfig;
FirebaseAuth firebaseAuth;
FirebaseData firebaseData;
#define REPORTING_PERIOD_MS 1000
#define Buzzer 25
uint32_t tsLastReport = 0;
volatile int ecgValue = 0;
void sensorTask(void *pvParameters) {
for (;;) {
// Check if either of the lead-off detection pins are indicating a lead-off condition
if (digitalRead(leadOffPin1) == 1 || digitalRead(leadOffPin2) == 1) {
Serial.println("0"); // If lead-off is detected, print "0" to the Serial Plotter
delay(500); // Wait for a while before the next check
} else {
// Read the ECG sensor value
int ecgValue = analogRead(sensorPin);
Serial.println(ecgValue); // Print the ECG sensor value to the Serial Plotter
delay(400); // Delay for stability and to match the rate of lead-off checks
}
// Check if it's time to send data to Firebase based on a time interval
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
// Send the ECG sensor value to Firebase under the path "/ECG/ECGVALUES"
Firebase.setInt(firebaseData, "/ECG/ECGVALUES", ecgValue);
Serial.println("ecg sent"); // Print a message indicating that the ECG data was sent
}
delay(10); // Small delay to control the loop speed
}
}
void setup() {
Serial.begin(9600); // Initialize serial communication with a baud rate of 9600.
pinMode(leadOffPin1, INPUT); // Set leadOffPin1 as an input pin.
pinMode(leadOffPin2, INPUT); // Set leadOffPin2 as an input pin.
WiFi.begin(ssid, password); // Connect to the Wi-Fi network with the specified SSID and password.
while (WiFi.status() != WL_CONNECTED) {
delay(500); // Wait for the Wi-Fi connection to establish. Checks every 500 milliseconds.
}
firebaseConfig.host = "ckd-web-interface-default-rtdb.firebaseio.com"; // Set the Firebase host URL.
firebaseConfig.api_key = "AIzaSyAhelk_39kII2gWBtqYZvf0BLbpb6VLkaE"; // Set the Firebase API key.
firebaseAuth.user.email = "firebase12@gmail.com"; // Set the Firebase authentication email.
firebaseAuth.user.password = "1234567"; // Set the Firebase authentication password.
Firebase.begin(&firebaseConfig, &firebaseAuth); // Initialize the Firebase connection.
Firebase.reconnectNetwork(true); // Reconnect to the Firebase network if the connection is lost.
xTaskCreatePinnedToCore(sensorTask, "SensorTask", 10000, NULL, 1, NULL, 0); // Create a separate task named "SensorTask" with specified parameters.
}
Your code reads analog values from GPIO14 while also using WiFi.
GPIO14 is on ADC2, which is not usable while WiFi is active.
From the ESP32's GPIO documentation:
ADC2: ADC2 pins cannot be used when Wi-Fi is used. So, if you are having trouble getting the value from an ADC2 GPIO while using Wi-Fi, you may consider using an ADC1 GPIO instead, which should solve your problem.
ADC2 pins are 0, 2, 4, 12, 13, 14, 15, 25, 26 and 27. Don't try to read analog values from these pins while using WiFi. ADC1 pins are 32 thru 39. Use one of these pins instead.
You asked how to troubleshoot this issue: it's reasonable to expect that you'd be able to read an analog value from an ADC2 pin at any time, but when you find you're getting unexpected values, you could try wiring the pin to a known value and seeing if that works. If it doesn't, you know that analogRead()
isn't working as expected.
Also reading the documentation is a great start.