COMPUTER SCIENCE CAFÉ
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
  • WORKBOOKS
  • BLOCKY GAMES
  • GCSE
    • CAMBRIDGE GCSE
  • IB
  • A LEVEL
  • LEARN TO CODE
  • ROBOTICS ENGINEERING
  • MORE
    • CLASS PROJECTS
    • Classroom Discussions
    • Useful Links
    • SUBSCRIBE
    • ABOUT US
    • CONTACT US
    • PRIVACY POLICY
Picture
RASPBERRY PI | SERVO CONTROL
Picture
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.
Servo motors are commonly used in robotics, automation, and precision control applications such as robotic arms, RC vehicles, and robotic joints. This project provides a foundation for using servos in larger automation projects.
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.
The PWM frequency used for servos is typically 50 Hz, meaning each cycle is 20ms long.
SECTION 3 | THE CONNECTIONS
Connect the servo motor to the Raspberry Pi’s GPIO pins as follows:

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
  • 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.
2. Servo Moves Erratically
  • 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.
3. Python Error: "ModuleNotFoundError: No module named ‘RPi’"

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.
  • ULTRASONICS WITH SERVO
<
>

    
Picture
SUGGESTIONS
We would love to hear from you
SUBSCRIBE 
To enjoy more benefits
We hope you find this site useful. If you notice any errors or would like to contribute material then please contact us.