Pulse Sensor Triggering Event - Not Working

I'm working with some students on code that triggers a "cheerful message" when it detects a high pulse.
We got it to display a pulse, and to set a trigger if we faked a high pulse (manually set variable), however it never triggers when it actually detects a high pulse.

#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 LED = LED_BUILTIN;   //The on-board Arduino LED, close to PIN 13.
int Threshold = 550;           //Determine which Signal to "count as a beat" and which to ignore.
int myBPM;                     //Variable for recording the Pulse Rate (BPM)
int Triggered = 0;             //Variable for triggering the cheer-up event
unsigned long lastTimeCheckPulse = millis();
unsigned long delayBetweenCheckPulse = 5000;
unsigned long timeNow = millis();
int AdjustedBPM = 100;

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(LED);       //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("Sensor Is Alive!!");  //This prints one time at Arduino power-up,  or on Arduino reset.  
  }
}



void loop() {                                     //Start of void loop

  timeNow = millis();

  Serial.println(timeNow);

  if(timeNow - lastTimeCheckPulse > delayBetweenCheckPulse){
    CheckPulse();                                   //This runs all the code inside the "void CheckPulse" command below
    lastTimeCheckPulse = timeNow;
  } //END if

  CheckTrigger();
  
  if(Triggered == 1){
    SendMessage();
  } //End if

  delay(1000);

} //End of Void Loop

void CheckPulse(){
     Serial.println("Checking Pulse");  //Dubugging check point
  if (pulseSensor.sawStartOfBeat()) {            // Test to see if "a beat happened".
    int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
                                                  // "myBPM" hold this BPM value now. 
   Serial.println("♥  I'm Alive ! ");            // 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. 
  
  }  //END if
  Serial.println(myBPM);
// myBPM = 101;           //Fake Pulse Sensor (This is where we manually set pulse, which then triggers event)
}  //END void CheckPulse

void CheckTrigger(){

  if (myBPM > AdjustedBPM){                              //Check if BPM is over 100
   Triggered = 1;                               //If it is, trigger cheer-up event
    myBPM = 0;
  }  //END if

}  //END void CheckTriggered

void SendMessage(){

  Serial.println("Cheer Up!");                 //If it is, send message.
  Triggered = 0;                              // I added this line to reset the trigger, so it waits for the next trigger.

}  //END void SendMessage

Any suggestions would be appreciated!
-Teacher Tom

I Have tested your code here with my UNO and a pulse sensor and it worked well.

I think you have a bad pulse sensor.

84027
85026
86027
Checking Pulse
♥  I'm Alive ! 
BPM: 179
0
87028
88029
89028
90029
91029
Checking Pulse
♥  I'm Alive ! 
BPM: 237
0
92030
93030
94030
95031
96031
Checking Pulse
♥  I'm Alive ! 
BPM: 237

Why here:
" int myBPM = pulseSensor.getBeatsPerMinute(); (line 59)

you created a local variable with the name myBPM,

whereas the global variable myBPM already existed.

" int myBPM; //Variable for recording the Pulse Rate (BPM) (line 8). ?

Thanks!

That sounds like the problem. It should be telling you to cheer up if your pulse is over 100, so the local/global should solve the issue.

The local was from sample code for the Pulse Sensor, and the global was making it available everywhere. Just forgot to remove the line 59 declaration, which caused it to be both.

Also, I think the threshold needs adjusting, if it reads your pulse as 237.

I'll have the student test this and see if it works, then report back for the record.

See this video:

" pulse sensor not work - Google Search

The previous results, (BPM: 237.......), that I was getting were caused by my hand interfering with the back of the sensor when I was holding it.

I isolated the back part and the sensor no longer worked.

So I decided to add another amplification stage with an op amp.
Then I got the results below.

But you cannot press the sensor with your finger, you just have to touch it without applying pressure for the sensor to measure.

Below is the schematic I used.
I know that LM358 is not the best solution, but it was what I had here.

♥  A HeartBeat Happened ! 
BPM: 73
♥  A HeartBeat Happened ! 
BPM: 74
♥  A HeartBeat Happened ! 
BPM: 74
♥  A HeartBeat Happened ! 
BPM: 75
♥  A HeartBeat Happened ! 
BPM: 75
♥  A HeartBeat Happened ! 
BPM: 74
♥  A HeartBeat Happened ! 
BPM: 75
♥  A HeartBeat Happened ! 
BPM: 75
♥  A HeartBeat Happened ! 
BPM: 75
♥  A HeartBeat Happened ! 
BPM: 74
♥  A HeartBeat Happened ! 
BPM: 76

To verify which pin number corresponds to pin A0 try Serial.print(A0). Spoiler: is it not zero, and it will vary depending on the specific Arduino board you are using...

Instead, you should use

const int PulseWire = A0; 

More


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.