Hello,
I'm trying to connect three I2C Sensors, and for now I've been only able to connect and read one device ( MLX90614 ). Both the MAX30105 and the MAX30205 are being troublesome.
I've already tried for example the Protocentral and the ClosedCube libraries for the MAX30205 but both aren't working.
I've also tried using only one sensor at a time, but that also doesn't get me any further.
I'm using an ESP32 from Waveshare (https://www.waveshare.com/esp32-s3-lcd-1.28.htm) and the SDA / SCL GPIO are 6, 7 respectively.
#include <Wire.h> //I2C Header
//HeartRate Includes
#include "MAX30105.h"
#include "heartRate.h"
#include "spo2_algorithm.h"
//#include "MAX30102_PulseOximeter.h"
#include <Adafruit_MLX90614.h> //IR Temperature
#include "Protocentral_MAX30205.h" //Touch Temperature
//#include "ClosedCube_MAX30205.h"
void TemperatureSetup();
void TemperatureRead();
void IRTemperatureSetup();
void IRTemperatureRead();
void HeartRateSetup();
void HeartRateLoop();
//Temperature Variables
Adafruit_MLX90614 temperatureIRSensor = Adafruit_MLX90614();
MAX30205 tempSensor;
//ClosedCube_MAX30205 max30205;
//HeartRate Variables
MAX30105 heartRateSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
void setup()
{
Serial.begin( 115200 ); /* prepare for possible serial debug */
Wire.setPins(6, 7);
HeartRateSetup();
BatterySetup();
BatteryLevelImage();
IRTemperatureSetup();
TemperatureSetup();
Serial.println( "Setup done" );
}
void loop()
{
unsigned long currentMillis = millis();
/* The Arduino executes this part of the code once every second
* (interval = 1000 (ms) = 1 second).
*/
if(currentMillis - previousMillis > interval)
{
BatteryLevelRead();
IRTemperatureRead();
TemperatureRead();
// Don't forget to update the previousMillis value
previousMillis = currentMillis;
}
HeartRateLoop();
}
void TemperatureSetup() {
//max30205.begin(0x48);
while(!tempSensor.scanAvailableSensors()){
Serial.println("Couldn't find the temperature sensor, please connect the sensor." );
}
tempSensor.begin(); // set continuos mode, active mode*/
}
void TemperatureRead() {
/*Serial.print("T=");
Serial.print(max30205.readTemperature());
Serial.println("C");*/
float temp = tempSensor.getTemperature(); // read temperature for every 100ms
Serial.print(temp ,2);
Serial.println("'c" );
}
void IRTemperatureSetup() {
if (!temperatureIRSensor.begin()) {
Serial.println("Error connecting to MLX sensor. Check wiring.");
while (1);
};
}
void IRTemperatureRead() {
Serial.print("Ambient = "); Serial.print(temperatureIRSensor.readAmbientTempC());
Serial.print("*C\tObject = "); Serial.print(temperatureIRSensor.readObjectTempC()); Serial.println("*C");
Serial.print("Ambient = "); Serial.print(temperatureIRSensor.readAmbientTempF());
Serial.print("*F\tObject = "); Serial.print(temperatureIRSensor.readObjectTempF()); Serial.println("*F");
}
void HeartRateSetup() {
// Initialize sensor
if (!heartRateSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("Place your index finger on the sensor with steady pressure.");
heartRateSensor.setup(); //Configure sensor with default settings
}
void HeartRateLoop()
{
long irValue = heartRateSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a beat!
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.print(beatAvg);
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
snprintf(heartRateText, sizeof(heartRateText), "#FFFFFF BPM: %d", beatAvg);
lv_label_set_text(heartRateLabel, heartRateText);
}
ClosedCube Library Error:
[E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
256.00CSparkFun MAX30100x Library Error:
Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
MAX30105 was not found. Please check wiring/power.ProtoCentral Error:
Couldn't find the temperature sensor, please connect the sensor.
Thank you