RASPBERRY PI | SERVO CONTROL
In this project, students will learn how to control a servo motor using a Raspberry Pi. They will understand:
- How servos work and their applications.
- How to wire a servo to a Raspberry Pi using GPIO pins.
- How to write Python code to control the servo’s position.
SECTION 1 | MATERIAL NEEDED
To complete this project, you will need:
- Raspberry Pi (any model with GPIO pins, e.g., Raspberry Pi 3, 4, or Zero W)
- Micro USB Power Supply (for powering the Raspberry Pi)
- 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)
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 (Physical Pin 2 or 4)
- Brown/Black (GND) GND (Physical Pin 6)
- Yellow/Orange (Signal) GPIO 13 (Physical Pin 33)
SECTION 3 | THE CODE
SECTION 2 | TROUBLE SHOOTING
Servo Not Moving
Install the GPIO library using:
4. Servo Moves Only Slightly
Adjust Pulse Width: Some servos may require a slightly different pulse width range (e.g., 0.5 ms to 2.4 ms instead of 0.4 ms to 2.5 ms).
Next Steps
- Check Wiring: Ensure the signal wire is connected to GPIO 13, VCC to 5V, and GND to GND.
- Power Issue: Some servos require more current than the Raspberry Pi’s 5V pin can provide. Try using an external power source with a common ground.
- Incorrect Pin Numbering: Ensure GPIO.setmode(GPIO.BOARD) is used for physical pin numbering.
- Check Power Supply: If the servo jitters, it may not be getting stable power.
- Reduce Delay Time: Use time.sleep(0.01) instead of time.sleep(0.5) for smoother motion.
Install the GPIO library using:
- sudo apt update
- sudo apt install python3-rpi.gpio
4. Servo Moves Only Slightly
Adjust Pulse Width: Some servos may require a slightly different pulse width range (e.g., 0.5 ms to 2.4 ms instead of 0.4 ms to 2.5 ms).
Next Steps
- Modify the code to move the servo in response to a button press.
- Use multiple servos for robotic applications.
- Integrate the servo into a robotic arm project.
SECTION 3 | COMBINE SERVO WITH ULTRASONICS
Here is some sample code to combine your ultrasonics with the servo motor. In this project when the distance from your ultrasonics is less than 10 the servo motor will move and then return to its original state when the distance increase.