ARDUINO | SERVO CONTROL
In this project, students will learn how to control a servo motor using a Arduino. They will understand:
- How servos work and their applications.
- How to wire a servo to a Arduino using GPIO pins.
- How to write C code to control the servo’s position.
SECTION 1 | MATERIAL NEEDED
To complete this project, you will need:
- Arduino Uno (or another model)
- USB Power Supply (for powering and downloading to the Arduino)
- Servo Motor (SG90 or MG996R recommended)
- Breadboard (optional but useful for stable connections)
- Jumper Wires (Male-to-Female for easy connections)
- Resistor (Optional, 330Ω-1kΩ) (for signal stabilization if needed)
- IF your servo motors draws a lot of current then you will need an external 5v power supply
SECTION 2 | THE PRINCIPLES
How Servo Motors WorkServo motors are closed-loop control devices, meaning they move to a specified angle based on a control signal. They receive PWM (Pulse Width Modulation) signals, where:
- A 0.4 ms pulse moves the servo to the left position.
- A 2.5 ms pulse moves the servo to the right position.
- A middle position is calculated as the average of the two.
SECTION 3 | THE CONNECTIONS
Connect the servo motor to the Raspberry Pi’s GPIO pins as follows:
Servo Wire Connection
Servo Wire Connection
- Red (Vcc) - 5V on Arduino - Powers the servo motor
Brown/Black (GND) - GND on Arduino - Common ground
Orange/Yellow (Signal) - Pin 9 - PWM signal to control position
SECTION 4 | THE CODE
SECTION 5 | TROUBLE SHOOTING
- Servo doesn't move - Check power and GND wires are connected correctly
- Servo jitters or buzzes - Avoid using USB power — use external 5V supply
- Servo moves randomly - Ensure the signal wire is firmly connected to the correct pin
- Code uploads but nothing happens - Use Serial.begin(9600); and debug with Serial.println()
- Servo not moving to full range - Some servos can't physically rotate full 180° — try smaller values like 10°–160°
SECTION 6 | COMBINE SERVO WITH WITH BUTTON PRESS
In this project, we add a button that, when pressed, toggles the servo motor between different positions. This is a great learning step toward understanding how to control a servo motor using external inputs.
Once you’ve mastered this, the same principles can be applied to other types of input, such as temperature sensors, light-dependent resistors (LDRs), or motion detectors. For example, this concept can be extended to build a solar tracker that uses LDRs to rotate a solar panel and follow the sun throughout the day.
Once you’ve mastered this, the same principles can be applied to other types of input, such as temperature sensors, light-dependent resistors (LDRs), or motion detectors. For example, this concept can be extended to build a solar tracker that uses LDRs to rotate a solar panel and follow the sun throughout the day.
Button Connections
In this example we add pull-up to the code.
Why is pull-up needed?
A pull-up resistor is needed to ensure the input pin reads a defined HIGH voltage when the button is not pressed.
Without it, the pin could "float", meaning it might randomly read HIGH or LOW due to electrical noise. Using INPUT_PULLUP activates the Arduino’s internal resistor, keeping the pin HIGH until the button connects it to GND, making it LOW when pressed.
In short:
Pull-up resistors prevent false readings by giving the pin a stable default state (HIGH).
- One side - Pin 2
- Other side - GND
In this example we add pull-up to the code.
Why is pull-up needed?
A pull-up resistor is needed to ensure the input pin reads a defined HIGH voltage when the button is not pressed.
Without it, the pin could "float", meaning it might randomly read HIGH or LOW due to electrical noise. Using INPUT_PULLUP activates the Arduino’s internal resistor, keeping the pin HIGH until the button connects it to GND, making it LOW when pressed.
In short:
Pull-up resistors prevent false readings by giving the pin a stable default state (HIGH).
SECTION 7 | THE CODE
SECTION 8 | WHAT IS PWM
Pulse Width Modulation (PWM) is a technique to control analog-like behavior using digital signals.
Key Ideas:
Example PWM Waveform:
Key Ideas:
- A digital pin can only be HIGH (5V) or LOW (0V).
- By turning the signal on and off very fast, you can simulate voltages between 0V and 5V.
- The "on-time" is called the pulse width.
- Servo motors use PWM to determine the angle:
- A short pulse (1ms) moves it to 0°
- A medium pulse (1.5ms) moves it to 90°
- A long pulse (2ms) moves it to 180°
- The Arduino Servo library handles this PWM signal for you!
Example PWM Waveform:
- AnglePulse Width - Approx.0°1ms
- 90° 1.5ms
- 180° 2ms