Lesson 88: Controlling a servo motor using two push button switches
This project demonstrates how to control a servo motor using two push buttons with an Arduino. Instead of relying on a potentiometer or a serial command, you can use simple tactile switches to manually move a servo to the left or right. This is incredibly useful for applications where you need direct, physical control over a mechanism, such as building a robotic arm, adjusting a camera gimbal, or creating a remote-controlled pan-and-tilt system. The buttons allow you to incrementally adjust the servo's angle, with the ability to hold the button to sweep it across its full range of motion.
Here are a few practical projects you can build with this setup:
- Building a manual control panel for a robotic arm or gripper.
- Creating a remote-controlled camera slider or pan-tilt head.
- Developing a physical control interface for a model airplane or boat rudder.
- Making a simple "teach pendant" to record positions for a CNC machine or 3D printer.

Hardware and Components
This project requires only a few basic components, making it an excellent starting point for learning about both input and output devices with the Arduino.
- Arduino board (Uno, Nano, Mega, etc.)
- Servo motor (e.g., SG90 or MG995)
- Two momentary push buttons
- Jumper wires
- Breadboard (optional but recommended)
Wiring Guide
In the video, the wiring is straightforward. The servo motor's signal pin is connected to a PWM-capable pin on the Arduino, which is pin 9 in this example. The power and ground for the servo are connected to the 5V and GND pins of the Arduino. The two push buttons are wired in a similar fashion to each other. One leg of each button is connected to the ground, and the other leg is connected to a digital input pin on the Arduino (pin 2 for the "left" button and pin 3 for the "right" button). The code uses the internal pull-up resistors, so no external resistors are needed for the buttons. (in video at 00:57)

Code Explanation
The code is designed to be easily configurable. The key user-configurable parts are at the top of the sketch. Here is a breakdown of the important variables and definitions you might want to adjust for your own project.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int angle =90; // initial angle for servo
int angleStep =5;
#define LEFT 2 // pin 2 is connected to left button
#define RIGHT 3 // pin 3 is connected to right button
servoPin: This variable defines the Arduino pin to which the servo's signal wire is connected. It is crucial to use a pin that supports Pulse Width Modulation (PWM), typically denoted with a~symbol on the board.angle: This sets the initial angle of the servo when the Arduino starts. The default value is 90 degrees, which typically centers the servo.angleStep: This is the increment by which the servo angle changes each time a button is pressed. A value of 5 means each press will move the servo by 5 degrees. You can increase this for faster movement or decrease it for finer control.LEFTandRIGHT: These#definedirectives assign the digital pins that the push buttons are connected to. If you wire your buttons to different pins, you must change these values to match your setup.
The core logic in the loop() function checks if a button is pressed (by reading a LOW signal due to the pull-up configuration). If the "right" button is pressed, it decreases the angle variable by angleStep. If the "left" button is pressed, it increases the angle. The code includes boundary checks to ensure the angle stays within the 0 to 180-degree range. The myservo.write(angle) function is then used to command the servo to move to the new position.
Live Project Demonstration
In the video demonstration, the functionality is clearly shown. Pressing and holding the "left" button causes the servo to sweep smoothly to the left, with the angle incrementing by 5 degrees at a time, until it reaches 180 degrees and stops. Similarly, pressing and holding the "right" button makes the servo sweep back to the right, decreasing the angle until it reaches 0 degrees. The current angle is printed to the Serial Monitor, allowing you to see the exact position of the servo in real-time. (in video at 05:20)
Video Chapters
- [00:00] Introduction to controlling a servo with buttons
- [00:57] Wiring guide for the servo and push buttons
- [02:12] Code explanation, part 1: Setup and configuration
- [03:25] Code explanation, part 2: The main loop logic
- [05:20] Live demonstration of the project
- [06:23] Conclusion and potential applications
/*
* S05-05 Servo control with 2 push buttons
* Download and resource page https://robojax.com/RJT764
Watch video on YouTube https://www.youtube.com/watch?v=J_kbyAY1rLM
*
* Written by Ahmad Shamshiri for Robojax.com
* on Jan 15, 2019 at 19:15 in Ajax, Ontario, Canada
* This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been download from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
Controlling a servo with two Push buttons with Arduino
when Left push button is pressed, the servo start moving to the left until it reaches 180( or zero) degrees
when Right push button is pressed, the servo start moving to the right until it reaches 180( or zero) degrees
At any instance if the button is released, servo stops
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int angle =90; // initial angle for servo
int angleStep =5;
#define LEFT 2 // pin 2 is connected to left button
#define RIGHT 3 // pin 3 is connected to right button
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
pinMode(LEFT,INPUT_PULLUP); // assign pin 12 ass input for Left button
pinMode(RIGHT,INPUT_PULLUP);// assing pin 2 as input for right button
myservo.write(angle);// send servo to the middle at 90 degrees
Serial.println("Robojax Servo 2 Buttons ");
}
void loop() {
// Servo button demo by Robojax.com
while(digitalRead(RIGHT) == LOW){
if (angle > 0 && angle <= 180) {
angle = angle - angleStep;
if(angle < 0){
angle = 0;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(100); // waits for the servo to get there
}// while
// Servo button demo by Robojax.com
while(digitalRead(LEFT) == LOW){
// Servo button demo by Robojax.com
if (angle >= 0 && angle <= 180) {
angle = angle + angleStep;
if(angle >180){
angle =180;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(100); // waits for the servo to get there
}//
}
Things you might need
-
Amazon
-
Amazon
-
Amazon
-
AmazonServo motor on Amazonamzn.to
-
eBay
Resources & references
Files📁
Other files
-
SG90 Servo motor datasheet
robojax-servo-SG90_datasheet.pdf0.12 MB