Working with heart rate sensor

I am trying to use the servo motor with heart rate sensor but the latter outputs values even when I am not touching the sensor.

The code:

#include <Servo.h>

#define USE_ARDUINO_INTERRUPTS true    // Set-up low-level interrupts for most acurate BPM math
#include <PulseSensorPlayground.h>     // Includes the PulseSensorPlayground Library
const int PulseWire = 0;       // 'S' Signal pin connected to A0
const int LED13 = 13;          // The on-board Arduino LED
int Threshold = 550;           // Determine which Signal to "count as a beat" and which to ignore
int distance;                               
PulseSensorPlayground pulseSensor;  // Creates an object
Servo servo;
long duration;

void setup() {
  Serial.begin(9600);
  servo.attach(12);
  servo.write(0);
  // Configure the PulseSensor object, by assigning our variables to it
  pulseSensor.analogInput(PulseWire);   
  pulseSensor.blinkOnPulse(LED13);       // Blink on-board LED with heartbeat
  pulseSensor.setThreshold(Threshold);   

  // Double-check the "pulseSensor" object was created and began seeing a signal
  if (pulseSensor.begin()) {
    Serial.println("PulseSensor object created!");
  }
}

void loop() {
  int myBPM = pulseSensor.getBeatsPerMinute();      // Calculates BPM

  if (pulseSensor.sawStartOfBeat()) {               // Constantly test to see if a beat happened
    Serial.println("♥  A HeartBeat Happened ! "); // If true, print a message
    Serial.print("BPM: ");
    Serial.println(myBPM);                        // Print the BPM value
    }

  delay(20);

  for (int i=0; i<180; i++){
      servo.write(i);
      //usdist();
      delay(5);
    }

    for (int i=180; i>0; i--){
      servo.write(i);
      //usdist();
      delay(5);
    }
}

When I test the heart rate sensor separately, it works fine that is only when I place it on my skin

I need help, please

The first thing to do is to comment out all the servo handling code in the loop which appears anyway to be active irrespective of the state of the heart beat sensor.
Then start playing with parameters like Threshold which is currently set at 550.

Hmm the servo motor and the heart beat sensor should run independent of each other

Any time you are coding a project it is best to make separate sketches for each component and then combine at the end. Otherwise your code gets large, messy and hard to debug. You should end up with lots of iterations each with their own well named sketch. You should always have at least the last working iteration and the current iteration under development. Bugs are located in the difference between these two.
If coding well with encapsulation then combining should be easy and lead to clear, legible code

1 Like

Assuming that you write the code yourself. :slight_smile:

So the servo works separately and the heart beat sensor works separately.
However, when you combine these in the same sketch, the heart beat sensor delivers spurious readings.
Is that correct ?

It could be an inadequate power supply, poor decoupling etc. Supply a schematic diagram of your set up and a link to your heart beat sensor.

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