血氧模块传感器基础功能实现:
1、Example1_Basic_Readings
Red LED和 IR LED交替点亮来检测人体皮肤下血液的搏动和血氧含量,基础数据读取,red和ir,及其green数据。
示例代码:
/*
Outputs all Red/IR/Green values.
Hardware Connections (Breakoutboard to Arduino):
-5V = 5V (3.3V is allowed)
-GND = GND
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V but it will also run at 3.3V.
*/
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
#define debug Serial //Uncomment this line if you're using an Uno or ESP
void setup()
{
debug.begin(115200);
debug.println("Initializing...");
// Initialize sensor
if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
{
debug.println("MAX30105 was not found. Please check wiring/power. ");
while (1);
}
particleSensor.setup(); //Configure sensor. Use 6.4mA for LED drive
}
void loop()
{
debug.print(" R[");
debug.print(particleSensor.getRed());
debug.print("] IR[");
debug.print(particleSensor.getIR());
debug.print("] G[");
debug.print(particleSensor.getGreen());
debug.print("]");
debug.println();
}
无数据检测状态

有数据检测状态

2、Example2_Presence_Sensing
读取通电时的平均读数,如果读数变化超过100,则打印"Something is there!"
示例代码:
/*
This takes an average reading at power up and if the reading changes more than 100 then print 'Something is there!'.
Hardware Connections (Breakoutboard to Arduino):
-5V = 5V (3.3V is allowed)
-GND = GND
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V but it will also run at 3.3V.
*/
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
long samplesTaken = 0; //Counter for calculating the Hz or read rate
long unblockedValue; //Average IR at power up
long startTime; //Used to calculate measurement rate
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
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);
}
//Setup to sense up to 18 inches, max LED brightness
byte ledBrightness = 0xFF; //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 = 400; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 2048; //Options: 2048, 4096, 8192, 16384
particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings
particleSensor.setPulseAmplitudeRed(0); //Turn off Red LED
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
//Take an average of IR readings at power up
unblockedValue = 0;
for (byte x = 0 ; x < 32 ; x++)
{
unblockedValue += particleSensor.getIR(); //Read the IR value
}
unblockedValue /= 32;
startTime = millis();
}
void loop()
{
samplesTaken++;
Serial.print("IR[");
Serial.print(particleSensor.getIR());
Serial.print("] Hz[");
Serial.print((float)samplesTaken / ((millis() - startTime) / 1000.0), 2);
Serial.print("]");
long currentDelta = particleSensor.getIR() - unblockedValue;
Serial.print(" delta[");
Serial.print(currentDelta);
Serial.print("]");
if (currentDelta > (long)100)
{
Serial.print(" Something is there!");
}
Serial.println();
}
无数据检测状态

有数据检测状态

3、Example3_Temperature_Sense
温度数据读取,摄氏和华氏
示例代码:
/*
This demo outputs the onboard temperature sensor. The temp sensor is accurate to +/-1 C but has an astonishing precision of 0.0625 C.
Hardware Connections (Breakoutboard to Arduino):
-5V = 5V (3.3V is allowed)
-GND = GND
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V but it will also run at 3.3V.
*/
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
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);
}
//The LEDs are very low power and won't affect the temp reading much but
//you may want to turn off the LEDs to avoid any local heating
// particleSensor.setup(0); //Configure sensor. Turn off LEDs
particleSensor.setup(); //Configure sensor. Use 25mA for LED drive
}
void loop()
{
float temperature = particleSensor.readTemperature();
Serial.print("temperatureC=");
Serial.print(temperature, 4);
float temperatureF = particleSensor.readTemperatureF(); //Because I am a bad global citizen
Serial.print(" temperatureF=");
Serial.print(temperatureF, 4);
Serial.println();
}
无数据检测状态

有数据检测状态

4、Example4_HeartBeat_Plotter
心跳数据读取
示例代码:
/*
Shows the user's heart beat on Arduino's serial plotter
Instructions:
1) Load code onto Redboard
2) Attach sensor to your finger with a rubber band (see below)
3) Open Tools->'Serial Plotter'
4) Make sure the drop down is set to 115200 baud
5) Checkout the blips!
6) Feel the pulse on your neck and watch it mimic the blips
It is best to attach the sensor to your finger using a rubber band or other tightening
device. Humans are generally bad at applying constant pressure to a thing. When you press your finger against the sensor it varies enough to cause the blood in your
finger to flow differently which causes the sensor readings to go wonky.
Hardware Connections (Breakoutboard to Arduino):
-5V = 5V (3.3V is allowed)
-GND = GND
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V but it will also run at 3.3V.
*/
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
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);
}
//Setup to sense a nice looking saw tooth on the plotter
byte ledBrightness = 0x1F; //Options: 0=Off to 255=50mA
byte sampleAverage = 8; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 3; //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
//Arduino plotter auto-scales annoyingly. To get around this, pre-populate
//the plotter with 500 of an average reading from the sensor
//Take an average of IR readings at power up
const byte avgAmount = 64;
long baseValue = 0;
for (byte x = 0 ; x < avgAmount ; x++)
{
baseValue += particleSensor.getIR(); //Read the IR value
}
baseValue /= avgAmount;
//Pre-populate the plotter so that the Y scale is close to IR values
for (int x = 0 ; x < 500 ; x++)
Serial.println(baseValue);
}
void loop()
{
Serial.println(particleSensor.getIR()); //Send raw data to plotter
}
无数据检测状态

