In this project, you’ll learn how to control the angle of a Servo Motor Control Using PWM in MicroPython for ESP32 and ESP8266. Servo motors are widely used in robotics and automation for precise angular control. By adjusting the PWM signal, we can control the angle of the servo motor, making this project an excellent introduction to PWM and frequency adjustment in embedded systems.
Purpose of the Project:
The purpose of this project is to help you:
- Understand PWM for servo motor control.
- Adjust the frequency and duty cycle to control the angular position of a servo motor.
- Apply PWM to other applications that require precise control over motors.
Data Types and Variable Table for Servo Motor Control Using PWM in MicroPython
Data Type | Variable Name | Description |
PWM | servo | Represents the PWM object controlling the servo motor’s angle. |
int | duty | Duty cycle value that determines the angle of the servo (40-115). |
Syntax Table for Servo Motor Control Using PWM in MicroPython
Operation | Syntax | Example |
Initialize PWM on a Pin | pwm = PWM(Pin(pin_number)) | servo = PWM(Pin(2)) |
Set PWM Frequency | pwm.freq(frequency) | servo.freq(50) |
Set PWM Duty Cycle | pwm.duty(duty_cycle) | servo.duty(80) (sets angle) |
Stop PWM | pwm.deinit() | servo.deinit() |
Required Components for Servo Motor Control Using PWM
- ESP32 or ESP8266 board
- 1x Servo Motor (SG90 or similar)
- External Power Supply (for the servo motor)
- Jumper Wires
- Breadboard
Circuit Diagram for Servo Motor Control Using PWM
Servo Motor
————
| |
| Signal --> GPIO2 (PWM Pin)
| VCC --> 5V (external power supply)
| GND --> GND (ESP32/ESP8266)
————
Circuit Connection Table
Component | Pin | ESP32/ESP8266 Pin | Explanation |
Servo Signal | Connected to GPIO2 | GPIO2 (PWM Pin) | Controls the angle of the servo motor using PWM. |
Servo VCC | Connected to 5V | External Power Supply | Provides power to the servo motor. |
Servo GND | Connected to GND | GND | Ground connection for the servo motor. |
Warning:
- External Power: Ensure that you use an external power supply to power the servo motor, as the ESP32/ESP8266 may not provide enough current.
- Avoid Overdriving the Servo: Ensure you set the correct frequency (typically 50Hz) for servo motors to avoid damage.
Circuit Analysis for Servo Motor Control Using PWM:
A servo motor is controlled by sending a PWM signal with a specific frequency (50Hz for most hobby servos). The position of the servo motor’s shaft is determined by the width of the pulse (duty cycle). By varying the duty cycle between 40 (0 degrees) and 115 (180 degrees), we can control the angle of the servo motor.
Installing MicroPython and Libraries:
No additional libraries are required for this project. The machine module in MicroPython provides built-in support for PWM control.
Writing the MicroPython Code for Servo Motor Control Using PWM:
import machine
import time
# Initialize PWM on GPIO2
servo = machine.PWM(machine.Pin(2))
# Set PWM frequency to 50Hz (standard for servo motors)
servo.freq(50)
# Function to set servo angle
def set_angle(angle):
# Convert angle (0 to 180) to duty cycle (40 to 115)
duty = int(40 + (angle / 180) * 75)
servo.duty(duty)
# Gradually move servo from 0 to 180 degrees and back
while True:
for angle in range(0, 181, 10):
set_angle(angle)
time.sleep(0.5)
for angle in range(180, -1, -10):
set_angle(angle)
time.sleep(0.5)
Running the Code and Checking the Output:
- Upload the code to your ESP32 or ESP8266 using Thonny or another MicroPython IDE.
- The servo motor will gradually move from 0 degrees to 180 degrees, then back to 0 degrees in 10-degree steps.
- You should observe smooth and controlled movements of the servo as it responds to the PWM signals.
Explanation of the Code:
- PWM Initialization: The servo is initialized on GPIO2, and the PWM frequency is set to 50Hz (the standard for servo motors).
- Duty Cycle Adjustment: The set_angle function converts the desired angle (0 to 180 degrees) into a corresponding PWM duty cycle.
- Loop Function: The program continuously moves the servo from 0 to 180 degrees and back, updating the position every 0.5 seconds.
Expanding the Project:
- Control Multiple Servos: Use additional GPIO pins to control multiple servos independently.
- Sensor Input: Add a potentiometer or sensor to control the servo’s angle dynamically based on user input or environmental conditions.
- Button Control: Integrate buttons to manually adjust the servo angle in defined steps.
Common Problems and Solutions for Servo Motor Control Using PWM:
Problem | Solution |
Servo motor not moving | Check the circuit connections and ensure that the servo has an external power supply. |
Servo jittering or erratic movement | Ensure the PWM frequency is set to 50Hz and that the power supply is stable. |
Servo not reaching the correct angle | Verify the duty cycle range (40-115) is correct for your specific servo model. |
FAQ for Servo Motor Control Using PWM:
Q: Can I use a different GPIO pin for the servo motor?
A: Yes, as long as the pin supports PWM. Check the ESP32/ESP8266 documentation to find PWM-capable pins.
Q: How do I adjust the servo motor’s speed?
A: The speed is controlled by how quickly you update the angle. Decrease the time delay in the loop to speed up the servo movement.
Q: Can I control other types of motors with PWM?
A: Yes, PWM can be used to control DC motors, stepper motors, and other actuators.
Conclusion:
In this project, you learned how to control a servo motor using PWM in MicroPython for ESP32 and ESP8266. By understanding how to adjust the PWM frequency and duty cycle, you now have the tools to control the angular position of servo motors, which is a critical skill for robotics and automation projects. This project can be expanded to include multiple servos or sensor-based control for more complex applications.