SunFounder ESP32 Starter Kit
import machine
import time
# Create a PWM (Pulse Width Modulation) object on Pin 25
servo = machine.PWM(machine.Pin(25))
# Set the frequency of the PWM signal to 50 Hz, common for servos
servo.freq(50)
# Define a function for interval mapping
def interval_mapping(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# Define a function to write an angle to the servo
def servo_write(pin, angle):
pulse_width = interval_mapping(angle, 0, 180, 0.5, 2.5) # Calculate the pulse width
duty = int(interval_mapping(pulse_width, 0, 20, 0, 1023)) # Calculate the duty␣
˓→cycle
pin.duty(duty) # Set the duty cycle of the PWM signal
# Create an infinite loop
while True:
# Loop through angles from 0 to 180 degrees
for angle in range(180):
servo_write(servo, angle)
time.sleep_ms(20)
# Loop through angles from 180 to 0 degrees in reverse
for angle in range(180, -1, -1):
servo_write(servo, angle)
time.sleep_ms(20)
When running this code, the servo will continuously sweep back and forth between 0 and 180 degrees.
How it works?
1. Import the necessary libraries: machine for controlling the microcontroller’s hardware, and time for adding
delays.
import machine
import time
2. Create a PWM (Pulse Width Modulation) object on Pin 25 and set its frequency to 50 Hz, which is common for
servo.
# Create a PWM (Pulse Width Modulation) object on Pin 25
servo = machine.PWM(machine.Pin(25))
# Set the frequency of the PWM signal to 50 Hz, common for servos
servo.freq(50)
3. Define an interval_mapping function to map values from one range to another. This will be used to convert
the angle to the appropriate pulse width and duty cycle.
354 Chapter 3. For MicroPython User