simple code for heart rate monitor

Hi Everyone,

I hope you can help me. I’ve been trying to make a simple code that will output my heart rate to the LED screed connector for an Arduino Uno. I have a pulse sensor (from but I cannot get anything to output. Here’s my code:

#include <Time.h>

int sensorPin = A0; // select the input pin for the potentiometer
float sensorValue = 0; // variable to store the value coming from the sensor
int count=9;
int starttime = 0;
int heartrate = 0;
boolean counted = false;

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(sensorPin, INPUT);

void loop() {
// read the value from the sensor:

starttime = millis();
while (millis()<starttime+10000) // so i’m reading the pulse for 10 seconds
sensorValue = analogRead(sensorPin);
if (sensorValue >550 && counted == false) // counting the pulse, and making sure I dont count
{ // each peak more than once
counted = true;
else if (sensorValue < 500)
counted = false;

heartrate = count*6; // multiplying by six to get pulse per minute
Serial.println(heartrate); // printing to serial screen

count =0;

This doesnt output anything to the serial screen and I can’t figure out why.

Thanks for your help!

#include <Time.h>
You are not using anything from the timer library so why include it?

int starttime = 0
All variables that use the millis timer must be of the unsigned long type not an int.

How have you wired up your sensor?
Please provide a link to it.


I included the time library because i thought I had to? I found that on another arduino forum, and they included it so I did as well. I will change the starttime to an unsigned long, thanks for that tidbit.

As to how I have wired the sensor, I followed the tutorial at which can be found here (Redirect to: - Google Docs )

so I put RED wire to +5V, BLACK wire to GND, and PURPLE wire to the A0 pin.

I had previously found the signal fluctuation between about 400 and 700, so that is where my cutoffs of 500 and 550 came from. How do you use a sliding average?

I would add a few lines to flash the on board LED in time with your counting.
In setup define pin 13 as an output.
Set this line high when you set counted = true
Set this lie low when you set counted = false

If you are getting signals it should flash.


Thank you for the troubleshooting skills. The LED on the Uno board now lights up with my heartbeat. However, I still cannot see the heartrate displayed on the script screen. I thought maybe moving the heartrate calculation and print command out of the void loop might help but that doesn't seem to do anything. Any thoughts?

hellow this is my code .i think it does not work properly,can u help me
#include <TimerOne.h>
int input=4;
int ledPin = 13;
int Bit=0;
int count=0;
int rate=0;
boolean ledOn = false;
bool flag = 0;

void setup()

Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt

void loop()
if(digitalRead(input) == 0)
flag = 1;

void callback(){
if(count==16) //after (16/2)=8sec,reset all
Serial.println(“your heart rate is” );


use unsigned long int for executing while loop for millis()

ie; unsigned long starttime = 0;

Why don't you use the code samples which are linked from ?
i.e. GitHub - WorldFamousElectronics/PulseSensor_Amped_Arduino: PulseSensor Arduino code for BPM and Processing-Visualizer

@tejalions This post is over a year old, do you think your comment is relevant anymore?