小编
Published2025-10-15
Introduction to Servo Motors and Arduino
Servo motors are widely used in robotics, automation, and a variety of DIY electronics projects because of their precise control over angular position. Whether you're working on a robotic arm, an automated camera mount, or a simple mechanical hand, a servo motor is often the best choice for providing accurate rotational movement. But how do you control a servo motor using an Arduino board? It’s easier than you might think, and the addition of a button interface opens up endless possibilities for hands-on interaction with your project.

In this article, we’ll explore how to combine a servo motor with a button to control its movement through an Arduino program. We'll take you step by step through the process of setting up the circuit, writing the code, and troubleshooting common issues.
A servo motor is a small, powerful motor that can be precisely controlled to rotate within a specific range of motion—typically from 0° to 180°. Unlike standard motors that rotate continuously, a servo motor only moves within a defined range, making it perfect for applications where precision is key. Most servo motors have a control wire that receives PWM (Pulse Width Modulation) signals, which determine the angle the motor should rotate to.
Why Use a Button with a Servo Motor?
While you could control a servo motor using a potentiometer, joystick, or other input devices, the button interface is a simple and effective way to interact with the motor. Buttons can trigger specific actions, making them ideal for toggling between positions or triggering predefined events like opening a door, turning a knob, or adjusting a camera angle.
For instance, pressing the button could rotate the servo motor to one angle, while releasing it might return the servo to its starting position. The beauty of this system is in its simplicity—just a push of a button can create a dynamic, interactive experience with minimal coding and hardware setup.
The Components You'll Need
Before you dive into the circuit and code, let’s list out the components you’ll need:
Arduino Board (e.g., Arduino Uno): The brain of your project. It will control the servo and read the button input.
Servo Motor: A small motor that can rotate to a specified angle.
Pushbutton: The simple on/off switch to control the motor.
Breadboard: To easily make temporary connections without soldering.
Jumper Wires: To make the electrical connections.
Resistor (10k Ohm): This is for the pull-down resistor to stabilize the button’s state.
External Power Supply (Optional): If your servo motor requires more current than the Arduino can provide, an external power supply may be necessary.
Once you have your components ready, it's time to set up the hardware.
The setup is fairly simple, and you’ll only need a few components to get started. Here’s how you can connect everything:
Connect the red wire of the servo to the 5V pin on the Arduino.
Connect the brown or black wire to the GND pin on the Arduino.
Connect the yellow or orange control wire to a PWM-capable pin on the Arduino, such as Pin 9.
One terminal of the button connects to Pin 7 on the Arduino.
The other terminal connects to GND.
Use a 10k Ohm resistor between the button and the GND to ensure the input is stable (this acts as a pull-down resistor).
Once the circuit is set up, we can move on to writing the Arduino code that will control the servo motor’s movements based on button presses.
Writing the Code and Testing the Button-Controlled Servo
Step 1: Arduino Code Structure
Now that your hardware is set up, it's time to write the code for the Arduino. The main task of the code is to monitor the state of the button and, depending on whether it’s pressed or released, move the servo motor to a specific angle. We’ll use digitalRead() to check the button’s state and servo.write() to control the motor's position.
Here's the basic structure of the code:
#include // Include the Servo library
const int buttonPin = 7; // Pin connected to the button
const int servoPin = 9; // Pin connected to the servo control wire
// Variables to hold button state
// Initialize the button pin as an input
pinMode(buttonPin, INPUT);
// Attach the servo to the pin
myServo.attach(servoPin);
// Initialize the servo position
myServo.write(0); // Start the servo at 0 degrees
// Set up serial monitor for debugging
// Read the button state
buttonState = digitalRead(buttonPin);
// If the button is pressed (LOW)
if (buttonState == LOW && lastButtonState == HIGH) {
// Move servo to 90 degrees
Serial.println("Button pressed, servo moved to 90 degrees.");
// If the button is released (HIGH)
else if (buttonState == HIGH && lastButtonState == LOW) {
// Move servo back to 0 degrees
Serial.println("Button released, servo moved back to 0 degrees.");
// Save the current button state for next iteration
lastButtonState = buttonState;
delay(50); // Debounce delay
Step 2: Upload and Test the Code
Upload the Code to Arduino: Connect your Arduino to your computer, select the correct board and port in the Arduino IDE, and click the upload button.
Open the Serial Monitor: Once the code is uploaded, open the Serial Monitor in the Arduino IDE (Tools > Serial Monitor). This will display messages each time the button is pressed or released.
Press the Button: When you press the button, the servo should rotate to 90 degrees. When you release the button, the servo should return to 0 degrees.
Button Not Working: Ensure your button wiring is correct and the pull-down resistor is connected to GND. If the button state is fluctuating, you may need to adjust the debounce delay.
Servo Not Moving: Double-check that the servo control wire is properly connected to the correct PWM pin on the Arduino. Also, verify that the servo is receiving enough power.
Button Bounce: Sometimes buttons can "bounce," causing multiple false triggers. If you notice this, you may need to implement a more sophisticated debouncing method, either in hardware or software.
By following these simple steps, you can easily control a servo motor using a pushbutton and an Arduino. This basic setup forms the foundation for more complex projects, such as robotic arms, automated systems, or any other device where precise rotational control is necessary.
This guide demonstrates that with just a few components and some straightforward code, you can create interactive, motor-driven devices that respond to user input. The simplicity of the button interface makes it a great choice for beginners, while the expandability of the concept allows more advanced users to explore a wide range of applications. So, get creative and start experimenting with servo motors and Arduino today!
Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.