Title: Vital Care
By: Md. Kamrul Hussain
Project Category: Design Challenge
Project Name: Design For A Cause 2021
Blog post: 04
Previous Blogs:
Blog No. | Title |
|---|---|
| 01 | Introduction |
| 02.a | SpO2 basics [PPG] |
| 02.b | SpO2 [MAX30102] |
The MAX30102 has an on-chip temperature sensor for
calibrating the temperature dependence of the SpO2 subsystem.
The temperature sensor has an inherent resolution
of 0.0625°C.
I would like to use it for body temperature detection. But it seemed to be slow. Temperature rises up to around 34'C but it does not rise up or get down rapidly. However, that will be fine as the body temperature does not change too fast.
Temperature Data (0x1F–0x21)
Temperature Integer
The on-board temperature ADC output is split into two registers, one to store the integer temperature and one to store the fraction. Both should be read when reading the temperature data, and the equation below shows how to add the two registers together:
TMEASURED = TINTEGER + TFRACTION
This register stores the integer temperature data in 2’s complement format, where each bit corresponds to 1°C.
Temperature Fraction
This register stores the fractional temperature data in increments of 0.0625°C.
Output:
| {gallery} Max30102 Temperature |
|---|
Serial Terminal: MAX30102 gives RED and IR PPG and temperature value. NANO 33 IOT calculates the Heart Rate and SpO2. serial terminal shows these output along with Temperature. |
Plotting output: Heart Rate, SpO2 and Temperature are plotted together for the time being. |
#include <Wire.h>
#include "MAX30105.h"
#include "spo2_algorithm.h"
MAX30105 particleSensor;
#define MAX_BRIGHTNESS 255
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
//Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
//To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
uint16_t irBuffer[50]; //infrared LED sensor data
uint16_t redBuffer[50]; //red LED sensor data
#else
uint32_t irBuffer[50]; //infrared LED sensor data
uint32_t redBuffer[50]; //red LED sensor data
#endif
int32_t bufferLength; //data length
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
byte pulseLED = 9; //Must be on PWM pin
byte readLED = 13; //Blinks with each data read
void setup()
{
Serial.begin(115200); // initialize serial communication at 115200 bits per second:
while (!Serial);
pinMode(pulseLED, OUTPUT);
pinMode(readLED, OUTPUT);
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println(F("MAX30105 was not found. Please check wiring/power."));
while (1);
}
Serial.println(F("Attach sensor to finger with rubber band. Press any key to start conversion"));
while (Serial.available() == 0) ; //wait until user presses a key
Serial.read();
float temperature;
byte ledBrightness = 27; //Options: 0=Off to 255=50mA
byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
particleSensor.enableDIETEMPRDY(); //Enable the temp ready interrupt. This is required.
}
void loop()
{
bufferLength = 50; //buffer length of 50 stores 2 seconds of samples running at 25sps
//read the first 100 samples, and determine the signal range
for (byte i = 0 ; i < bufferLength ; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
redBuffer[i] = particleSensor.getRed();
irBuffer[i] = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
Serial.print(F("red="));
Serial.print(redBuffer[i], DEC);
Serial.print(F(", ir="));
Serial.println(irBuffer[i], DEC);
}
//calculate heart rate and SpO2 after first 50 samples (first 2 seconds of samples)
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
//Continuously taking samples from MAX30102. Heart rate and SpO2 are calculated every 1 second
while (1)
{
//dumping the first 15 sets of samples in the memory and shift the last 15 sets of samples to the top
for (byte i = 15; i < 50; i++)
{
redBuffer[i - 15] = redBuffer[i];
irBuffer[i - 15] = irBuffer[i];
}
heartRate = heartRate * 0.5;
//spo2 = spo2 * 1000;
//take 15 sets of samples before calculating the heart rate.
for (byte i = 35; i < 50; i++)
{
while (particleSensor.available() == false) //do we have new data?
particleSensor.check(); //Check the sensor for new data
digitalWrite(readLED, !digitalRead(readLED)); //Blink onboard LED with every data read
redBuffer[i] = particleSensor.getRed();
irBuffer[i] = particleSensor.getIR();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
//send samples and calculation result to terminal program through UART
Serial.print(F("RED= "));
Serial.print(redBuffer[i], DEC);
Serial.print('\t');
Serial.print(F("IR= "));
Serial.print(irBuffer[i], DEC);
Serial.print('\t');
Serial.print(F("HR= "));
Serial.print(heartRate, DEC);
Serial.print('\t');
//Serial.print(F(", HRvalid="));
//Serial.print(validHeartRate, DEC);
Serial.print(F("SPO2= "));
Serial.print(spo2, DEC);
Serial.print('\t');
//Serial.print(F(", SPO2Valid="));
//Serial.print(validSPO2, DEC);
float temperature = particleSensor.readTemperature();
Serial.print(F("temperatureC= "));
Serial.println(temperature, 4);
}
//After gathering 15 new samples recalculate HR and SP02
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &spo2, &validSPO2, &heartRate, &validHeartRate);
}
}




