How can I detect when there is no pulse coming from the pulse sensor?

Hello,

I recently bought a pulse sensor from pulsesensor.com and I'm using the example sketch called "Getting_BPM_to_Monitor" that's provided in the library. When I attach the sensor to my finger and run the code, I can see my BPM being displayed on the serial monitor. But when I remove my finger, it returns nothing. I would like to return "No pulse" when the sensor is not receiving any pulse. I am stuck and I don't know how to go about doing that.

Here's the code:

  • List item

Have a timer reset by the pulses. When the pulses stop, the timer is no longer being reset and will time out. The timer output can be polled or trigger an interrupt.

"no pulse" is on a continuum between infinity pulses per second and infinity seconds per pulse.

What is the lowest pulse rate you are likely to see? If its a healthy human pulse

27bpm.

I'd guess if you see no pulse in a 10 sec period the sensor is faulty, not connected, or youre dead. (edited - thanks @markd833 )

Assuming 1 pulse equates to 1 heartbeat, that's 10 beats/sec or 600 beats/min. Or did my maths go wrong ...

I thought a timer was a great idea. I tried that, but it didn't work like I wanted. If you could provide me with a more simple explanation and perhaps a code example, I'd appreciate it. Sorry, I'm a bit slow.

That sounds like a lie designed to fool the forum members into writing code for you. Prove to the forum that you did try, by showing us the code you wrote and describing what the code did and what you wanted it to do.

Well - you'ld still be dead so you wouldnt SEE the display! :grinning:

Yep, either way round the prognosis isn't good .....

I didn't think I'd be accused of lying. But anyway, here's the proof.

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
                               // Otherwise leave the default "550" value. 
int Timer = 0;
                               
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

void setup() {   
  Serial.begin(9600);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
}
void loop() {
 int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 
 if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
  Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
  Serial.print("BPM: ");                        // Print phrase "BPM: " 
  Serial.println(myBPM);                        // Print the value inside of myBPM. 
 }
 Timer = Timer + 1;
 Serial.println(Timer);
 delay(1000);

 if(Timer > 4 && PulseWire < Threshold){
  Serial.println("No pulse");
  Timer = 0;
 }
 delay(20);                    // considered best practice in a simple sketch.
}

I added a timer where if 5 seconds had passed without the sensor detecting a beat, "no pulse" would print on the serial monitor. But it still shows "no pulse" even when it's picking up a beat.

Thank you, and I apologise for calling you a liar. We do get visitors to the forum who do not want to have to write code themselves and want the forum to do the hard work for them without any effort on their part. Often they produce no proof that they have even tried.

You should remove those delay() and use millis() to detect when the last beat was > 5s ago.

After reading your post over and over again, it finally clicked! It took me a while to figure it out, but I got it! Thank you so much! I'll leave the code here just in case someone needs it.

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library.   

//  Variables
const int PulseWire = 0;       // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13;          // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore.
                               // Use the "Gettting Started Project" to fine-tune Threshold Value beyond default setting.
                               // Otherwise leave the default "550" value. 
int Timer = 0;
                               
PulseSensorPlayground pulseSensor;  // Creates an instance of the PulseSensorPlayground object called "pulseSensor"

void setup() {   

  Serial.begin(9600);          // For Serial Monitor

  // Configure the PulseSensor object, by assigning our variables to it. 
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       //auto-magically blink Arduino's LED with heartbeat.
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and "began" seeing a signal. 
   if (pulseSensor.begin()) {
    Serial.println("We created a pulseSensor Object !");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
}

void loop() {
 int myBPM = pulseSensor.getBeatsPerMinute();  // Calls function on our pulseSensor object that returns BPM as an "int".
                                               // "myBPM" hold this BPM value now. 

  Timer = Timer + 1;
  delay(1000);
  //Serial.println(Timer);
  
   
 if (pulseSensor.sawStartOfBeat()) {            // Constantly test to see if "a beat happened". 
  Serial.println("♥  A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened".
  Serial.print("BPM: ");                        // Print phrase "BPM: " 
  Serial.println(myBPM);                        // Print the value inside of myBPM. 
  Timer = 0;
 }

 if (Timer > 1){
  Serial.println("No pulse");
 }
 
 delay(20);                    // considered best practice in a simple sketch.
}

Why?

Your sketch will show a fail if there is no pulse in over a second. Thats (check my maths) 60bpm.

You can fix this easily by changing this code

Now that you have the idea you would learn a lot from modifying it to use millis() timing.

and the "blink without delay" example.