Servo not working as expected

I am using a 25kg server and an IR sensor.

Here is my code:

#include <ESP32Servo.h>
 
Servo myservo;  

int input1 = 26;

bool servo_run = false;

int servoPin = 13;
 
void setup() {
	Serial.begin(115200);
	myservo.setPeriodHertz(50);    
	myservo.attach(servoPin, 500, 2500); 
	pinMode(input1, INPUT);  
}
 
void loop() {
  if(digitalRead(input1) == LOW) {
    Serial.println("Servo Activated");
    myservo.write(150);
    Serial.println("Moving to 150");
    delay(700);
    myservo.write(0);
    Serial.println("Moving to 0");
    delay(1000);
  }	
}

It functions properly sometimes, other times the servo arm will move but not come back. If I hold my hand in front of the IR sensor, the servo will do all sorts of crazy....(in out halfway, nothing etc)

Any ideas what I am doing wrong

how is input 1 wired?

Small information about your projects creates a big lists of questions to ask.
Only a few of them:
From where does your 25 kg-cm-servo get its power?
Do you use an external power-supply?
Is GND of your external power-supply connected to the GND of your ESP32?
What is the minimum input-voltage that your 25 kg-cm-servo needs to operate properly?
What is the range of pulse-width your servo can deal with?

The more precise you describe your project including datasheets and schematics the less questions must be asked back.

best regards Stefan

this code does not appear to contain anything which reads an IR sensor

assuming button is pulled up, state low will be the case only when the button is held down at the time the check is made in each loop

It is wired from pin 'd26' to the 'out' on the IR sensor. This was how I read to read an IR sensor.

Here is the sensors I am using.

repeating the questions:

Yes I am using a 5v external power supply. The power supply feeds a breadboard and all devices are linked to that breadboard(GND and PWR) including the VIN and GND on the ESP32. The tested power is 4.995 on my volt meter.

What is the minimum input-voltage that your 25 kg-cm-servo needs to operate properly? 4.8 volts
What is the range of pulse-width your servo can deal with? 500-2500 from what I found on google as it did not come with this information but found THIS data that is the same model.

Let me know if you need more information. As far as the code goes, is it correct?

Thanks

Given that your code is triggered by the motion, and the motion should complete without further interaction, but you say it does so only erratically, I have to think your servo is overloading your 5V supply when it moves, and the ESP32 is probably rebooting as a result. Two questions:
What is the maximum current demand of the servo (usually, specified as a stall current)
What is the stated output current capability of your power supply?
If the latter is not in excess of the stall current, there's your problem.

It looks like its 3A is the stall current and the PSU is rated for 40A(its a large one).

The erratic behavior can be quick little movements back and fourth, if the ESP32 was rebooting the delay between the movements would be longer I would think. Also would the LED on the ESP32 blink on reboot?

Thanks

Tell me you're not using a solderless breadboard as a power distribution point for the servo.

I think a photo of your installation/setup/whatever is in order at this point.

I am, could this be it? It is for my 11 y/o daughter's project and she cannot solder :slight_smile:

The servo-signal that comes from the ESP32 has 3.3V.
Usually servos are powered with 5V. Some of them with higher voltages.

It might be that 3.3V is only barely enough for the servo as proper input-voltage.
Hence the question what the minimum voltage is what the servo needs on the servo-input.

If you use a testcode that does simply move servo to position X wait two seconds move servo to position Y wait two seconds. Does this code show the same behaviour?

Breadboards are not made for higher currents. 100mA or 300 mA will do on a breadboard but going above one 1A is a problem.
Same thing for jumper-wires. Some jumperwires have very thing copper inside.
This means higher currents cause a significant voltage-drop across such thin-wired jumperwires.

Well so this is a opportunity to teach your daughter soldering.
Me personal I learned soldering at the age of 10 years.

Or a different solderless solution are Wago-Clamps

best regards Stefan

It certainly won't be reliable, not sure it's your only problem; it certainly could damage the solderless breadboard, which will make future projects frustrating. Provide power wired directly to the servo, and make sure that power is also referenced to the Arduino ground.

I wired the servo direct(still with jumper wires as I want them 11y/o able hehe) and first tests seemed to be working normal.

Thanks for all you guys' help, I'm new to this myself and had no idea this would be a problem

Thanks

1 Like

What does that mean???
your 11 y/o able to do what?

And what kind of meaning does the "hehe" have??

embarrassed laugh?
be a bit mischievous?
be sneaky?
be funny
to make fun of

I tend to think it's not all that.

But I can't help - but have a certain gut feeling that it might be meant that way

best regards Stefan

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