from machine import Pin, PWM
import time
import dht # Import the dht module to interface with the DHT sensor
# Define GPIO pins
# Inputs
start_button = Pin(14, Pin.IN, Pin.PULL_UP)
low_switch = Pin(18, Pin.IN, Pin.PULL_UP) # Float level low switch connected to GPIO18
middle_switch = Pin(20, Pin.IN, Pin.PULL_UP) # Float level middle switch connected to GPIO20
middle_switch_2 = Pin(16, Pin.IN, Pin.PULL_UP) # Float level middle switch connected to GPIO16
high_switch = Pin(17, Pin.IN, Pin.PULL_UP) # Float level high switch connected to GPIO17
final_switch = Pin(13, Pin.IN, Pin.PULL_UP) # Float level final switch connected to GPIO13
# Outputs
resistance = Pin(15, Pin.OUT) # Resistance connected to GPIO15
electrovanne = Pin(19, Pin.OUT) # Electrovanne connected to GPIO19
buzzer = PWM(Pin(21)) # Buzzer connected to GPIO21
indicator = Pin(22, Pin.OUT) # electrovanne out connected to GPI22
# DHT Sensors
dht_sensor_1 = dht.DHT22(Pin(4)) # First DHT sensor connected to GPIO4
dht_sensor_2 = dht.DHT22(Pin(5)) # Second DHT sensor connected to GPIO5
# State variable to track whether monitoring is enabled
monitoring_enabled = False
# Timer variables
buzzer_timer = time.ticks_ms()
buzzer_state = False # Track if the buzzer should be on or off
BUZZER_INTERVAL = 10000 # Interval for buzzer toggle (10 seconds)
def check_start_button():
"""Checks the state of the start button and toggles monitoring."""
global monitoring_enabled
if start_button.value() == 0: # Start button pressed
time.sleep_ms(100) # Debounce delay
if start_button.value() == 0: # Confirm button is still pressed
monitoring_enabled = not monitoring_enabled # Toggle monitoring state
print("Monitoring enabled" if monitoring_enabled else "Monitoring disabled")
electrovanne.value(1)
if not monitoring_enabled:
# Turn off all systems when monitoring is disabled
print("System OFF: Turning electrovanne, resistance, electrovanne out, indicator and buzzer OFF")
electrovanne.value(0) # Turn electrovanne OFF
resistance.value(0) # Turn resistance OFF
indicator.value(0)
buzzer.deinit() # Turn buzzer OFF
while start_button.value() == 1:
time.sleep_ms(100) # Wait for button release to avoid multiple toggles
def check_low_switch():
"""Checks the state of the low float switch and controls the resistance."""
if low_switch.value() == 0 and middle_switch.value() == 1 and high_switch.value() == 1: # Low float switch closed (liquid level detected)
print("Low switch activated: Turning resistance ON")
resistance.value(1) # Turn resistance ON
def check_middle_switch():
"""Checks the state of the middle float switch and controls the electrovanne."""
if middle_switch.value() == 0 and high_switch.value() == 1: # Middle float switch closed (liquid level detected)
print("Middle switch activated: Turning electrovanne ON still")
electrovanne.value(1) # Turn electrovanne ON
def check_high_switch():
"""Checks the state of the high float switch and controls the resistance."""
if high_switch.value() == 0: # High float switch closed (liquid level detected)
print("High switch activated: Turning electrovanne OFF")
electrovanne.value(0) # Turn electrovanne OFF
def toggle_buzzer():
"""Toggles the buzzer state based on timing intervals."""
global buzzer_timer, buzzer_state
current_time = time.ticks_ms()
# Toggle buzzer state every 10 seconds
if time.ticks_diff(current_time, buzzer_timer) >= BUZZER_INTERVAL:
buzzer_state = not buzzer_state # Toggle buzzer state
buzzer_timer = current_time # Reset buzzer timer
# Set buzzer on or off based on the current state
if buzzer_state:
print("Buzzer ON")
buzzer.freq(1000) # Set buzzer frequency to 1000 Hz
buzzer.duty_u16(32768) # Set duty cycle to 50%
else:
print("Buzzer OFF")
buzzer.duty_u16(0) # Turn buzzer off
def read_temperature_1():
"""Reads the first DHT sensor and prints temperature and humidity."""
try:
dht_sensor_1.measure() # Measure temperature and humidity
temperature = dht_sensor_1.temperature() # Get temperature in Celsius
humidity = dht_sensor_1.humidity() # Get humidity as a percentage
print(f"DHT Sensor 1 - Temperature: {temperature}°C, Humidity: {humidity}%")
# If temperature exceeds 50°C, turn resistance OFF and activate buzzer
if temperature >= 50:
print("DHT Sensor 1 - Temperature exceeds 50°C: Turning resistance OFF and toggling buzzer")
resistance.value(0) # Turn resistance OFF
toggle_buzzer()
else:
buzzer.duty_u16(0)
check_low_switch()
except OSError as e:
print("Failed to read from DHT Sensor 1:", e)
def check_middle_switch_2():
"""Checks the state of the middle float switch and controls the electrovanne."""
if middle_switch_2.value() == 0: # Middle float switch closed (liquid level detected)
print("Middle switch activated: Turning indicator ON")
indicator.value(1) # Turn indicator ON
def check_final_switch():
"""Checks the state of the high float switch and controls the resistance."""
if final_switch.value() == 0: # High float switch closed (liquid level detected)
print(" end of distillation")
electrovanne.value(0) # Turn electrovanne OFF
indicator.value(0)
resistance.value(0)
buzzer.deinit()
def read_temperature_2():
"""Reads the second DHT sensor and prints temperature and humidity."""
try:
dht_sensor_2.measure() # Measure temperature and humidity
temperature_2 = dht_sensor_2.temperature() # Get temperature in Celsius
humidity_2 = dht_sensor_2.humidity() # Get humidity as a percentage
print(f"DHT Sensor 2 - Temperature: {temperature_2}°C, Humidity: {humidity_2}%")
except OSError as e:
print("Failed to read from DHT Sensor 2:", e)
def main():
"""Main function to continuously monitor the float switches and temperature."""
print("Waiting for Start button...")
while True:
check_start_button()
if monitoring_enabled:
read_temperature_1() # Read and print data from the first DHT sensor
read_temperature_2() # Read and print data from the second DHT sensor
check_middle_switch() # Continuously monitor the middle float switch
check_high_switch() # Continuously monitor the high float switch
check_middle_switch_2()
check_final_switch()
time.sleep(0.3) # Delay for stability and readability
# Run the main function
if __name__ == "__main__":
main()
Loading
ds18b20
ds18b20