Skip to content

Commit

Permalink
Added chip temperature reading, applied potential bug fix, confined I…
Browse files Browse the repository at this point in the history
…2C code to max30102.cpp.

"Parasetamol" suggested adding the MAX30102 die temperature reading. Why not, there is a register call for it, but keep it mind such a reading has _nothing_ to do with body temperature! Next, "williamesp2015" has raised an issue of initializing I2C twice: one in the main routine and one in max30102.cpp. Indeed, it may cause problems when custom Wire settings are defined. Thus, I have eliminated Wire call from the main routine. In addition, following "williamesp2015" I have changed the Wire clock speed to 400k.
  • Loading branch information
aromring committed Oct 10, 2021
1 parent 22b89c1 commit f6160ab
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
32 changes: 18 additions & 14 deletions RD117_ARDUINO.ino
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@
* ------------------------------------------------------------------------- */

#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include "algorithm_by_RF.h"
#include "max30102.h"

//#define DEBUG // Uncomment for debug output to the Serial stream
#define USE_ADALOGGER // Comment out if you don't have ADALOGGER itself but your MCU still can handle this code
//#define USE_ADALOGGER // Comment out if you don't have ADALOGGER itself but your MCU still can handle this code
//#define TEST_MAXIM_ALGORITHM // Uncomment if you want to include results returned by the original MAXIM algorithm
//#define SAVE_RAW_DATA // Uncomment if you want raw data coming out of the sensor saved to SD card. Red signal first, IR second.

Expand Down Expand Up @@ -82,17 +81,11 @@ void setup() {
digitalWrite(sdIndicatorPin,LOW);
#endif

Wire.begin();

#if defined(DEBUG) || !defined(USE_ADALOGGER)
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
#endif

maxim_max30102_reset(); //resets the MAX30102
delay(1000);

maxim_max30102_read_reg(REG_INTR_STATUS_1,&uch_dummy); //Reads/clears the interrupt status register
maxim_max30102_init(); //initialize the MAX30102
old_n_spo2=0.0;

Expand Down Expand Up @@ -150,9 +143,9 @@ void setup() {
dataFile.println(measuredvbat);
dataFile.println(my_status);
#ifdef TEST_MAXIM_ALGORITHM
dataFile.print(F("Time[s]\tSpO2\tHR\tSpO2_MX\tHR_MX\tClock\tRatio\tCorr"));
dataFile.print(F("Time[s]\tSpO2\tHR\tSpO2_MX\tHR_MX\tClock\tRatio\tCorr\tTemp[C]”));
#else // TEST_MAXIM_ALGORITHM
dataFile.print(F("Time[s]\tSpO2\tHR\tClock\tRatio\tCorr"));
dataFile.print(F("Time[s]\tSpO2\tHR\tClock\tRatio\tCorr\tTemp[C]"));
#endif // TEST_MAXIM_ALGORITHM
#ifdef SAVE_RAW_DATA
int32_t i;
Expand All @@ -178,9 +171,9 @@ void setup() {
}
uch_dummy=Serial.read();
#ifdef TEST_MAXIM_ALGORITHM
Serial.print(F("Time[s]\tSpO2\tHR\tSpO2_MX\tHR_MX\tClock\tRatio\tCorr"));
Serial.print(F("Time[s]\tSpO2\tHR\tSpO2_MX\tHR_MX\tClock\tRatio\tCorr\tTemp[C]"));
#else // TEST_MAXIM_ALGORITHM
Serial.print(F("Time[s]\tSpO2\tHR\tClock\tRatio\tCorr"));
Serial.print(F("Time[s]\tSpO2\tHR\tClock\tRatio\tCorr\tTemp[C]"));
#endif // TEST_MAXIM_ALGORITHM
#ifdef SAVE_RAW_DATA
int32_t i;
Expand Down Expand Up @@ -233,6 +226,12 @@ void loop() {
millis_to_hours(elapsedTime,hr_str); // Time in hh:mm:ss format
elapsedTime/=1000; // Time in seconds
// Read the _chip_ temperature in degrees Celsius
int8_t integer_temperature;
uint8_t fractional_temperature;
maxim_max30102_read_temperature(&integer_temperature, &fractional_temperature);
float temperature = integer_temperature + ((float)fractional_temperature)/16.0;
#ifdef DEBUG
Serial.println("--RF--");
Serial.print(elapsedTime);
Expand All @@ -241,7 +240,9 @@ void loop() {
Serial.print("\t");
Serial.print(n_heart_rate, DEC);
Serial.print("\t");
Serial.println(hr_str);
Serial.print(hr_str);
Serial.print("\t");
Serial.println(temperature);
Serial.println("------");
#endif // DEBUG
Expand Down Expand Up @@ -290,6 +291,8 @@ void loop() {
dataFile.print(ratio);
dataFile.print("\t");
dataFile.print(correl);
dataFile.print("\t");
dataFile.print(temperature);
#ifdef SAVE_RAW_DATA
// Save raw data for unusual O2 levels
for(i=0;i<BUFFER_SIZE;++i)
Expand Down Expand Up @@ -331,6 +334,8 @@ void loop() {
Serial.print(ratio);
Serial.print("\t");
Serial.print(correl);
Serial.print("\t");
Serial.print(temperature);
#ifdef SAVE_RAW_DATA
// Save raw data for unusual O2 levels
for(i=0;i<BUFFER_SIZE;++i)
Expand Down Expand Up @@ -393,4 +398,3 @@ void blinkLED(const byte led, bool isOK)
}
}
#endif

Empty file modified algorithm.cpp
100644 → 100755
Empty file.
Empty file modified algorithm.h
100644 → 100755
Empty file.
Empty file modified algorithm_by_RF.cpp
100644 → 100755
Empty file.
Empty file modified algorithm_by_RF.h
100644 → 100755
Empty file.
19 changes: 19 additions & 0 deletions max30102.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ bool maxim_max30102_init()
*/
{
Wire.begin();
Wire.setClock(400000L);

maxim_max30102_reset(); //resets the MAX30102
delay(1000);

uint8_t uch_dummy;
maxim_max30102_read_reg(REG_INTR_STATUS_1,&uch_dummy); //Reads/clears the interrupt status register

if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)) // INTR setting
return false;
if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x00))
Expand Down Expand Up @@ -211,3 +219,14 @@ bool maxim_max30102_reset()
return true;
}

bool maxim_max30102_read_temperature(int8_t *integer_part, uint8_t *fractional_part)
{
maxim_max30102_write_reg(REG_TEMP_CONFIG,0x1); // Enabling TEMP_EN
delayMicroseconds(1); // Let the processor do its work
// For proper conversion, read the integer part as uint8_t
uint8_t temp;
maxim_max30102_read_reg(REG_TEMP_INTR, &temp); // 2's complement integer part of the temperature in degrees Celsius
*integer_part = temp;
maxim_max30102_read_reg(REG_TEMP_FRAC, fractional_part); // Fractional part of the temperature in 1/16-th degree Celsius
return true;
}
1 change: 1 addition & 0 deletions max30102.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,5 @@ bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led);
bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data);
bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data);
bool maxim_max30102_reset(void);
bool maxim_max30102_read_temperature(int8_t *integer_part, uint8_t *fractional_part);
#endif /* MAX30102_H_ */

0 comments on commit f6160ab

Please sign in to comment.