I am using the MAX30100 and Sparkfun TMP102 sensors with a Nodemcu esp8266. I want to run both sensors at the same time, so I have merged the two example codes for those two sensors. There is an issue with the program though, and I am unsure how to fix it. The individual example codes work fine, but there is an error with my merged code. How can I fix the merged code I've created?
Here are the connections for the sensors to the Nodemcu:
TMP102:
3.7v ==> 3v3
Gnd ==> Gnd
SDA ==> D2
SCL ==> D1
ALT ==> D0
MAX30100:
VIN ==> 3v3
Gnd ==> Gnd
SDA ==> D2
SCL ==> D1
Pictures of circuit
Sparkfun TMP102 Example1 code
#include <Wire.h> // Used to establied serial communication on the I2C bus
#include <SparkFunTMP102.h> // Used to send and recieve specific information from our sensor
// Connections
// VCC = 3.3V
// GND = GND
// SDA = A4
// SCL = A5
const int ALERT_PIN = D0;
TMP102 sensor0;
// Sensor address can be changed with an external jumper to:
// ADD0 - Address
// VCC - 0x49
// SDA - 0x4A
// SCL - 0x4B
void setup() {
Serial.begin(115200);
Wire.begin(); //Join I2C Bus
pinMode(ALERT_PIN,INPUT); // Declare alertPin as an input
/* The TMP102 uses the default settings with the address 0x48 using Wire.
Optionally, if the address jumpers are modified, or using a different I2C bus,
these parameters can be changed here. E.g. sensor0.begin(0x49,Wire1)
It will return true on success or false on failure to communicate. */
if(!sensor0.begin())
{
Serial.println("Cannot connect to TMP102.");
Serial.println("Is the board connected? Is the device ID correct?");
while(1);
}
Serial.println("Connected to TMP102!");
delay(100);
// Initialize sensor0 settings
// These settings are saved in the sensor, even if it loses power
// set the number of consecutive faults before triggering alarm.
// 0-3: 0:1 fault, 1:2 faults, 2:4 faults, 3:6 faults.
sensor0.setFault(0); // Trigger alarm immediately
// set the polarity of the Alarm. (0:Active LOW, 1:Active HIGH).
sensor0.setAlertPolarity(1); // Active HIGH
// set the sensor in Comparator Mode (0) or Interrupt Mode (1).
sensor0.setAlertMode(0); // Comparator Mode.
// set the Conversion Rate (how quickly the sensor gets a new reading)
//0-3: 0:0.25Hz, 1:1Hz, 2:4Hz, 3:8Hz
sensor0.setConversionRate(2);
//set Extended Mode.
//0:12-bit Temperature(-55C to +128C) 1:13-bit Temperature(-55C to +150C)
sensor0.setExtendedMode(0);
//set T_HIGH, the upper limit to trigger the alert on
sensor0.setHighTempF(85.0); // set T_HIGH in F
//sensor0.setHighTempC(29.4); // set T_HIGH in C
//set T_LOW, the lower limit to shut turn off the alert
sensor0.setLowTempF(84.0); // set T_LOW in F
//sensor0.setLowTempC(26.67); // set T_LOW in C
}
void loop()
{
float temperature;
boolean alertPinState, alertRegisterState;
// Turn sensor on to start temperature measurement.
// Current consumtion typically ~10uA.
sensor0.wakeup();
// read temperature data
temperature = sensor0.readTempF();
//temperature = sensor0.readTempC();
// Check for Alert
alertPinState = digitalRead(ALERT_PIN); // read the Alert from pin
alertRegisterState = sensor0.alert(); // read the Alert from register
// Place sensor in sleep mode to save power.
// Current consumtion typically <0.5uA.
sensor0.sleep();
// Print temperature and alarm state
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("\tAlert Pin: ");
Serial.print(alertPinState);
Serial.print("\tAlert Register: ");
Serial.println(alertRegisterState);
delay(1000); // Wait 1000ms
}
MAX30100 Example code
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
// PulseOximeter is the higher level interface to the sensor
// it offers:
// * beat detection reporting
// * heart rate calculation
// * SpO2 (oxidation level) calculation
PulseOximeter pox;
uint32_t tsLastReport = 0;
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
Serial.println("B:1");
}
void setup()
{
Serial.begin(115200);
// Initialize the PulseOximeter instance and register a beat-detected callback
// The parameter passed to the begin() method changes the samples flow that
// the library spews to the serial.
// Options:
// * PULSEOXIMETER_DEBUGGINGMODE_PULSEDETECT : filtered samples and beat detection threshold
// * PULSEOXIMETER_DEBUGGINGMODE_RAW_VALUES : sampled values coming from the sensor, with no processing
// * PULSEOXIMETER_DEBUGGINGMODE_AC_VALUES : sampled values after the DC removal filter
// Initialize the PulseOximeter instance
// Failures are generally due to an improper I2C wiring, missing power supply
// or wrong target chip
if (!pox.begin(PULSEOXIMETER_DEBUGGINGMODE_PULSEDETECT)) {
Serial.println("ERROR: Failed to initialize pulse oximeter");
for(;;);
}
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void loop()
{
// Make sure to call update as fast as possible
pox.update();
// Asynchronously dump heart rate and oxidation levels to the serial
// For both, a value of 0 means "invalid"
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("H:");
Serial.println(pox.getHeartRate());
Serial.print("O:");
Serial.println(pox.getSpO2());
tsLastReport = millis();
}
}
My Merged Code
#include <Wire.h> // Used to establied serial communication on the I2C bus
#include <SparkFunTMP102.h> // Used to send and recieve specific information from our sensor
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
const int ALERT_PIN = D0;
TMP102 sensor0;
PulseOximeter pox
uint32_t tsLastReport = 0;
void onBeatDetected()
{
Serial.println("B:1");
}
void setup()
{
Serial.begin(115200);
Wire.begin(); //Join I2C Bus
pinMode(ALERT_PIN, INPUT);
if (!sensor0.begin())
{
Serial.println("Cannot connect to TMP102.");
Serial.println("Is the board connected? Is the device ID correct?");
while (1);
}
Serial.println("Connected to TMP102!");
delay(100);
// set the number of consecutive faults before triggering alarm.
// 0-3: 0:1 fault, 1:2 faults, 2:4 faults, 3:6 faults.
sensor0.setFault(0); // Trigger alarm immediately
// set the polarity of the Alarm. (0:Active LOW, 1:Active HIGH).
sensor0.setAlertPolarity(1); // Active HIGH
// set the sensor in Comparator Mode (0) or Interrupt Mode (1).
sensor0.setAlertMode(0); // Comparator Mode.
// set the Conversion Rate (how quickly the sensor gets a new reading)
//0-3: 0:0.25Hz, 1:1Hz, 2:4Hz, 3:8Hz
sensor0.setConversionRate(2);
//set Extended Mode.
//0:12-bit Temperature(-55C to +128C) 1:13-bit Temperature(-55C to +150C)
sensor0.setExtendedMode(0);
//set T_HIGH, the upper limit to trigger the alert on
sensor0.setHighTempF(85.0); // set T_HIGH in F
//sensor0.setHighTempC(29.4); // set T_HIGH in C
//set T_LOW, the lower limit to shut turn off the alert
sensor0.setLowTempF(84.0); // set T_LOW in F
//sensor0.setLowTempC(26.67); // set T_LOW in C
if (!pox.begin(PULSEOXIMETER_DEBUGGINGMODE_PULSEDETECT)) {
Serial.println("ERROR: Failed to initialize pulse oximeter");
for (;;);
}
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void loop()
{
float temperature;
boolean alertPinState, alertRegisterState;
// Turn sensor on to start temperature measurement.
// Current consumtion typically ~10uA.
sensor0.wakeup();
// read temperature data
temperature = sensor0.readTempF();
//temperature = sensor0.readTempC();
// Check for Alert
alertPinState = digitalRead(ALERT_PIN); // read the Alert from pin
alertRegisterState = sensor0.alert(); // read the Alert from register
// Place sensor in sleep mode to save power.
// Current consumtion typically <0.5uA.
sensor0.sleep();
// Print temperature and alarm state
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("\tAlert Pin: ");
Serial.print(alertPinState);
Serial.print("\tAlert Register: ");
Serial.println(alertRegisterState);
// Make sure to call update as fast as possible
pox.update();
// Asynchronously dump heart rate and oxidation levels to the serial
// For both, a value of 0 means "invalid"
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print("H:");
Serial.println(pox.getHeartRate());
Serial.print("O:");
Serial.println(pox.getSpO2());
tsLastReport = millis();
}
delay(1000); // Wait 1000ms
}