# ============================================================
# SUBSYSTEM 1: Pico + 1x Load Cells (HX711) + LCD
# ============================================================
from machine import Pin, SoftI2C
from pico_i2c_lcd import I2cLcd
from time import sleep
# ── I2C LCD SETUP ───────────────────────────────────────────
I2C_ADDR = 0x27
I2C_NUM_ROWS = 4
I2C_NUM_COLS = 20
i2c = SoftI2C(sda=Pin(4), scl=Pin(5), freq=100000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
def _pad(text, width=20):
text = str(text)
if len(text) > width: return text[:width]
return text + " " * (width - len(text))
def lcd_row(row, text):
lcd.move_to(0, row)
lcd.putstr(_pad(text))
def screen_full(r0, r1, r2, r3):
lcd_row(0, r0); lcd_row(1, r1); lcd_row(2, r2); lcd_row(3, r3)
# ── HX711 DRIVER ────────────────────────────────────────────
class HX711:
def __init__(self, dt, sck):
self.dt = Pin(dt, Pin.IN)
self.sck = Pin(sck, Pin.OUT)
self.offset = 0
def read(self):
while self.dt.value() == 1:
pass
data = 0
for _ in range(24):
self.sck.value(1)
data = (data << 1) | self.dt.value()
self.sck.value(0)
self.sck.value(1)
self.sck.value(0)
if data & 0x800000:
data -= 0x1000000
return data
def tare(self, samples=15):
total = sum(self.read() for _ in range(samples))
self.offset = total // samples
def get_weight(self, scale=1):
weight = (self.read() - self.offset) / scale
return max(0, weight)
# ── SENSOR INIT ─────────────────────────────────────────────
hx1 = HX711(dt=2, sck=3)
scale_factor = 1000
SLOT_THRESHOLD = 300
screen_full("====================", " Taring Sensors ", " Please Wait... ", "====================")
hx1.tare()
#hx1.tare()
tick = 0
try:
while True:
tick += 1
w1 = 500
slots = [w1]
occupied = sum(1 for w in slots if w > SLOT_THRESHOLD)
indicator = ["|", "/", "-", "\\"][tick % 4]
lcd_row(0, "=Weight Dashboard{}=".format(indicator))
tag = "V" if w1 > SLOT_THRESHOLD else "E"
lcd_row(1, "S1:{:.0f}g({})".format(w1, tag))
lcd_row(2, "Threshold: 300g")
lcd_row(3, "Load Cell Test")
sleep(0.2)
except KeyboardInterrupt:
lcd.clear()
lcd.backlight_off()