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

Participated in the
Makerspace Contest 2017
18 Discussions
Question 4 months ago on Step 4
Hi I'm new to arduino uno and programming,
I have uploaded the code, have attached the arduino uno to the laptop for power, but when I push the button, nothing is happening?
Answer 4 months ago
Thanks, but I got the answer, I had connected the yellow wire of servo to ~9 instead of ~3😅
9 months ago
hallo, i got a problem in my project. can you tell me how to use push button + HCSR04 + Arduino Uno ? please help me.
this my code :
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HCSR04.h>
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
const int buttonPin = 5;
int buttonState = 0;
UltraSonicDistanceSensor distanceSensor(2, 3);
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup()
{ lcd.init();
lcd.backlight();
pinMode(buttonPin, INPUT);
Serial.begin(9600);
while (!Serial) {
;
}
Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
Serial.println("card initialized.");
}
void loop()
{
int j = distanceSensor.measureDistanceCm();
buttonState = digitalRead(buttonPin);
if (buttonState == 1) {
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print("Jarak,");
dataFile.print(j);
dataFile.println("cm");
delay(3000);
dataFile.close();
Serial.print("Jarak,");
Serial.print(j);
Serial.println("cm");
delay(3000);
//lcd
lcd.setCursor(0, 0);
lcd.print("D =");
lcd.setCursor(3, 0);
lcd.print(j);
lcd.setCursor(6, 0);
lcd.print("cm");
delay(3000);
lcd.clear();
}
}
else {
Serial.println("Press Button");
lcd.setCursor(2, 0);
lcd.print("Press Button");
}
}
Question 2 years ago on Introduction
can i use
use adruino uno?
Question 2 years 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..???
Answer 2 years ago
Same as the push button switch except you are measuring the voltage coming out of the pins of this switch. So one of them is hooked up to 3.3V and the rest is hooked up to GND and you're reading the voltage with a voltage divider setup
2 years 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.
Question 2 years ago on Introduction
Circuit diagram to understand little more
2 years 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
Question 2 years 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
3 years ago
How Would you program it so that when you press one button it moves to 0 and another moves to 180?
3 years 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?
Reply 3 years ago
You have the same code right? Double check your wires and if there are any loose connections.
Reply 3 years 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 3 years ago
Got it! I need an external Power Supply for my Servo...
That was almost too easy xP
Thank you anyway!
3 years ago
question if you don't mind, but how can you use more servo's in the program?
Reply 3 years 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.
3 years ago
Hello, I would like to know what's the code for 3 servomotors