from machine import Pin, ADC, I2C, PWM
import time
from ssd1306 import SSD1306_I2C
# Pins
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB)
pot.width(ADC.WIDTH_12BIT)
# Buzzer PWM
buzzer = PWM(Pin(25))
buzzer.freq(1000)
buzzer.duty(0)
# OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
oled = SSD1306_I2C(128, 64, i2c, addr=0x3C)
while True:
adc_value = pot.read()
voltage = (adc_value / 4095) * 3.3
percentage = (adc_value / 4095) * 100
# OLED
oled.fill(0)
oled.text("POTENTIOMETER", 5, 0)
oled.text("ADC: {}".format(adc_value), 5, 18)
oled.text("Volt: {:.2f} V".format(voltage), 5, 32)
oled.text("Level: {:.0f}%".format(percentage), 5, 46)
oled.show()
# Buzzer
if percentage >= 80:
buzzer.duty(512) # BUZZER ON
else:
buzzer.duty(0) # BUZZER OFF
time.sleep(0.1)