有数据检测状态

5、Example5_HeartRate
PBA算法演示案例
示例代码:
/*
This is a demo to show the reading of heart rate or beats per minute (BPM) using
a Penpheral Beat Amplitude (PBA) algorithm.
It is best to attach the sensor to your finger using a rubber band or other tightening
device. Humans are generally bad at applying constant pressure to a thing. When you press your finger against the sensor it varies enough to cause the blood in your
finger to flow differently which causes the sensor readings to go wonky.
Hardware Connections (Breakoutboard to Arduino):
-5V = 5V (3.3V is allowed)
-GND = GND
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V but it will also run at 3.3V.
*/
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
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("Place your index finger on the sensor with steady pressure.");
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED
}
void loop()
{
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true)
{
//We sensed a 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;
}
}
Serial.print("IR=");
Serial.print(irValue);
Serial.print(", BPM=");
Serial.print(beatsPerMinute);
Serial.print(", Avg BPM=");
Serial.print(beatAvg);
if (irValue < 50000)
Serial.print(" No finger?");
Serial.println();
}
无数据检测状态:

有数据检测状态:

6、Example6_FIFO_Readings
通过轮询FIFO输出25赫兹时的所有红色/红外/绿色值
示例代码:
/*
Outputs all Red/IR/Green values at 25Hz by polling the FIFO
Hardware Connections (Breakoutboard to Arduino):
-5V = 5V (3.3V is allowed)
-GND = GND
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V but it will also run at 3.3V.
*/
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
long startTime;
long samplesTaken = 0; //Counter for calculating the Hz or read rate
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
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);
}
particleSensor.setup(); //Configure sensor. Use 6.4mA for LED drive
startTime = millis();
}
void loop()
{
particleSensor.check(); //Check the sensor, read up to 3 samples
while (particleSensor.available()) //do we have new data?
{
samplesTaken++;
Serial.print(" R[");
Serial.print(particleSensor.getFIFORed());
Serial.print("] IR[");
Serial.print(particleSensor.getFIFOIR());
Serial.print("] G[");
Serial.print(particleSensor.getFIFOGreen());
Serial.print("] Hz[");
Serial.print((float)samplesTaken / ((millis() - startTime) / 1000.0), 2);
Serial.print("]");
Serial.println();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
}
}
无数据检测状态:

有数据检测状态:

7、Example7_Basic_Readings_Interrupts
输出所有红色/红外/绿色值,检查是否发生了中断,检查中断寄存器是否设置
示例代码:
/*
Outputs all Red/IR/Green values as fast as possible
Checks the interrupt pin to see if an interrupt occurred
Checks the interrupt register to see if a bit was set
Hardware Connections (Breakoutboard to Arduino):
-5V = 5V (3.3V is allowed)
-GND = GND
-SDA = A4 (or SDA)
-SCL = A5 (or SCL)
-INT = Not connected
The MAX30105 Breakout can handle 5V or 3.3V I2C logic. We recommend powering the board with 5V but it will also run at 3.3V.
*/
#include <Wire.h>
#include "MAX30105.h"
MAX30105 particleSensor;
long startTime;
long samplesTaken = 0; //Counter for calculating the Hz or read rate
byte interruptPin = 3; //Connect INT pin on breakout board to pin 3
void setup()
{
pinMode(interruptPin, INPUT);
Serial.begin(115200);
Serial.println("Initializing...");
// Initialize sensor
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);
}
//Let's configure the sensor to run fast so we can over-run the buffer and cause an interrupt
byte ledBrightness = 0x7F; //Options: 0=Off to 255=50mA
byte sampleAverage = 1; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 3; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 400; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 69; //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.enableAFULL(); //Enable the almost full interrupt (default is 32 samples)
particleSensor.setFIFOAlmostFull(3); //Set almost full int to fire at 29 samples
startTime = millis();
}
void loop()
{
particleSensor.check(); //Check the sensor, read up to 3 samples
while (particleSensor.available()) //do we have new data?
{
samplesTaken++;
Serial.print(" R[");
Serial.print(particleSensor.getRed());
Serial.print("] IR[");
Serial.print(particleSensor.getIR());
Serial.print("] G[");
Serial.print(particleSensor.getGreen());
Serial.print("] Hz[");
Serial.print((float)samplesTaken / ((millis() - startTime) / 1000.0), 2);
Serial.print("]");
if (digitalRead(interruptPin) == LOW) //Hardware way of reading interrupts
{
Serial.print(" INT!");
}
byte flags = particleSensor.getINT1(); //Software way of reading interrupts
if (flags)
{
Serial.print(" I[");
Serial.print(flags, BIN);
Serial.print("]");
}
Serial.println();
particleSensor.nextSample(); //We're finished with this sample so move to next sample
}
}
无数据检测状态:

有数据检测状态:

网友评论