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

1a
  • 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

2a
  • 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
  {
    counter++;
    delay(150);
  }
  if(counter == 0)
    servo.write (20);  // zero degrees
  else if(counter == 1)
    servo.write(90);
  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
  else
   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!

Share

    Recommendations

    • Optics Contest

      Optics Contest
    • Plastics Contest

      Plastics Contest
    • Make it Glow Contest 2018

      Make it Glow Contest 2018

    13 Discussions

    0
    None
    RajeshB58

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

    Capture_1.JPGCapture_2.JPG
    0
    None
    MichaelK605

    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.

    0
    None
    JuanU13

    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 juangiluruguay@hotmail.com

    0
    None
    LemuelD4

    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

    1
    None
    sniperno1

    10 months ago

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

    0
    None
    DuckR1

    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
    0
    None
    UnicornClockworksDuckR1

    Reply 10 months ago

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

    0
    None
    DuckR1UnicornClockworks

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

    0
    None
    DuckR1DuckR1

    Reply 10 months ago

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

    That was almost too easy xP
    Thank you anyway!

    0
    None
    pottymouth_480

    11 months ago

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

    1 reply
    0
    None
    UnicornClockworkspottymouth_480

    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:

    servo1.attach(servo1pin#);

    servo2.attach(servo2pin#);

    hope that makes sense.

    0
    None
    mireiem

    1 year ago

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