Example Code for Arduino-Read Heart Rate (Digital Mode)
This example shows how to read heart rate data in digital mode by configuring the sensor, uploading the sample code, and viewing the results in the Arduino Serial Monitor.
Hardware Preparation
- DFRduino UNO (or similar)
- Heart Rate Monitor Sensor for Arduino
- IO Expansion Shield for Arduino (suggested)
- Some wires
Software Preparation
- Arduino IDE 1.6.6+ - Click to Download Arduino IDE from Arduino®
- Library: Sample Code on Github,How to install Libraries in Arduino IDE?
Wiring Diagram
- Select the sensor's mode switch as "D" for Digital
- Connect it with UNO's A1 as the code indicates (you could change it as you wish)
Other Preparation Work
Once you opened the box, you can find a black belt inside of the package. Thread it through the holes of the sensor as indicted in the photo. And then you could attach it on your finger (suggested), wrist or any other places where exposes blood vessels. Do not attach the belt too tight or too loose to your finger or the reading might be not stable. During the test, you should steady your finger and do not move too much, or the readings might be unstable.
1.Thread the belt through the holes
2.Wrap on finger
3.Wrap on wrist
4.Wrap on the back of wrist
Sample Code
The sample code could be found in the Github Page, open the example "DFRobot_Heartrate_Digital_Mode".
/*!
* @file DFRobot_Heartrate.h
* @brief DFRobot_Heartrate.h detailed description for Heartrate.cpp
*
* This is written for the heart rate sensor the company library. Mainly used for real
* time measurement of blood oxygen saturation, based on measured values calculate heart rate values.
*
* @author linfeng([email protected])
* @version V1.1
* @date 2016-8-16
* @version V1.0
* @date 2015-12-24
*/
#define heartratePin A1
#include "DFRobot_Heartrate.h"
DFRobot_Heartrate heartrate(DIGITAL_MODE); ///< ANALOG_MODE or DIGITAL_MODE
void setup() {
Serial.begin(115200);
}
void loop() {
uint8_t rateValue;
heartrate.getValue(heartratePin); ///< A1 foot sampled values
rateValue = heartrate.getRate(); ///< Get heart rate value
if(rateValue) {
Serial.println(rateValue);
}
delay(20);
}
/******************************************************************************
Copyright (C) <2015> <linfeng>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Contact: [email protected]
******************************************************************************/
Result
Open the Serial Monitor and place your finger on the sensor.
Adjust the finger position until the onboard LED flashes regularly in sync with your heartbeat.
After a few seconds, the Serial Monitor will display the measured heart rate.
The switch is used to select the working mode of the module, setting the output signal to either Digital or Analog.
The switch setting must match both the code and the wiring. For example, if the switch is set to Analog (A) but the code usesdigitalRead()or the module is connected to a digital pin on the Arduino, no valid readings will be obtained. The same issue applies in the opposite configuration.
Was this article helpful?
