from machine import Pin, ADC, I2C
import ssd1306
from time import sleep
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
btn_nav = Pin(11, Pin.IN)
btn_sel = Pin(12, Pin.IN)
adc = ADC(Pin(26))
# resistor values — change to match your circuit
R1 = 30000 # voltage divider high side
R2 = 10000 # voltage divider low side
R_shunt = 1.0 # shunt resistor for current
R_known = 10000 # known resistor for resistance measurement
modes = ["Voltage", "Current", "Resistance", "Capacitance"]
units = ["V", "A", "Ohm", "uF"]
cursor = 0
def read_value():
raw = adc.read_u16()
vout = raw * 3.3 / 65535
if cursor == 0: # voltage
val = vout * (R1 + R2) / R2
elif cursor == 1: # current
val = vout / R_shunt
elif cursor == 2: # resistance
if vout >= 3.3: # avoid divide by zero
val = 99999
else:
val = R_known * vout / (3.3 - vout)
elif cursor == 3: # capacitance placeholder
val = 0.0
return round(val, 3)
def draw_option():
oled.fill(0)
oled.text("Select mode:", 0, 0, 1)
oled.hline(0, 12, 128, 1)
oled.text(modes[cursor], 20, 30, 1)
oled.text(str(cursor+1)+"/"+str(len(modes)), 50, 52, 1)
oled.show()
def draw_measuring():
while btn_nav.value() == 1:
val = read_value()
oled.fill(0)
oled.text("Measuring:", 0, 0, 1)
oled.hline(0, 12, 128, 1)
oled.text(modes[cursor], 0, 20, 1)
oled.text(str(val) + " " + units[cursor], 0, 38, 1)
oled.text("NAV = back", 0, 54, 1)
oled.show()
sleep(0.3)
# ── MAIN ──
draw_option()
last_nav = 1
last_sel = 1
while True:
nav = btn_nav.value()
sel = btn_sel.value()
if last_nav == 1 and nav == 0:
sleep(0.05)
cursor = (cursor + 1) % len(modes)
draw_option()
if last_sel == 1 and sel == 0:
sleep(0.05)
draw_measuring()
sleep(0.05)
draw_option()
last_nav = nav
last_sel = sel
sleep(0.01)