Hi!
I am very new to the Arduino community and i just learned many tutorials on Arduino website.
I am using Arduino UNO board and I am trying to get the time difference between two peaks of heart rate sensor.
The output from the heart rate sensor looks as the one shown below (CRO output). I need the time difference between two successive R-R peaks.
Can anyone help me with the code?


Hi,
Do you have your arduino reading an analog input from the sensor? Your photo shows no scale. What does the signal look like?
What is your circuit/sensor?
Need more information to help with this..
First you need to teach the Arduino to identify the R peak.
When you've solved that problem, use the millis() function to record the time that the peak is observed.
Repeat for the second peak and calculate the difference in times.
terryking228:
Hi,
Do you have your arduino reading an analog input from the sensor? Your photo shows no
scale. What does the signal look like?
What is your circuit/sensor?
Need more information to help with this..
Hi
The sensor output is the one shown in the image- index.jpg
The sensor I am using is a HEART/PULSE RATE SENSOR. I have posted the image below
jremington:
First you need to teach the Arduino to identify the R peak.
When you've solved that problem, use the millis() function to record the time that the peak
is observed.
Repeat for the second peak and calculate the difference in times.
Hi,
Thank you for your reply
Today I have tried to developed a code in arduino which is as follows.
My aim is to detect the peak and calculate the time difference between adjacent peaks. If the time difference varies from the normal (say a threshold) value, then a LED is used to show that variation (A LED blink)
I don't know whether the code works correctly.(It got compiled successfully). Do suggest if there is something wrong!
const int ledpin=13;
const int oledpin=10;
int i;
int ledstate=LOW;
long int previousMillis=0;
long int timediff;
int peakvalue=0;
int threshold=80; //The threshold is fixed depending on the sensor output(yet to be decided. For now I have assumed 80.
void setup() {
pinMode(ledpin,LOW);
pinMode(oledpin,LOW);
}
void loop() {
int sensorvalue = analogRead(A0);//Reads the sensor input
if(sensorvalue>peakvalue) //If the sensor value obtained is greater than the peak value already initialised.
{
peakvalue=sensorvalue;//the sensor value is the peak obtained
long int currentMillis=millis();// The time at which the peak is detected is noted
previousMillis=currentMillis;// This is stored in another variable
if(sensorvalue<=threshold && peakvalue>threshold)// The ecg reads 3 peaks PRT. The sensor value first reads the peak P.
// Now at this point the sensor will read the peak 'R' which is higher than 'P'
//with this comparison the R peak will be detected.
{
ledstate=HIGH; // when the peak is detected the output is given to the LED
long int currentMillis=millis(); // Now the current time is noted
timediff=previousMillis-currentMillis; // The time difference between the previous value and the current value is found.
digitalWrite(ledpin,HIGH);
}
else
{
ledstate=LOW;
digitalWrite(ledpin,LOW);
}
for(i=0;i<=5;i++) // The abnormality of HRV(Heart Rate variability cannot be detected with a
single difference
// So a series of 5 values are taken to confirm the abnormality
{
if(timediff<1000) // I have assumed the usual difference to be 1 second
i++;
}
if(i==5)
{
digitalWrite(oledpin,HIGH);
}
}
}
The image of HEART/PULSE RATE SENSOR

I don't know whether the code works correctly.
We don't either. You will need to actually test it.