print("Project Title: Water Quality Monitoring System")
print("Date: 22/11/2024")
print("Created by Nilla")
# Import libraries/modules
from machine import Pin, ADC, SoftI2C, PWM
import oled_library # Ensure the SSD1306 library is included
from utime import sleep
# Pin Declaration
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21)) # SCL to GPIO 22, SDA to GPIO 21
ph_sensor = ADC(Pin(34)) # Potentiometer connected to GPIO 34 (pH)
temp_sensor = ADC(Pin(35)) # Potentiometer connected to GPIO 35 (Temperature)
turb_sensor = ADC(Pin(32)) # Potentiometer connected to GPIO 32 (Turbidity)
led = Pin(23, Pin.OUT) # LED connected to GPIO 23
buzzer = Pin(5, Pin.OUT) # Buzzer connected to GPIO 5
# Create PWM object on the buzzer pin
pwm_buzzer = PWM(buzzer)
pwm_buzzer.freq(2000) # Set frequency to 2 kHz
pwm_buzzer.duty(0) # Start with buzzer off
# ADC configuration
ph_sensor.atten(ADC.ATTN_11DB) # Set ADC to 0-3.3V range for pH
temp_sensor.atten(ADC.ATTN_11DB) # Set ADC to 0-3.3V range for Temperature
turb_sensor.atten(ADC.ATTN_11DB) # Set ADC to 0-3.3V range for Turbidity
# Object Declaration for OLED
# Parameters: width=128, height=64
Skrim = oled_library.SSD1306_I2C(128, 64, i2c=oled_pin)
# Thresholds
TEMP_THRESHOLD = 30 # Unsafe temperature threshold (°C)
TURB_THRESHOLD = 50 # Unsafe turbidity threshold (raw ADC value)
# Function to display on OLED
def display_on_oled(text1, text2, text3):
Skrim.fill(0) # Clear the screen
Skrim.text(text1, 10, 10, 1)
Skrim.text(text2, 10, 30, 1)
Skrim.text(text3, 10, 50, 1)
Skrim.show()
# Function to activate the buzzer
def activate_buzzer():
pwm_buzzer.duty(512) # Turn on the buzzer with 50% duty cycle (moderate volume)
sleep(0.5) # Keep the buzzer on for 0.5 seconds
pwm_buzzer.duty(0) # Turn off the buzzer after 0.5 seconds
# Main Program
while True:
# Read potentiometer values (0-4095)
raw_ph_value = ph_sensor.read() # pH potentiometer
raw_temp_value = temp_sensor.read() # Temperature potentiometer
raw_turb_value = turb_sensor.read() # Turbidity potentiometer
# Map the raw pH value to a pH scale (0-14)
ph_value = raw_ph_value / 4095 * 14 # Scaling from 0-4095 to 0-14 pH
# Map the raw temperature value to a temperature range (0-100°C)
temp_value = raw_temp_value / 4095 * 100 # Scaling from 0-4095 to 0-100°C
# Map the raw turbidity value to a range (0-1000)
turb_value = raw_turb_value # Direct scaling from 0-4095 to 0-1000
# Display the pH value
if ph_value > 7.5:
display_on_oled(f"pH Value: {ph_value:.2f}", "Water UNSAFE!", "")
led.value(1) # Turn on LED if pH is unsafe
activate_buzzer() # Activate buzzer if pH is unsafe
else:
display_on_oled(f"pH Value: {ph_value:.2f}", "Water Safe", "")
led.value(0) # Turn off LED if water is safe
sleep(1) # Wait for 1 seconds before switching to the next screen
# Display the temperature value
if temp_value > TEMP_THRESHOLD:
display_on_oled(f"Temp: {temp_value:.2f}C", "Water UNSAFE!", "")
led.value(1) # Turn on LED if temperature is unsafe
activate_buzzer() # Activate buzzer if temperature is unsafe
else:
display_on_oled(f"Temp: {temp_value:.2f}C", "Water Safe", "")
led.value(0) # Turn off LED if temperature is safe
sleep(1) # Wait for 1 seconds before switching to the next screen
# Display the turbidity value
if turb_value > TURB_THRESHOLD:
display_on_oled(f"Turbidity: {turb_value}", "Turbidity High!", "")
led.value(1) # Turn on LED if turbidity is high
activate_buzzer() # Activate buzzer if turbidity is unsafe
else:
display_on_oled(f"Turbidity: {turb_value}", "Turbidity Safe", "")
led.value(0) # Turn off LED if turbidity is safe
sleep(1) # Wait for 1 seconds before cycling back to the pH screen