Problem to drive a servo motor with Arduino uno

Interesting, not my recollection of my experience at one time; that was 3 years ago. However, I see it in the code. Curious. Thanks for that.

Regardless, it's far better to set the position before attaching, unless 90 is your desired starting point. Because you will see a twitch, as our OP is seeing, when 90 is applied, then 0 is applied, as will happen when the servo interrupt wakes and applies that initial 90, then applies his 0 in the next 20 ms interval.

ok thank you,
I will try with Wokwi.

it doesn't work, it's still same.

Please post the exact code you tried. "it doesn't work" isn't sufficient.

What doesn't work, a motor or a simulation? My advice was for a physical (REAL) motor.
If I had known you were playing with a "sim", I wouldn't have replied.

Give Wokwi a try:

1 Like

You are correct Tinkercad moves it a little before it starts.
Try this in you original code

MonServo.attach(6, 500, 2000);

I used this diagram

and this code

#include <Servo.h>

int pos = 0;
Servo MonServo;


void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

  MonServo.attach(6, 500, 2000);
  MonServo.write(0);
}


void loop()
{
  if (digitalRead(3) == LOW)
  {
    MonServo.write(pos);
    pos += 1;
    delay(100);
    if (pos > 180) { pos = 180; }
  }

  if (digitalRead(2) == LOW)
  {
    MonServo.write(pos);
    pos -= 1;
    delay(100);
    if (pos < 0) { pos = 0; }
  }

  delay(10);
}

in Tinkercad and works OK

hi
thank you so much,
yeeeees it works in WokWi. :folded_hands: :folded_hands: :folded_hands:
You definitly solve my problem.
I think from now I will work only with Wokwi.
Thank again.
Thank you everyone too. :folded_hands: :folded_hands:

As I show, it also works in Tinkercad if you do it right.
Actually Tinkercad may be closer to how a real servo works.

Ok sir,

I will try your code and circuit this evening as soon as I enter home.

Thank you so much.:folded_hands:

You don't have to but it is interesting that the servo jitters like a real one would. I don't know if that was done on purpose or it's a bug in the simulator.
If you like wokwi stay with it but I think Tinkercad has more components you can use.

I just tried,
the servo motor doesn't move at the beginning like before, the bug is fixed,
but when I push button plus to move to 180 degres, it stops at about 140 degres,
not possible to go until 180 !!! it's strange.
But it's ok for me,
I give up definitly Tinkercad.
I don't want to lose my time in bugs.
But thank you sir to try to help me I appreciate.

  • Some servos cannot go to 180° :sob:

  • You can try to tune your particular servo to see if the maximum angle can be increased.

Servo.attach(6, minPulseWidth, maxPulseWidth);

  • min (optional): the pulse width, in microseconds, corresponding to the minimum (0 degree) angle on the servo (defaults to 544)
  • max (optional): the pulse width, in microseconds, corresponding to the maximum (180 degree) angle on the servo (defaults to 2400)

Example:

myservo.attach(7, 500, 2500);

FYI

  • Also look into servo.writeMicroseconds(us) too.
    1000 is fully counter-clockwise, 2000 is fully clockwise, and 1500 is in the middle.

If your code still has this statement, from upthread,

Then yes, the angle will be limited in the library to much less than 180.

I was playing with the numbers, the 2000 should be set to 2500. MonServo.attach(6, 500, 2500);

The numbers correspond to the minimum and maximum pulse width in microseconds. For most 180 degree servos 500 coresponds to 0 degrees and 2500 to 180 degrees and 1500 to 90 degrees. You can also use .writeMicroseconds( value) to move the servo
The numbers may be different for diferent servos.

Not on the planet where I live.

1000 to 2000 is more or less standard for cheap r/c servos.

Arduino servo documentation says

On standard servos a parameter value of 1000 is fully counter-clockwise, 2000 is fully clockwise, and 1500 is in the middle.

they go on to say

Note that some manufactures do not follow this standard very closely so that servos often respond to values between 700 and 2300.

a7

To me, this indicates the servo has 181 degrees (0 to 179 + 1). Does the 181st degree register differently than 180 on an oscope and to the servo?

Should it be...

if (pos > 179) { pos = 179; }

If code talks, it says servos have 181 positions numbered 0 .. 180.

I've never seen anyone avoid setting a servo angle to 180.

This code from servo.h

void Servo::write(int value)
{
  if(value < MIN_PULSE_WIDTH)
  {  // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
    if(value < 0) value = 0;
    if(value > 180) value = 180;
    value = map(value, 0, 180, SERVO_MIN(),  SERVO_MAX());
  }
  this->writeMicroseconds(value);
}

leaves only the perennial question of exzctly how bad using map() sucks in this case. Probably not too much.

a7

You right but I'm still not sure what planet you are from.