# write a micropython script to control the toglling rate of an led connected with
# the gpio 4 with respect to the knob position of a potentiometer connecte to gpio 34 of esp 32
from machine import Pin, ADC
import time
led_pin = Pin(4, Pin.OUT)
potentiometer_pin = ADC(Pin(34))
potentiometer_pin.atten(ADC.ATTN_11DB)
def map_value(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
led_state = False
while True:
pot_value = potentiometer_pin.read()
toggle_rate = map_value(pot_value, 0, 4095, 1000, 50)
led_state = not led_state
led_pin.value(led_state)
time.sleep_ms(toggle_rate)