Servo Motor + Push Button + Arduino

About: I love to make projects based on microcontrollers including robots and IoT technologies. Absolutely, truly, madly, deeply in love with planes. I also dabble in the art of Origami and play chess in my spare ...

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!



    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    13 Discussions


    Question 8 weeks ago

    I'm new with arduino... Anyone can help me regarding arduino code to run two servo motors both in the clockwise and anticlockwise directions controlled by two DPDT switches..???


    3 months ago on Step 3

    for the wiring up the Servo, you say in your description (and the code) to hook up the blue wire to pin 3 of the arduino, but in your wiring diagram, you have that particular wire from the servo hooked up to pin 9.

    took me a minute to figure out why the servo was not working.


    6 months ago on Introduction

    Hey, hello!!!.

    First of all, I don't know why nobody has written a comment yet in YouTube.

    Second... Its almost a plug and play code for what I am looking for!!!!, and I am going to tell you why, and.. you are going to be surprised!!

    Flir released a thermal camera designed for drones, called FLIR VUE PRO.

    Has 2 PWM inputs to control 2 functions of the camera.

    For 1 of them, zoom, has 2 stages for zoom in/out (1ms, and 2ms), and for the second Channel, needs 3 stages to control 3 different Pallets (1ms, 1.5ms, 2ms) at 50HZ, values that match the industry standards for servos on RC Toys, and is used to control the camera via receiver PWM outputs in the drone.

    Now.. I want to be able to use it in a hand held device (besides a drone), but need to use push buttons for inputs (1 per channel), and PWM outputs with "servos values" to the camera.

    The only thing I have to figure out now, is the code to be used with 2 push buttons, and 2 servos.

    If you could help me with that, I would TRIPLE appreciate it, since I already own you a double THANKS!!

    Regards Juan


    Question 8 months ago on Step 5

    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


    10 months ago

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


    10 months ago

    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?

    3 replies

    Reply 10 months ago

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


    Reply 10 months ago

    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...


    Reply 10 months ago

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

    That was almost too easy xP
    Thank you anyway!


    11 months ago

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

    1 reply

    Reply 10 months ago

    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.


    1 year ago

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