from machine import Pin, ADC, I2C
import time
from i2c_lcd import I2cLcd
# ---------- Pin Setup ----------
ph_sensor = ADC(Pin(34))
turbidity_sensor = ADC(Pin(35))
flow_sensor = Pin(27, Pin.IN)
alert_led = Pin(18, Pin.OUT) # Single LED
# ---------- ADC Configuration ----------
ph_sensor.atten(ADC.ATTN_11DB)
turbidity_sensor.atten(ADC.ATTN_11DB)
# ---------- LCD Setup ----------
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# ---------- Flow Variables ----------
flow_count = 0
def flow_interrupt(pin):
global flow_count
flow_count += 1
flow_sensor.irq(trigger=Pin.IRQ_RISING, handler=flow_interrupt)
# ---------- Main Loop ----------
while True:
# Read Sensors
ph_value = ph_sensor.read()
turbidity_value = turbidity_sensor.read()
# Convert to approximate values
ph = (ph_value / 4095) * 14
turbidity = (turbidity_value / 4095) * 100
flow_rate = flow_count
flow_count = 0
# LCD Display
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("pH:{:.1f} T:{:.1f}".format(ph, turbidity))
lcd.move_to(0, 1)
lcd.putstr("Flow:{} L/m".format(flow_rate))
# Alert Condition (Single LED)
if ph < 6.5 or ph > 8.5 or turbidity > 70:
alert_led.on() # LED ON = Problem
else:
alert_led.off() # LED OFF = Normal
# Serial Output
print("pH:", ph)
print("Turbidity:", turbidity)
print("Flow:", flow_rate)
time.sleep(2)