常用传感器讲解三--心率传感器-KY-039(heartbeat)

简介: 常用传感器讲解三

具体讲解

心跳传感器会返回一个心率的数字。传感器提供的只是一个从0到1023的“模拟”值

简而言之:把手指放在传感器的IR led和光晶体管之间。你的心跳会使手指的血管扩张,从而过滤掉红外线。这就产生了一个脉动信号。

电路连接

截屏2023-12-27 下午7.19.27.png

效果图
截屏2023-12-27 下午7.19.49.png

代码介绍

#define samp_siz 4
#define rise_threshold 5
// Pulse Monitor Test Script
int sensorPin = 0;
void setup() {
   
   
   Serial.begin(9600);
}
void loop ()
{
   
   
   float reads[samp_siz], sum;
   long int now, ptr;
   float last, reader, start;
   float first, second, third, before, print_value;
   bool rising;
   int rise_count;
   int n;
   long int last_beat;
   for (int i = 0; i < samp_siz; i++)
     reads[i] = 0;
   sum = 0;
   ptr = 0;
   while(1)
   {
   
   
     // calculate an average of the sensor
     // during a 20 ms period (this will eliminate
     // the 50 Hz noise caused by electric light
     n = 0;
     start = millis();
     reader = 0.;
     do
     {
   
   
       reader += analogRead (sensorPin);
       n++;
       now = millis();
     }
     while (now < start + 20);  
     reader /= n;  // we got an average
     // Add the newest measurement to an array
     // and subtract the oldest measurement from the array
     // to maintain a sum of last measurements
     sum -= reads[ptr];
     sum += reader;
     reads[ptr] = reader;
     last = sum / samp_siz;
     // now last holds the average of the values in the array
     // check for a rising curve (= a heart beat)
     if (last > before)
     {
   
   
       rise_count++;
       if (!rising && rise_count > rise_threshold)
       {
   
   
         // Ok, we have detected a rising curve, which implies a heartbeat.
         // Record the time since last beat, keep track of the two previous
         // times (first, second, third) to get a weighed average.
         // The rising flag prevents us from detecting the same rise 
         // more than once.
         rising = true;
         first = millis() - last_beat;
         last_beat = millis();
         // Calculate the weighed average of heartbeat rate
         // according to the three last beats
         print_value = 60000. / (0.4 * first + 0.3 * second + 0.3 * third);
         Serial.print(print_value);
         Serial.print('\n');
         third = second;
         second = first;
       }
     }
     else
     {
   
   
       // Ok, the curve is falling
       rising = false;
       rise_count = 0;
     }
     before = last;
     ptr++;
     ptr %= samp_siz;
   }
}
相关文章
|
7月前
|
传感器 数据采集 监控
[开源免费]基于STM32的心率监控仪 —— 从原理到实现的完整技术解析
本文详解基于STM32的心率监控仪设计,涵盖硬件搭建、ADC采样、峰值检测算法及OLED波形显示。采用STM32F103C8T6与PulseSensor实现心率实时监测,支持报警提示与按键交互,适合嵌入式入门与课程实践。
如何查看自己电脑的windows系统版本?
这篇文章提供了一种简单快捷的方法来查看自己电脑的Windows系统版本,通过使用Windows的"运行"功能并输入`winver`命令来快速获取系统版本信息。
如何查看自己电脑的windows系统版本?
|
C++
爱心代码 C++
这段C++代码使用EasyX图形库生成动态爱心图案。程序通过数学公式绘制爱心形状,并以帧动画形式呈现渐变效果。运行时需安装EasyX库,教程链接:http://【EasyX图形库的安装和使用】https://www.bilibili.com/video/BV1Xv4y1p7z1。代码中定义了屏幕尺寸、颜色数组等参数,利用随机数与数学函数生成动态点位,模拟爱心扩散与收缩动画,最终实现流畅的视觉效果。
1300 0
|
传感器 物联网 Linux
Python:蓝牙心率广播设备监测(BLE 心率监测器)技术解析与实现
本文探讨了如何使用 Python 脚本与支持蓝牙低功耗(BLE)心率广播的设备交互以获取实时心率数据。重点分析了 BLE 协议、GATT 服务模型,以及具体方法。此外,还讨论了华为手表等设备的兼容性问题。
2532 19
|
缓存 网络协议 网络架构
以太网数据链路层、Ethernet_II帧格式、IEEE802.3帧格式,以太网的MAC地址的组成,ARP地址解析协议的工作原理,单播帧、组播帧、广播帧的区别
数据链路层,Ethernet_II帧格式、IEEE802.3帧格式,帧格式的区分以及链路层每种帧格式有什么作用,怎么区别分辨帧格式,以太网MAC地址的组成,ARP地址解析协议原理、什么是单播帧?什么是组播数据帧?什么是广播帧?...............
2089 0
以太网数据链路层、Ethernet_II帧格式、IEEE802.3帧格式,以太网的MAC地址的组成,ARP地址解析协议的工作原理,单播帧、组播帧、广播帧的区别
|
传感器 编解码 IDE
|
传感器 安全 物联网
5G车联网技术:智能交通的未来
【10月更文挑战第26天】
753 1
|
Ubuntu Linux 虚拟化
安装Windows Linux 子系统的方法:适用于windows 11 版本
本文提供了在Windows 11系统上安装Linux子系统(WSL)的详细步骤,包括启用子系统和虚拟化功能、从Microsoft Store安装Linux发行版、设置WSL默认版本、安装WSL2补丁,以及完成Ubuntu的首次安装设置。
6626 2
|
Linux Windows
LabVIEW安装了哪些版本的LabVIEW运行引擎
LabVIEW安装了哪些版本的LabVIEW运行引擎
684 0
|
JSON 安全 Java
Star 28.2k!这个开源库真是好用
阅读Hutool的源码是深入理解其工作原理的有效方式。通过阅读源码,你可以学习到Hutool的实现细节,了解其内部的逻辑和设计模式。这对于提高自己的编程技能和理解Hutool的精髓非常有帮助。由于分析源码需要更大的文章篇幅,后续有时间,V 哥再单独写一篇文章来解释这些好用工具类的源码分析。
429 1