arduino esp32 MAX30102
时间: 2023-09-07 17:17:49 浏览: 316
Arduino ESP32和MAX30102传感器可以一起使用,用于监测心率和血氧水平。以下是使用Arduino IDE编写的简单代码示例:
1. 首先,需要安装MAX30102库。在Arduino IDE中点击“工具”>“管理库”,然后搜索“MAX30102”并安装“MAX30105 Particle and MAX30102 Heart-Rate Sensor Library”。
2. 然后,将MAX30102传感器连接到Arduino ESP32。连接如下:
- VCC连接到ESP32的3.3V引脚
- GND连接到ESP32的GND引脚
- SCL连接到ESP32的SCL引脚
- SDA连接到ESP32的SDA引脚
3. 接下来,打开Arduino IDE并创建一个新的空白项目。将以下代码复制并粘贴到项目中:
```
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
byte rate[4];
long hr;
byte spo2;
boolean began = false;
void setup()
{
Serial.begin(115200);
pinMode(13, OUTPUT);
Serial.print("Initializing...");
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
Serial.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
Serial.println("MAX30105 found!");
particleSensor.setup();
particleSensor.setPulseAmplitudeRed(0x0A);
particleSensor.setPulseAmplitudeGreen(0);
particleSensor.setPulseAmplitudeIR(0);
Serial.println("Place your index finger on the sensor with steady pressure.");
began = true;
}
void loop()
{
if (began == true)
{
int32_t irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
digitalWrite(13, !digitalRead(13)); //Blink onboard LED with every heart beat
long delta = millis() - lastBeat;
lastBeat = millis();
beatsPerMinute = 60 / (delta / 1000.0);
if (beatsPerMinute < 255 && beatsPerMinute > 20)
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable
//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}
if (millis() - tsLastReported > REPORTING_PERIOD_MS)
{
Serial.print("Heart rate:");
Serial.print(beatAvg);
Serial.print(" bpm ");
Serial.print("Oxygen Saturation:");
Serial.print(particleSensor.getSpO2());
Serial.println("%");
tsLastReported = millis();
}
}
}
```
4. 上传代码到Arduino ESP32并打开串口监视器。将手指放在MAX30102传感器上,应该能够看到心率和血氧水平的实时读数。
阅读全文