|
本帖最后由 vany5921 于 2019-9-25 13:43 编辑
https://github.com/AmbientDataInc/M5Stack_PulseSensor/blob/master/M5Stack_PulseSensor/M5Stack_PulseSensor.ino
关于心率传感器目前常见的有ECG,MAX3010X系列,PulseSensor,ECG通过采集生物电信号进行检测,MAX3010x通过血管搏动时透光率不同进行光电信号转换,PulseSensor也是通过采集光电信号进行转化,输出模拟信号。从使用难易程度上比较,PulseSensor是最容易的,官网提供了库和使用的参考实例,应用起来比较简单。显示界面如下,没有接入任何外设,adc很平稳
下面通过M5Stack与PulseSensor进行连接,利用M5Stack自带的屏幕进行显示,PIN36是ADC输入,所以将传感器接入36pin,VCC和GND分别接入3V3和GND,为了过滤掉一些噪声,程序中设置了阈值,高于此数值的为有效数据
[mw_shl_code=arduino,true]
#define ARDUINO_M5Stack_Core_ESP32
#ifdef ARDUINO_M5Stack_Core_ESP32
#include <M5Stack.h>
#endif
#define USE_ARDUINO_INTERRUPTS true //开启中断
#include <PulseSensorPlayground.h>
const int OUTPUT_TYPE = SERIAL_PLOTTER; //图形绘制
const int PIN_INPUT = 36; //ADC引脚
const int THRESHOLD = 550; // 调节阈值,避免空闲电平时噪声
PulseSensorPlayground pulseSensor;
void setup() {
#ifdef ARDUINO_M5Stack_Core_ESP32
M5.begin();
dacWrite(25, 0); // 关闭M5Stack的扬声器
#endif
pulseSensor.analogInput(PIN_INPUT);
pulseSensor.setSerial(Serial);
pulseSensor.setOutputType(OUTPUT_TYPE);
pulseSensor.setThreshold(THRESHOLD);
if (!pulseSensor.begin()) {
Serial.println("PulseSensor.begin: failed");
for(;;) {
delay(0);
}
}
}
const int LCD_WIDTH = 320;
const int LCD_HEIGHT = 240;
const int DOTS_DIV = 30; //虚线间隔
#define GREY 0x7BEF //显示颜色
void DrawGrid() { //绘制描点绘制网格
for (int x = 0; x <= LCD_WIDTH; x += 2) { // 水平方向绘制
for (int y = 0; y <= LCD_HEIGHT; y += DOTS_DIV) {
M5.Lcd.drawPixel(x, y, GREY); //描点画虚线
}
if (LCD_HEIGHT == 240) { //如果到达屏幕底部,上移一行画虚线
M5.Lcd.drawPixel(x, LCD_HEIGHT - 1, GREY);
}
}
for (int x = 0; x <= LCD_WIDTH; x += DOTS_DIV) { // 垂直方向绘制
for (int y = 0; y <= LCD_HEIGHT; y += 2) {
M5.Lcd.drawPixel(x, y, GREY); //描点画虚线
}
}
}
#define REDRAW 20 //采集时间间隔20毫秒
short lastMin = 0, lastMax = 4096; //adc精度为12位
short minS = 4096, maxS = 0; //初始化最大最小值
int lastY = 0;
int x = 0;
void loop() {
delay(REDRAW);
int y = pulseSensor.getLatestSample(); //获取采样数据
if (y < minS) minS = y;
if (maxS < y) maxS = y;
if (x > 0) {
y = (int)(LCD_HEIGHT - (float)(y - lastMin) / (lastMax - lastMin) * LCD_HEIGHT); //等比例缩放数值计算显示坐标
M5.Lcd.drawLine(x - 1, lastY, x, y, WHITE); //绘制直线
lastY = y;
}
if (++x > LCD_WIDTH) { //如果画面超过屏幕宽度屏幕清空重新绘制网格同时显示心率
x = 0;
M5.Lcd.fillScreen(BLACK);
DrawGrid();
lastMin = minS - 20;
lastMax = maxS + 20;
minS = 4096;
maxS = 0;
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextSize(4);
M5.Lcd.printf("BPM: %d", pulseSensor.getBeatsPerMinute());
}
}[/mw_shl_code]
|
|