2 I2C and 1 UART are not working simultaneously in ESP32
Posted: Mon Oct 25, 2021 1:48 pm
I have a project in which I have to monitor Acceleration of the subject, Location and Pulse + SpO2 of the subject. So, I've written this program to check data from MAX30100 Pulse Oxi sensor and MPU6050 Accelero and Gyroscope sensor using ESP32 -
This program works completely fine. No issues at all.
Now I added GPS, so that I can track the location too. And GPS is working along with accelerometer but Pulse oximeter sensor is not working - Output from that is 0 and the rest of the sensors are producing the expected outputs. Program for that is given below
Will you please help me with this. What's wrong with the program.
This program works completely fine. No issues at all.
Code: Select all
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "*********************";
// For Pulse sensor
PulseOximeter pox;
float BPM, SpO2;
uint32_t tsLastReport = 0;
// For Accelerometer sensor
const int MPU_addr=0x68;
int16_t accelX, accelY, accelZ, gyroX, gyroY, gyroZ;
float gForceX, gForceY, gForceZ,rotX, rotY, rotZ;
void onBeatDetected()
{
Serial.println("Beat Detected!");
}
void setup() {
Serial.begin(115200);
WiFi.begin("TP-Link", "**********");
// Wait for wifi to be connected
uint32_t notConnectedCounter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.println("Wifi connecting...");
notConnectedCounter++;
if(notConnectedCounter > 50) { // Reset board if not connected after 5s
Serial.println("Resetting due to Wifi not connecting...");
ESP.restart();
}
}
Serial.print("Wifi connected, IP address: ");
Serial.println(WiFi.localIP());
Blynk.begin(auth,"TP-Link_380B", "installation*500");
Serial.println("Initializing Pulse Oximeter..");
if (!pox.begin())
{
Serial.println("FAILED");
for(;;);
}
else
{
Serial.println("SUCCESS");
pox.setOnBeatDetectedCallback(onBeatDetected);
}
// The default current for the IR LED is 50mA and it could be changed by uncommenting the following line.
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
Wire1.begin(32,33,100000);
Wire1.beginTransmission(MPU_addr);
Wire1.write(0x6B); // PWR_MGMT_1 register
Wire1.write(0); // set to zero (wakes up the MPU-6050)
Wire1.endTransmission(true);
}
void loop()
{
// Reading data from Accelerometer sensor
Wire1.beginTransmission(MPU_addr);
Wire1.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire1.endTransmission(false);
Wire1.requestFrom(MPU_addr,14,true); // request a total of 14 registers
accelX=Wire1.read()<<8|Wire1.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
accelY=Wire1.read()<<8|Wire1.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
accelZ=Wire1.read()<<8|Wire1.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
gyroX=Wire1.read()<<8|Wire1.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
gyroY=Wire1.read()<<8|Wire1.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
gyroZ=Wire1.read()<<8|Wire1.read();
gForceX = (accelX-2050) / 16384.0;
gForceY = (accelY-77) / 16384.0;
gForceZ = (accelZ-1947) / 16384.0;
rotX = (gyroX+270) / 131.0;
rotY = (gyroY-351) / 131.0;
rotZ = (gyroZ+136) / 131.0;
// calculating Amplitute vactor for 3 axis
float Raw_Amp = pow(pow(gForceX,2)+pow(gForceY,2)+pow(gForceZ,2),0.5);
int Amp = Raw_Amp * 10; // Mulitiplied by 10 bcz values are between 0 to 1
int angleChange = pow(pow(rotX,2)+pow(rotY,2)+pow(rotZ,2),0.5);
// Reading data from pulseoximeter sensor
pox.update();
BPM = pox.getHeartRate();
SpO2 = pox.getSpO2();
// Uploading data to Blynk server AND printing data on serial monitor
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
Blynk.run();
Blynk.virtualWrite(V3, BPM);
Blynk.virtualWrite(V4, SpO2);
Serial.println(Amp);
Serial.println(angleChange);
Serial.print("Heart rate:");
Serial.print(BPM);
Serial.print(" bpm / SpO2:");
Serial.print(SpO2);
Serial.println(" %");
tsLastReport = millis();
}
}
Code: Select all
#include <TinyGPSPlus.h>
#include <HardwareSerial.h>
#include <Arduino.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char auth[] = "VUjbOe139IPotsyC2Sjx-MQbB54al9s0";
// For GPS sensor
float latitude , longitude;
String latitude_string , longitiude_string;
TinyGPSPlus gps;
WidgetMap myMap(V0);
HardwareSerial SerialGPS(2);
// For Pulse sensor
PulseOximeter pox;
float BPM, SpO2;
uint32_t tsLastReport = 0;
// For Accelerometer sensor
const int MPU_addr=0x68;
int16_t accelX, accelY, accelZ, gyroX, gyroY, gyroZ;
float gForceX, gForceY, gForceZ,rotX, rotY, rotZ;
void onBeatDetected()
{
Serial.println("Beat Detected!");
}
void setup() {
Serial.begin(115200);
WiFi.begin("TP-Link", "***********");
// Wait for wifi to be connected
uint32_t notConnectedCounter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.println("Wifi connecting...");
notConnectedCounter++;
if(notConnectedCounter > 50) { // Reset board if not connected after 5s
Serial.println("Resetting due to Wifi not connecting...");
ESP.restart();
}
}
Serial.print("Wifi connected, IP address: ");
Serial.println(WiFi.localIP());
Blynk.begin(auth,"TP-Link_380B", "installation*500");
// Pulse oximeter setup
Serial.println("Initializing Pulse Oximeter..");
if (!pox.begin())
{
Serial.println("FAILED");
for(;;);
}
else
{
Serial.println("SUCCESS");
pox.setOnBeatDetectedCallback(onBeatDetected);
}
// The default current for the IR LED is 50mA and it could be changed by uncommenting the following line.
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
// Accelerometer setup
Wire1.begin(32,33,100000);
Wire1.beginTransmission(MPU_addr);
Wire1.write(0x6B); // PWR_MGMT_1 register
Wire1.write(0); // set to zero (wakes up the MPU-6050)
Wire1.endTransmission(true);
// GPS Setup
SerialGPS.begin(9600, SERIAL_8N1, 16, 17);
}
void loop()
{
// Reading data from Accelerometer sensor
Wire1.beginTransmission(MPU_addr);
Wire1.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire1.endTransmission(false);
Wire1.requestFrom(MPU_addr,14,true); // request a total of 14 registers
accelX=Wire1.read()<<8|Wire1.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
accelY=Wire1.read()<<8|Wire1.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
accelZ=Wire1.read()<<8|Wire1.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
gyroX=Wire1.read()<<8|Wire1.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
gyroY=Wire1.read()<<8|Wire1.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
gyroZ=Wire1.read()<<8|Wire1.read();
gForceX = (accelX-2050) / 16384.0;
gForceY = (accelY-77) / 16384.0;
gForceZ = (accelZ-1947) / 16384.0;
rotX = (gyroX+270) / 131.0;
rotY = (gyroY-351) / 131.0;
rotZ = (gyroZ+136) / 131.0;
// calculating Amplitute vactor for 3 axis
float Raw_Amp = pow(pow(gForceX,2)+pow(gForceY,2)+pow(gForceZ,2),0.5);
int Amp = Raw_Amp * 10; // Mulitiplied by 10 bcz values are between 0 to 1
int angleChange = pow(pow(rotX,2)+pow(rotY,2)+pow(rotZ,2),0.5);
// Reading data from pulseoximeter sensor
pox.update();
BPM = pox.getHeartRate();
SpO2 = pox.getSpO2();
// Reading the data from GPS
if(SerialGPS.available() > 0) {
if (gps.encode(SerialGPS.read()))
{
if (gps.location.isValid())
{
latitude = gps.location.lat();
latitude_string = String(latitude , 6);
longitude = gps.location.lng();
longitiude_string = String(longitude , 6);
}
}
}
// Uploading data to Blynk server AND printing data on serial monitor
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
Blynk.run();
Blynk.virtualWrite(V3, BPM);
Blynk.virtualWrite(V4, SpO2);
Serial.println(Amp);
Serial.println(angleChange);
Serial.print("Heart rate:");
Serial.print(BPM);
Serial.print(" bpm / SpO2:");
Serial.print(SpO2);
Serial.println(" %");
Blynk.virtualWrite(V0, 1, latitude, longitude, "Location");
Serial.print("Latitude = ");
Serial.println(latitude_string);
Serial.print("Longitude = ");
Serial.println(longitiude_string);
tsLastReport = millis();
}
}