Problem to drive a servo motor with Arduino uno

Hi guys,

Please can you help me.
I have a problem when I run my code the servo motor move little bit without I touch nothing yet (any buttons).
How can I do when I run the code the servo motor doesn't move. But the servo motor can move only when I push the right or left button .

Thank you very much

here is my code:

<#include <Servo.h>

int pos=0;
Servo MonServo;


void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  
  MonServo.attach(6);
  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;}
                            }
  
}

and here is the electrical cicuit:

Welcome to the forum

When you say that the servo moves do you mean it continues to turn or does it vibrate in one spot ?

Does the servo move through 180 degrees or through a larger angle ?

Do not try to power motors or servos from the Arduino 5V output, as that can damage the Arduino or cause it to malfunction. Ignore any tutorials that recommend otherwise.

A 4xAA battery pack will power 1 or 2 small servos like SG-90. Don't forget to connect the grounds.

2 Likes

@norddist The code works fine, you probably have something connected incorrectly, most likely one of the pushbuttons.

Hi, @norddist
Welcome to the forum.

I think what you are trying to say is when you power up your project the servo twitches BEFORE you press any buttons.

Tom.... :smiley: :+1: :coffee: :australia:

Your code does not compile because of the stray "<":

<#include <Servo.h>
^

Try it now:

#include <Servo.h>

int pos = 0;
Servo MonServo;

void setup()
{
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  MonServo.write(0);
  MonServo.attach(6);
}

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;
    }
  }
}

Thank you for your advice,
I will follow.
For the moment I just simulate by Tinkercard the oline wesite.

I tried too by an external supply but it's still same probleme.

Yes exactly,
As soon as I aunch the code the servi motor twitch just littlbit about 1 or 2 degres and stop.
Then if I push one button right or left it move like I want.

Hello
Thank you :blush:.
Like a guys said the servo motor twitches little bit (it moves of 1 or 2 degres then stop) when I Iaunch the code.

The code is ok,
I just done a mistake when I published my topic here.

I tried my project like you see,
I tried by the online website Tinkercad not yet for real.

That may be the way Tinkercad works.
Is it a problem?

Reverse the two servo lines - try this:

  MonServo.write(0);
  MonServo.attach(6);

By presetting the position, then attaching, you ensure that the servo doesn't attempt to go to a 'garbage' position(random memory value), followed almost instantly by a move to zero. Probably wouldn't see it with real hardware, but simulators have a way of exposing 'edges' like this.

This happens with real hardware, too, by the way, and scales with the amount of code between the attach statements and the first write statements for each servo. For example, code with several servo attach statements, followed by some analog manipulation to read initial settings, followed by servo writes to set first positions, will likely have servos twitching all over.

Why? The default position within Servo.h for any servo being attached is 0(but it isn't explicitly set as that...*); if you want your initial servo position to be non-zero, you have to do a write before the attach, to establish that non-zero setting.

    • and consequently, if you 'reset' your Arduino, without a power cycle, you may find a non-zero value for the first servo position, even if you don't write one. You can do a Servo.read() to determine if that's happening, or just do the initial Servo.write().

So for the simulation it's not a problem for me but I hopes it does not same in real.
This evening I will the circuit in real then I reply you if it's same in real.

Why would that be an issue?
Do you plan on turning the Arduino on and off many times in your real world application?

Because it's not normal for me,:grin:
This example is only for my learning of Arduino.

Simulators like Tinkercad don't always work the same as real hardware. You can't burnout a simulator if you make a short circuit and they will often still work OK even if you exceed the voltage and current ratings of the Arduino.

Wokwi simulates it perfectly.

1 Like

Now, including carrying over the actual physical position if you restart the simulation; so, if your code has the servo at 175, you stop and restart the simulation, and set the position to 0, you can expect the servo to do exactly that. Very useful, as it is exactly how the hardware will perform - for better or worse.

The default angle when first connected is set at 90 by the Servo library, not some 'garbage' position. Writing an angle to be servo before attaching it overrides the default setting