from machine import Pin, ADC, PWM
from servo import Servo
import time
# Setup for the potentiometer (slide) connected to GPIO34 (ADC pin)
potentiometer = ADC(Pin(34)) # GPIO34 is a common ADC pin on ESP32
potentiometer.atten(ADC.ATTN_0DB) # Set the ADC range to 0-3.3V
# Setup for the servo motor connected to GPIO13 (PWM pin)
servo = Servo(13) # Servo motors use 50Hz PWM
# Function to map potentiometer value to servo angle
def map_pot_to_angle(pot_value):
# Potentiometer value ranges from 0 to 4095
# Servo angle ranges from 0 to 180 degrees
angle = (pot_value / 4095) * 180
return angle
# Function to set the servo position based on the angle
# Main loop to read the potentiometer value and set servo position
while True:
# Read the potentiometer value (0-4095)
pot_value = potentiometer.read()
# Map potentiometer value to angle (0-180 degrees)
angle = map_pot_to_angle(pot_value)
servo.move(angle)
# Delay to make the movement smooth
time.sleep(0.1)