Pulse sensor servo motor

Hello,

I am trying to make sevo motor move by pulse sensor using the code below.
The led light works but the servo motor does not work, I think I did something wrong with batteries on the board but don't know what. What seems wrong?


#include <Servo.h>

#include <PulseSensorPlayground.h>

const int PULSE_INPUT = A0;
const int PULSE_BLINK = LED_BUILTIN;
const int PULSE_FADE = 5;
const int THRESHOLD = 550;

PulseSensorPlayground pulseSensor;


Servo heart;
const int SERVO_PIN = 6;
int pos = 90;

void setup() {

  Serial.begin(115200);

  heart.attach(SERVO_PIN);
  heart.write(pos);

  pulseSensor.analogInput(PULSE_INPUT);
  pulseSensor.blinkOnPulse(PULSE_BLINK);
  pulseSensor.fadeOnPulse(PULSE_FADE);

  pulseSensor.setSerial(Serial);
  pulseSensor.setOutputType(OUTPUT_TYPE);
  pulseSensor.setThreshold(THRESHOLD);

  if (!pulseSensor.begin()) {

    for (;;) {
      digitalWrite(PULSE_BLINK, LOW);
      delay(50);
      digitalWrite(PULSE_BLINK, HIGH);
      delay(50);
    }
  }
}

void loop() {

  if (pulseSensor.UsingHardwareTimer) {

    delay(20);

    pulseSensor.outputSample();

    moveServo(pulseSensor.getLatestSample());
  } else {

    if (pulseSensor.sawNewSample()) {

      if (--pulseSensor.samplesUntilReport == (byte)0) {
        pulseSensor.samplesUntilReport = SAMPLES_PER_SERIAL_SAMPLE;
        pulseSensor.outputSample();

        moveServo(pulseSensor.getLatestSample());
      }
    }
  }
}



void moveServo(int value) {
  pos = map(value, 0, 1023, 0, 180);
  heart.write(pos);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

9V "box" battery through voltage regulator might not be sufficient power (current) supply for your servo. Feel free to post specs of your servo to have more precise opinions.

It's basic servo included in arduino starter pack, SM-S2309S, I think with spec of operating voltage between 4.8V to 6.0V

Along with the reformatted code, can you please provide an annotated schematic - showing all the components - excluding the thumb.

Yes, your servo quite likely takes 4.8-6V.

But voltage is only half the story. It also likely takes at least 650mA of current every time it tries to start moving. Possibly more. A full amp would not be out of the question.

Your PP3 9V battery is designed to supply roughly 15mA on a constant basis.

Your Arduino alone will be asking it to supply 45mA.

The LED, another 20mA.

And if that TO-220 package is a 78xx regulator, it will want another 5-8mA.

Quite simply, you have the wrong battery for the job. It's designed for smoke alarms and old fashioned transistor radios. Low current applications. Not for things like servos that need a lot of current on demand.

I followed the baisc library example pulse sensor tutorial provided for using it with servo motor and the prototype had 9v battery. Do I need to change it to bigger one then?

5 x AA battery in series through 5V voltage regulator would be better choice. Datasheet of that servo gives 800mA stall current and 250mA no load current.

Okay thanks I'll try that

If you are powering arduino separately from USB or power plug, better option is to power just servo directly with 4 x AA (6V) and skip the voltage regulator. Common GND of course.

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