Skip to main content

Build. Learn.
Innovate.

Controlling a Servo Motor

Written By:

Published on:

Project Overview

This project teaches students how to control a servo motor using the ESP32 microcontroller and MicroPython. Students will learn how PWM signals correspond to angular movements and how to control the servo to move to specific angles. This is commonly used in robotic arms, steering mechanisms, and automated systems.

Educational Goals

  • Understand how PWM signals control servo motors
  • Write code to move the servo to specific angles
  • Implement functions to sweep, position, or reset the servo
  • Explore how servos are used in real-world applications

Detailed Parts List

  • 1x ESP32 Dev Board (36-pin)
  • 1x SG90 or MG995 Servo Motor
  • External power supply (recommended for MG995)
  • Jumper wires
  • Breadboard (optional for basic testing)

Circuit Diagram Description

  • Servo has 3 pins: Signal (orange), VCC (red), GND (brown/black)
  • Connect signal to ESP32 GPIO pin (e.g., GPIO 23)
  • Connect VCC to 5V (external power recommended for MG995)
  • Connect GND of servo and ESP32 together

ESP32 GPIO23 —> Signal (Servo)
ESP32 5V or Ext PSU —> VCC (Servo)
ESP32 GND —> GND (Servo)

Software Functionality

  • Uses PWM to control servo angle
  • Allows rotation to a specified angle between 0° and 180°
  • Sweeps servo from one side to the other

Web Interface Features (Optional Extension)

  • Slider to select angle (0–180°)
  • Button to reset to center
  • Real-time angle display

Implementation Steps

  1. Wire the servo motor to ESP32 as per diagram
  2. Flash MicroPython firmware to ESP32
  3. Upload and run “** using your IDE**
  4. Test servo motion and adjust angle values
  5. (Optional) Extend control to a web interface

Extensions and Challenges

  • Add multiple servos for robotic arms
  • Combine with buttons or sensors for dynamic movement
  • Create motion sequences or animation routines
  • Add OLED to display current angle

Troubleshooting Guide

  • Servo jittering or weak: Use external 5V power
  • No movement: Confirm signal pin and ground wiring
  • Overheating or buzzing: Avoid holding servo in extreme angles
  • Servo goes beyond 180° or rotates erratically: Recheck pulse calculations

Project Code

from machine import Pin, PWM
from time import sleep

# Setup PWM on GPIO 23
servo = PWM(Pin(23), freq=50)

# Helper function to set angle
# Servo expects duty between ~40 (0°) and ~115 (180°)
def set_angle(angle):
duty = int((angle / 180) * 75 + 40)  # scale 0–180 to 40–115
servo.duty(duty)

# Main loop
while True:
print("Rotating to 0°")
set_angle(0)
sleep(1)

print("Rotating to 90°")
set_angle(90)
sleep(1)

print("Rotating to 180°")
set_angle(180)
sleep(1)

STEM Benefits

  • Science: Understand torque, rotation, and angular displacement.
  • Technology: Learn how servo motors are used in robotics, automation, and precise positioning tasks.
  • Engineering: Build systems that require accurate movement using PWM control.
  • Math: Apply angle measurement and timing calculations to achieve precise control.

Project Code

Projects
ShowCase

Real-World Projects
with Code & Hardware

Insights, Ideas
& How-Tos

Help, Support, and
Common Questions

What types of projects can I find on your website?

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.

You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.