Overview
Lub dub… Lub dub… In the midst of the silence, you hear the most hardworking muscle beating 24/7 to keep you alive. It’s no doubt, with it’s distinct rhythm, that it’s the Heart.
Continuing from the previous post, the goal of this mini-tutorial is to use a heartbeat sensor to perceive a pulse. The Beats per Minute is calculated as the number of beats divided by a sampling time.
Hardware Used
Software Used
Library Used
If you open the Pulse Sensor library, it is well documented and very self-explanatory. (Open the .h file with notepad)
Application Description
According to medical-dictionary.com, a heartbeat is the cycle of contraction and expansion of the heart.
When the heart beats, blood pressure causes a very slight movement in some parts of the body, such as in the necks and fingertips. The Heartbeat sensor, albeit very similar to a proximity sensor, recognizes this movement via a photodiode and an LED.
The typical resting heart beats per minute lies about 60 to 100 BPM.
Calibration
Before we do anything else, we should calibrate the sensor ! Luckily, the steps are straightforward.
- Set-up and Upload the Code in the Arduino
- Strap/Mount the Pulse Sensor. (Typically placed in the fingertips)
- Open Arduino’s Serial Plotter. Tools->Serial Plotter OR CTLR+SHIFT+L. After a few samples, it should show the following graph/figure
- Note the left number (Y-Axis) on where your heart beat occurs. Set this as your threshold.
- You have now calibrated your Pulse Sensor !
Set-up the Hardware
Setting up the hardware is pretty straightforward.
Code
This code is used for heartbeat detection calibration. The threshold separates the actual heartbeat from the background noise.
/* PulseSensor Starter Project and Signal Tester * The Best Way to Get Started With, or See the Raw Signal of, your PulseSensor & Arduino. * * Here is a link to the tutorial * https://pulsesensor.com/pages/code-and-guide * * WATCH ME (Tutorial Video): * https://www.youtube.com/watch?v=82T_zBZQkOE * * ------------------------------------------------------------- 1) This shows a live human Heartbeat Pulse. 2) Live visualization in Arduino's Cool "Serial Plotter". 3) Blink an LED on each Heartbeat. 4) This is the direct Pulse Sensor's Signal. 5) A great first-step in troubleshooting your circuit and connections. 6) "Human-readable" code that is newbie friendly." */ // Variables int PulseSensorPurplePin = 0; // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0 int LED13 = 13; // The on-board Arduion LED int Signal; // holds the incoming raw data. Signal value can range from 0-1024 int Threshold = 550; // Determine which Signal to "count as a beat", and which to ingore. // The SetUp Function: void setup() { pinMode(LED13,OUTPUT); // pin that will blink to your heartbeat! Serial.begin(9600); // Set's up Serial Communication at certain speed. } // The Main Loop Function void loop() { Signal = analogRead(PulseSensorPurplePin); // Read the PulseSensor's value. // Assign this value to the "Signal" variable. Serial.println(Signal); // Send the Signal value to Serial Plotter. if(Signal > Threshold){ // If the signal is above "550", then "turn-on" Arduino's on-Board LED. digitalWrite(LED13,HIGH); } else { digitalWrite(LED13,LOW); // Else, the sigal must be below "550", so "turn-off" this LED. } delay(10); }
The Code for measuring BPM is well-documented in the Library functions.
/* Code to detect pulses from the PulseSensor, using an interrupt service routine. Here is a link to the tutorial\ https://pulsesensor.com/pages/getting-advanced Copyright World Famous Electronics LLC - see LICENSE Contributors: Joel Murphy, https://pulsesensor.com Yury Gitman, https://pulsesensor.com Bradford Needham, @bneedhamia, https://bluepapertech.com Licensed under the MIT License, a copy of which should have been included with this software. This software is not intended for medical use. */ /* Every Sketch that uses the PulseSensor Playground must define USE_ARDUINO_INTERRUPTS before including PulseSensorPlayground.h. Here, #define USE_ARDUINO_INTERRUPTS true tells the library to use interrupts to automatically read and process PulseSensor data. See ProcessEverySample.ino for an example of not using interrupts. */ #define USE_ARDUINO_INTERRUPTS true #include <PulseSensorPlayground.h> /* The format of our output. Set this to PROCESSING_VISUALIZER if you're going to run the Processing Visualizer Sketch. See https://github.com/WorldFamousElectronics/PulseSensor_Amped_Processing_Visualizer Set this to SERIAL_PLOTTER if you're going to run the Arduino IDE's Serial Plotter. */ const int OUTPUT_TYPE = SERIAL_PLOTTER; /* Pinout: PIN_INPUT = Analog Input. Connected to the pulse sensor purple (signal) wire. PIN_BLINK = digital Output. Connected to an LED (and 220 ohm resistor) that will flash on each detected pulse. PIN_FADE = digital Output. PWM pin onnected to an LED (and resistor) that will smoothly fade with each pulse. NOTE: PIN_FADE must be a pin that supports PWM. Do not use pin 9 or 10, because those pins' PWM interferes with the sample timer. */ const int PIN_INPUT = A0; const int PIN_BLINK = 13; // Pin 13 is the on-board LED const int PIN_FADE = 5; const int THRESHOLD = 550; // Adjust this number to avoid noise when idle /* All the PulseSensor Playground functions. */ PulseSensorPlayground pulseSensor; void setup() { /* Use 115200 baud because that's what the Processing Sketch expects to read, and because that speed provides about 11 bytes per millisecond. If we used a slower baud rate, we'd likely write bytes faster than they can be transmitted, which would mess up the timing of readSensor() calls, which would make the pulse measurement not work properly. */ Serial.begin(115200); // Configure the PulseSensor manager. pulseSensor.analogInput(PIN_INPUT); pulseSensor.blinkOnPulse(PIN_BLINK); pulseSensor.fadeOnPulse(PIN_FADE); pulseSensor.setSerial(Serial); pulseSensor.setOutputType(OUTPUT_TYPE); pulseSensor.setThreshold(THRESHOLD); // Now that everything is ready, start reading the PulseSensor signal. if (!pulseSensor.begin()) { /* PulseSensor initialization failed, likely because our particular Arduino platform interrupts aren't supported yet. If your Sketch hangs here, try ProcessEverySample.ino, which doesn't use interrupts. */ for(;;) { // Flash the led to show things didn't work. digitalWrite(PIN_BLINK, LOW); delay(50); digitalWrite(PIN_BLINK, HIGH); delay(50); } } } void loop() { /* Wait a bit. We don't output every sample, because our baud rate won't support that much I/O. */ delay(20); // write the latest sample to Serial. pulseSensor.outputSample(); /* If a beat has happened since we last checked, write the per-beat information to Serial. */ if (pulseSensor.sawStartOfBeat()) { pulseSensor.outputBeat(); } }
Code Breakdown
If you open the library, it is very self-explanatory and simple. The author of the library has a detailed comment about each function calls.
pulsesensor.begin();
Start reading and processing data from the PulseSensors.
pulsesensor.sawNewSample();
Returns true if a new sample has been read from each PulseSensor.
pulsesensor.getBeatsPerMinute(int sensorIndex = 0);
Returns the latest beats-per-minute measure for the given PulseSensor. The sensorIndex is optional if you put more pulse sensor.
pulsesensor.sawStartOfBeat(int sensorIndex = 0);
Returns true if a new heartbeat (pulse) has been detected from the given PulseSensor since the last call to sawStartOfBeat() on this PulseSensor.
pulsesensor.setThreshold(int threshold, int sensorIndex = 0);
Threshold is used to find the heartbeat. Adjust this value up in the setup function to avoid noise.
Conclusion
The Infrared LED plus Photodiode is a flexible combination of electronic elements. Applications of the Infrared transceiver ranges from a simple proximity sensor (such as the pulse sensor) to your every-day Television Remote Control. By simply adjusting the sensitivity of the photo diode, it has been made to sense the beat of the heart. How cool is that ?
References
https://pulsesensor.com/pages/installing-our-playground-for-pulsesensor-arduino
The post (2/4) BPM (Pulse Sensor) and Pedometer (ADXL345 Accelerometer) Wearable Device with OLED Display appeared first on CreateLabz.