from machine import Pin
from onewire import OneWire
from ds18x20 import DS18X20
import time
# 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
high_switch = Pin(17, Pin.IN, Pin.PULL_UP) # Float level high switch connected to GPIO17
# Outputs
resistance = Pin(15, Pin.OUT) # Resistance connected to GPIO15
electrovanne = Pin(19, Pin.OUT) # Electrovanne connected to GPIO19
# Temperature Sensor
temp_sensor_pin = Pin(26) # Temperature sensor connected to GPIO16
temp_sensor = DS18X20(OneWire(temp_sensor_pin))
# State variable to track whether monitoring is enabled
monitoring_enabled = False
# Global temperature value
temperature = 0.0
def read_temperature():
"""Reads temperature from the DS18B20 sensor and updates the global variable."""
# Scan for DS18B20 devices
roms = temp_sensor.scan()
if not roms:
print("No DS18B20 sensor detected! Please check the wiring.")
return None
print(f"Found {len(roms)} device(s): {', '.join([rom.hex() for rom in roms])}")
# Start temperature conversion
temp_sensor.convert_temp()
time.sleep(0.75) # Wait for the conversion to complete
# Read temperature from all detected devices
for rom in roms:
temperature = temp_sensor.read_temp(rom)
print(f"ROM: {rom.hex()} - Temperature: {temperature:.2f}°C")
return temperature # Return the last temperature read
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(0.1) # 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 and resistance OFF")
electrovanne.value(0) # Turn electrovanne OFF
resistance.value(0) # Turn resistance OFF
while start_button.value() == 1:
time.sleep(0.1) # 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 check_temperature():
"""Checks the temperature and turns off the resistance if it's too high."""
if temperature > 50.0:
print("Temperature exceeds 50°C! Turning resistance OFF.")
resistance.value(0) # Turn resistance OFF
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:
temp = read_temperature() # Read and print temperature
if temp is not None: # Continue only if the sensor is working
check_temperature() # Monitor temperature and control resistance
check_low_switch() # Continuously monitor the low float switch
check_middle_switch() # Continuously monitor the middle float switch
check_high_switch() # Continuously monitor the high float switch
time.sleep(0.3) # Delay for stability and readability
# Run the main function
if __name__ == "__main__":
main()