Code with a Servo Motor

Let’s create a simple project that uses an ESP32 to control a servo motor with MicroPython. We’ll use a basic servo driver circuit to control the angle of the servo.

Project: Controlling a Servo Motor with ESP32

Components Needed:

1. ESP32 development board
2. Servo motor (e.g., SG90)
3. NPN Transistor (e.g., 2N2222) or a dedicated servo driver (optional, if the servo can be powered directly from the ESP32)
4. 220-ohm resistor (if using a transistor)
5. Breadboard and jumper wires
6. External power supply (if needed, e.g., 5V for the servo)

Wiring:

1. Servo Motor:
• Power (Red wire) to 5V (or external power supply if needed).
• Ground (Brown or Black wire) to GND.
• Control (Yellow or Orange wire) to GPIO pin (e.g., GPIO 23) of ESP32.
2. Optional Transistor Circuit (for power control):
• Collector of NPN Transistor to Servo Power (+5V)
• Emitter of NPN Transistor to Ground
• Base of NPN Transistor through a 220-ohm resistor to GPIO pin of ESP32
• Servo Power (Red wire) to Collector of Transistor
• Servo Ground (Black wire) to Emitter of Transistor
—————————————————————————————————–

from machine import Pin, PWM
import time

# Define the GPIO pin connected to the servo control wire
servo_pin = Pin(23, Pin.OUT)

# Initialize PWM on the servo pin
servo_pwm = PWM(servo_pin)
servo_pwm.freq(50)  # Servo motors typically use a 50 Hz PWM signal

def set_angle(angle):
    # Convert angle (0-180) to duty cycle
    # Servo motors usually require a pulse width between 1ms and 2ms
    # which corresponds to a duty cycle range (e.g., 40 to 115)
    duty = int((angle / 180 * (115 - 40)) + 40)
    servo_pwm.duty(duty)

# Main loop to sweep the servo
while True:
    for angle in range(0, 180, 5):  # Sweep from 0 to 180 degrees
        set_angle(angle)
        time.sleep(0.1)  # Wait for the servo to reach the position

    for angle in range(180, 0, -5):  # Sweep from 180 to 0 degrees
        set_angle(angle)
        time.sleep(0.1)  # Wait for the servo to reach the position

—————————————————————————————————–
Explanation:

1. Imports:
• Pin and PWM are imported from the machine module to control the GPIO and PWM functionality.
• time is used for delays.
2. Pin Definitions:
• servo_pin is defined as GPIO 23 for controlling the servo.
3. PWM Initialization:
• servo_pwm is created as a PWM object on servo_pin with a frequency of 50 Hz.
4. Set Angle Function:
• Converts the desired angle (0-180 degrees) to a duty cycle that the servo expects.
• The duty cycle is mapped to a typical servo range (40-115).
5. Main Loop:
• Sweeps the servo from 0 to 180 degrees and back to 0 degrees, with a 0.1-second delay for each step to allow the servo to move.

This code should be saved and run on your ESP32 using Thonny or another MicroPython IDE. If you’re using an external power supply for the servo, make sure it’s connected properly and the ground is shared with the ESP32.

Leave a Comment

Your email address will not be published. Required fields are marked *