from machine import Pin
import time
import hx711 # your driver module file
# Initialize HX711 with CLK and DAT pins
clk_pin = Pin(17, Pin.OUT)
dat_pin = Pin(16, Pin.IN)
scale = hx711.hx711(clk=clk_pin, dat=dat_pin)
# Optional: Set gain if needed (default is 128)
# scale.set_gain(hx711.hx711.gain.gain_128)
# Wait for sensor settling after power up
hx711.hx711.wait_settle(hx711.hx711.rate.rate_10)
def read_weight():
try:
raw_val = scale.get_value()
print("Raw HX711 value:", raw_val)
# Convert raw_val to weight by calibration (apply your calibration here)
# weight = (raw_val - offset) / scale_factor
return raw_val
except Exception as e:
print("Error reading HX711:", e)
return None
while True:
weight = read_weight()
if weight is not None:
# Example threshold to prevent reverse flow (your logic here)
if weight < 1000:
print("Warning: Low saline level detected!")
else:
print("Saline level OK.")
time.sleep(1)