Hello all, I am trying to combine two MAX30100 and MLX90614 sensors on an Arduino Uno. I am using the Wire library and connected the sensors as described in the documentation. However, I am not able to read data from both sensors. Here the code:
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <Adafruit_MLX90614.h>
#define REPORTING_PERIOD_MS 1000
PulseOximeter pox;
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
uint32_t tsLastReport = 0;
// Callback (registered below) fired when a pulse is detected
void onBeatDetected() {
Serial.println("Beat!");
}
void setup() {
Serial.begin(115200);
Wire.begin();
mlx.begin();
Wire.setClock(100000);
// Initialize the pulse oximeter.
if (!pox.begin()) {
Serial.println("Failed to initialize pulse oximeter!");
for (;;);
}
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void loop() {
// Update the pulse oximeter.
printTemp();
printFreq();
}
void printTemp(){
Serial.print("Ambient = "); Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = "); Serial.print(mlx.readObjectTempC()); Serial.println("*C");
}
void printFreq(){
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
tsLastReport = millis();
}
}
Run the I2C scanner program and let us know what you find.
I have run the I2C scanner program and found the following devices:
Device found at address: 0x57
Device found at address: 0x5A
What should I do next?
Try putting that in loop, it appears doing the printTemp(); almost constantly.
Those two addresses should be the sensors, the test proves they are wired correctly and your problem is more then likely in the software.
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <Adafruit_MLX90614.h>
#define REPORTING_PERIOD_MS 1000
PulseOximeter pox;
Adafruit_MLX90614 mlx;
uint32_t tsLastReport = 0;
// Callback (registered below) fired when a pulse is detected
void onBeatDetected() {
Serial.println("Beat!");
}
void setup() {
Serial.begin(115200);
Wire.begin();
mlx.begin();
Wire.setClock(100000);
// Initialize the pulse oximeter.
if (!pox.begin()) {
Serial.println("Failed to initialize pulse oximeter!");
for (;;);
}
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void loop() {
// Update the pulse oximeter.
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
tsLastReport = millis();
}
// Print temperature data
Serial.print("Ambient = ");
Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = ");
Serial.print(mlx.readObjectTempC());
Serial.println("*C");
}
I have made the changes you suggested, but I am still not getting the desired results. Where should I fix the error?
Ambient = nanC Object = nan C
Heart rate:0.00bpm / SpO2:0%
Try removing the } else {, you are doing one or the other, you want to do both only when it is timed out.
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
// Print temperature data
Serial.print("Ambient = ");
Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = ");
Serial.print(mlx.readObjectTempC());
Serial.println("*C");
tsLastReport = millis();
}
I followed your instructions and removed the else
statement, but the code is still not working. What else can I try?
for debugging replace this with a delay(4000); you should get results.
loop{ delay() code} that will allow you to check each block of code. If it works the problem is in your if statement.
void loop() {
// Update the pulse oximeter.
pox.update();
delay(4000);
Serial.print("Heart rate:");
Serial.print(pox.getHeartRate());
Serial.print("bpm / SpO2:");
Serial.print(pox.getSpO2());
Serial.println("%");
// Print temperature data
Serial.print("Ambient = ");
Serial.print(mlx.readAmbientTempC());
Serial.print("*C\tObject = ");
Serial.print(mlx.readObjectTempC());
Serial.println("*C");
tsLastReport = millis();
}
It's not working, I'm not sure if I followed the instructions correctly.
system
Closed
April 12, 2024, 7:43am
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.