Servo Motor + Push Button + Arduino





Introduction: Servo Motor + Push Button + Arduino

Hello World!

Welcome back to another Unicorn ClockWorks Instructable. Here, we will be setting up and interfacing a servo motor to the Arduino and controlling it using a push button switch. The servo motor is a very popular and useful tool among hobbyist and I am sure you will find yourself needing to use it in numerous occasions. As such, we will be learning how to interface control it!

This is made to be broad ranging and generic so you can apply it to any projects with any microcontroller!

Step 1: Materials

  • Arduino or any Arduino-compatible microcontroller
  • Servo motor
  • Push button switch
  • 10k Ohm Resistor - brown-black-orange

Step 2: Push Button Connections

  • The Black wire connects pin 1 of the switch (on the left hand side) to the GND pin on the Arduino.
  • The Yellow wire connects pin 2 of the switch (on the right-hand side) to pin 8 on the Arduino.
  • Connect a 10kΩ resistor -- brown-black-orange -- to pin 2 and the other terminal to the 3.3V pin on the Arduino.

Step 3: Servo Connections

  • The Blue wire is connected from the signal pin of the servo motor to pin 3 of the Arduino.
  • The Red wire is connected from the signal pin of the servo motor to the 5V pin of the Arduino
  • The Black wire is connected from the ground pin of the servo motor to the GND pin of the Arduino. Note that the switch is also connected here. so we can also connect it there on the breadboard, but the Arduino has multiple GNDs that are all common.

Step 4: Code

#include <Servo.h>;
 // pushbutton pin
 const int buttonPin = 8;
 // servo pin
 const int servoPin = 3;
 Servo servo;
//create a variable to store a counter and set it to 0
int counter = 0;
void setup()
  servo.attach (servoPin);
  // Set up the pushbutton pins to be an input:
  pinMode(buttonPin, INPUT);
void loop()
 // local variable to hold the pushbutton states
  int buttonState;  
  //read the digital state of buttonPin with digitalRead() function and store the           //value in buttonState variable
  buttonState = digitalRead(buttonPin);
  //if the button is pressed increment counter and wait a tiny bit to give us some          //time to release the button
  if (buttonState == LOW) // light the LED
  if(counter == 0)
    servo.write (20);  // zero degrees
  else if(counter == 1)
  else if(counter == 2)
    servo.write (150); 
  else if(counter == 3)  
    servo.write (180);
  //else reset the counter to 0 which resets thr servo to 0 degrees
   counter = 0;

Step 5: It Moves!

Push the button and watch the servo move to a different angle each time.

I hope you enjoyed making this project and learned something along the way!

Unicorn Clockworks Out!



    • Epilog Challenge 9

      Epilog Challenge 9
    • Paper Contest 2018

      Paper Contest 2018
    • Trash to Treasure

      Trash to Treasure

    We have a be nice policy.
    Please be positive and constructive.



    I want a code where when I press the button(single click) the servo comes to 0 and when i press again it goes to 90 irrespective oh how i press the button


    How Would you program it so that when you press one button it moves to 0 and another moves to 180?

    Hi, your code was very helpfull for me. I have got an MGS Servo, he just doesn't move normal... Sometimes he goes fast to 0 and then he stucks or is really slow to reach 90, 150, 180..?

    Do you know why the servo behaves like this?

    You have the same code right? Double check your wires and if there are any loose connections.

    Yes, i even tried it with another code with a potentiometer. Result: The Servo works if i turn the potentiometer really slow, if i just move to fast, the Servo is kinda waiting, maybe a little bit jittering....
    I checked for loose connections, found nothing...

    Got it! I need an external Power Supply for my Servo...

    That was almost too easy xP
    Thank you anyway!

    question if you don't mind, but how can you use more servo's in the program?

    Same way you do for 1 servo, except now you would attach more servos. i.e.
    Servo servo1;

    Servo servo2;

    in setup:



    hope that makes sense.

    Hello, I would like to know what's the code for 3 servomotors