from machine import ADC, Pin
from gpio_lcd import GpioLcd
import time
data = ADC(Pin(27))
lcd = GpioLcd(rs_pin=Pin(16),
enable_pin=Pin(17),
d4_pin=Pin(18),
d5_pin=Pin(19),
d6_pin=Pin(20),
d7_pin=Pin(21),
num_lines=2, num_columns=16)
while True:
raw_val = data.read_u16()
calib_val = (2200 / (raw_val / 65535) ) / 40 - 75
# 2200 is the resistor used to split voltage (2.2k ohm)
# 65535 is the range of ADC reading
# 40 and 35 are calibration factors, you can change this to calibrate the value
# Note that the first 1 inch are not accurate
# -This is also shown in the resisstance vs water level graph in the datasheet
# -You might be able to do some curve fitting to get good reading in that range
print(raw_val)
lcd.move_to(0,0)
lcd.putstr(f"Raw: {raw_val}")
lcd.move_to(0,1)
lcd.putstr(f"Reading mm:{round(calib_val, 1)}")
lcd.putstr(" ")