import time
from machine import Pin, ADC, PWM, I2C
from i2c_lcd import I2cLcd
# 1. LCD Setup
I2C_ADDR = 0x27
NUM_ROWS = 2
NUM_COLS = 16
# Initialize I2C on standard ESP32 pins (SDA=21, SCL=22)
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, NUM_ROWS, NUM_COLS)
# 2. Potentiometer Setup - CORRECTED TO PIN 34
pot = ADC(Pin(34, Pin.IN))
pot.width(ADC.WIDTH_10BIT) # 0-4095 range
pot.atten(ADC.ATTN_11DB) # allows full 0-3.3V range
# 3. RGB LEDs Setup - CORRECTED TO PINS 25, 26, 27
led_r = PWM(Pin(25), freq=1000)
led_g = PWM(Pin(26), freq=1000)
led_b = PWM(Pin(27), freq=1000)
while True:
# Read the ADC value
value = pot.read()
# Calculate ohm/voltage based on your snippet
ohm = (value / 4095) * 3.3
# Map 0-4095 ADC value to 0-1023 duty cycle for ESP32 PWM
duty = int((value / 4095) * 1023)
# Apply the duty cycle to control the RGB LED brightness
led_r.duty(duty)
led_g.duty(duty)
led_b.duty(duty)
# Print to console for debugging
print("pot value:", value, "| ohm:", round(ohm, 2), "| duty:", duty)
# Update the LCD display
lcd.clear()
# Top Row: Display Ohm
lcd.move_to(0, 0)
lcd.putstr("Ohm: {:.2f}".format(ohm))
# Bottom Row: Display Pot Value
lcd.move_to(0, 1)
lcd.putstr("Pot: {}".format(value))
time.sleep(0.5)