ESP32驱动MAX30102脉搏血氧心率传感器MAX30102脉搏血氧心率传感器MAX30102模块引脚定义MAX30102模块与ESP32开发板引脚接线MAX30102 Arduino驱动程序获取心率数据获取血氧饱和度数值获取指尖温度MAX30102脉搏血氧心率传感器MAX30102一种光学传感器能够以无创方式检测血氧饱和度SpO2和心率。此外它还配备有温度传感器用于进行温度补偿和校准。MAX30102包含一个红色LED、一个红外LED以及一个光检测器。这些LED会向皮肤发出光线而光检测器则负责检测反射回来的光线。随着心脏的跳动血液流动状况的变化会导致反射光强度的改变。含氧血红蛋白会吸收更多的红外线脱氧血红蛋白会吸收更多的红光。根据红色和红外线波长下反射光强度的比值传感器便可计算出血氧饱和度SpO2值。而每次心跳时血液容量的变化则有助于我们测定心率。MAX30102模块引脚定义VIN供电3.3/5VGND供电负极SCLIIC通信时钟信号SDAIIC通信数据信号INT中断输出MAX30102模块与ESP32开发板引脚接线MAX30102ESP32开发板GNDGNDVIN5VSDA21SCL22MAX30102 Arduino驱动程序打开Arduino IDE库管理器安装SparkFun MAX3010x驱动库获取心率数据#includeWire.h#includeMAX30105.h#includeheartRate.hMAX30105sensor;// SettingsconstbyteRATE_SIZE5;// How many beats we averagebyte rates[RATE_SIZE];// Store last few valid BPM valuesbyte rateSpot0;byte validBeats0;long lastBeat0;float currentBPM0;int averageBPM0;unsigned long lastPrint0;// Only accept realistic heart rates (adjust for your case)constfloatMIN_BPM45;constfloatMAX_BPM180;voidsetup(){Serial.begin(115200);Serial.println(MAX30102 Heart Rate Sensor);Serial.println();Serial.println(Place your finger on the sensor...);Serial.println();// Initialize the sensorif(!sensor.begin(Wire,I2C_SPEED_FAST)){Serial.println(Error initializing the sensor...);while(1);}sensor.setup();// Use default settingssensor.setPulseAmplitudeRed(0);// Turn off red LED}voidloop(){long irValuesensor.getIR();// Read infrared value// Check if a heartbeat was detectedif(checkForBeat(irValue)){long timeBetweenBeatsmillis()-lastBeat;lastBeatmillis();currentBPM60.0/(timeBetweenBeats/1000.0);// Only keep realistic valuesif(currentBPMMIN_BPMcurrentBPMMAX_BPM){rates[rateSpot](byte)currentBPM;rateSpot;if(rateSpotRATE_SIZE){rateSpot0;}// Count valid beats (until buffer is full)if(validBeatsRATE_SIZE){validBeats;}// Only calculate average when buffer is fullif(validBeatsRATE_SIZE){// Calculate the averageaverageBPM0;for(byte i0;iRATE_SIZE;i){averageBPMrates[i];}averageBPM/RATE_SIZE;}}}// Print once every secondif(millis()-lastPrint1000){lastPrintmillis();if(irValue50000){// No finger on the sensorSerial.println(Waiting for finger...);}elseif(averageBPM0){// Finger is present but not enough valid beats yetSerial.println(Measuring... keep your finger still);}else{// We have a valid averageSerial.print(Heart Rate: );Serial.print(averageBPM);Serial.println( BPM);}}}上传程序到开发板打开串口监视器把手指按压到传感器表面上稍等片刻即可输出心率数据人静坐时的心率通常属于静息心率范畴一般成年人在 50100 次/分钟 之间都算正常。获取血氧饱和度数值#includeWire.h#includeMAX30105.h#includespo2_algorithm.hMAX30105 sensor;// Buffers needed by the algorithm#defineBUFFER_SIZE100uint32_tirBuffer[BUFFER_SIZE];uint32_tredBuffer[BUFFER_SIZE];int32_tspo2;// Oxygen saturation valueint8_tvalidSPO2;// 1 valid reading, 0 not valid yetint32_theartRate;// Not used, but required by the functionint8_tvalidHeartRate;voidsetup(){Serial.begin(115200);Serial.println(MAX30102 Oxygen Saturation);Serial.println();Serial.println(Place your finger on the sensor...);Serial.println();// Initialize the sensorif(!sensor.begin(Wire,I2C_SPEED_FAST)){Serial.println(Error initializing the sensor...);while(1);}byte ledBrightness60;// 0Off to 25550mAbyte sampleAverage4;// 1, 2, 4, 8, 16, 32byte ledMode2;// 1 Red only, 2 Red IR, 3 Red IR Greenbyte sampleRate100;// 50, 100, 200, 400, 800, 1000, 1600, 3200intpulseWidth411;// 69, 118, 215, 411intadcRange4096;// 2048, 4096, 8192, 16384// Configure sensor with these settingssensor.setup(ledBrightness,sampleAverage,ledMode,sampleRate,pulseWidth,adcRange);}voidloop(){// Collect 100 samples (takes a few seconds)for(byte i0;iBUFFER_SIZE;i){while(sensor.available()false){sensor.check();}redBuffer[i]sensor.getRed();irBuffer[i]sensor.getIR();sensor.nextSample();}// Calculate SpO2maxim_heart_rate_and_oxygen_saturation(irBuffer,BUFFER_SIZE,redBuffer,spo2,validSPO2,heartRate,validHeartRate);// Display oxygen saturationif(validSPO2){Serial.print(Oxygen Saturation: );Serial.print(spo2);Serial.println( %);}else{Serial.println(Measuring... keep your finger still);}Serial.println();}上传程序到开发板打开串口监视器把手指按压到传感器表面上稍等片刻即可输出血氧饱和度数值静坐或安静状态下98%100%很常见很好95%97%一般也算正93%94%偏低建议复测看是否手指冷、末梢循环差、指甲油、仪器误差等≤92%尤其如果持续这样建议就医≤90%属于低氧血症范围比较危险尤其伴呼吸困难、胸闷、意识模糊时应尽快就医。获取指尖温度MAX30102传感器还配备了一个温度传感器该传感器可用于校准心率及血氧饱和度数值。还可以利用这个传感器来获取体温值此处指指尖温度。以下代码展示了如何使用ESP32从MAX30102传感器中获取体温值。#includeWire.h#includeMAX30105.hMAX30105 sensor;voidsetup(){Serial.begin(115200);Serial.println(MAX30102 Temperature Sensor);Serial.println();// Initialize the sensorif(!sensor.begin(Wire,I2C_SPEED_FAST)){Serial.println(Error initializing the sensor...);while(1);}// Turn off the LEDs so they dont heat the sensorsensor.setup(0);// 0 LEDs offsensor.enableDIETEMPRDY();// Enable temperature ready signal}voidloop(){// Read temperature in CelsiusfloattempCsensor.readTemperature();// Convert to FahrenheitfloattempFtempC*1.832.0;// Print the temperature readingsSerial.print(Temperature: );Serial.print(tempC,2);Serial.print( °C | );Serial.print(tempF,2);Serial.println( °F);delay(1000);}上传程序到开发板打开串口监视器把手指按压到传感器表面上即可输出温度值
阅读完成 · 觉得有帮助?