from machine import Pin, ADC, SoftI2C
import ssd1306
import time
# Initialize I2C for OLED
i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) # SCL to GPIO 22, SDA to GPIO 21
oled = ssd1306.SSD1306_I2C(128, 64, i2c) # Create OLED object
# Initialize potentiometers as analog inputs
ph_sensor = ADC(Pin(34)) # Potentiometer for pH
temp_sensor = ADC(Pin(35)) # Potentiometer for temperature
turbidity_sensor = ADC(Pin(32)) # Potentiometer for turbidity
# Configure ADC range (0-3.3V)
ph_sensor.atten(ADC.ATTN_11DB)
temp_sensor.atten(ADC.ATTN_11DB)
turbidity_sensor.atten(ADC.ATTN_11DB)
# Initialize LED as output
led = Pin(15, Pin.OUT)
# Define thresholds
PH_THRESHOLD = 7.5 # Example pH threshold
TEMP_THRESHOLD = 30 # Example temperature threshold (°C)
TURBIDITY_THRESHOLD = 800 # Example turbidity threshold (raw ADC value)
while True:
# Read analog values
ph_value = ph_sensor.read() / 4095 * 14 # Scale ADC value to pH range (0-14)
temp_value = temp_sensor.read() / 4095 * 100 # Scale ADC value to temperature (0-100°C)
turbidity_value = turbidity_sensor.read() # Raw ADC value
# Determine if any value exceeds safe thresholds
alert = ph_value > PH_THRESHOLD or temp_value > TEMP_THRESHOLD or turbidity_value > TURBIDITY_THRESHOLD
# Update LED status
led.value(1 if alert else 0)
# Update OLED display
oled.fill(0) # Clear the display
oled.text("Water Quality:", 0, 0)
oled.text(f"pH: {ph_value:.2f}", 0, 10)
oled.text(f"Temp: {temp_value:.2f}C", 0, 20)
oled.text(f"Turbidity: {turbidity_value}", 0, 30)
oled.text("ALERT!" if alert else "SAFE", 0, 50)
oled.show()
time.sleep(1) # Delay for 1 second