from machine import Pin
import time
import dht # Import DHT sensor library
# Define GPIO pins
float_switch = Pin(18, Pin.IN, Pin.PULL_UP) # Float switch connected to GPIO18
led = Pin(15, Pin.OUT) # LED connected to GPIO15
sensor = dht.DHT11(Pin(14)) # DHT11 sensor connected to GPIO14 (change if using a different GPIO pin)
# Set thermostat temperature threshold (e.g., 30°C for high temperature)
temperature_threshold = 30 # Temperature threshold for normal thermostat control
high_temperature_threshold = 50 # If temperature exceeds 100°C, turn OFF LED
def check_float_switch():
"""Checks the state of the float switch and controls the LED."""
if float_switch.value() == 0: # Float switch pressed (closed), LOW state
print("Float switch is CLOSED: Liquid level detected")
led.value(1) # Turn LED ON
# else: # Float switch not pressed (open), HIGH state
# print("Float switch is OPEN: No liquid level detected")
# led.value(0) # Turn LED OFF
def read_temperature():
"""Reads the temperature from the DHT sensor."""
try:
sensor.measure() # Measure the temperature
temperature = sensor.temperature() # Get the temperature in Celsius
print("Temperature:", temperature, "°C")
return temperature
except OSError as e:
print("Failed to read sensor:", e)
return None
def thermostat_control():
"""Controls the LED based on the temperature."""
temperature = read_temperature() # Read the temperature
if temperature is not None:
if temperature >= high_temperature_threshold: # If temperature exceeds 100°C
print("Temperature exceeds 100°C! Turning LED OFF.")
led.value(0) # Turn LED OFF (high temperature)
elif temperature <= high_temperature_threshold: # If temperature is above threshold but below 100°C
print("Temperature exceeds threshold! Turning LED ON.")
led.value(1) # Turn LED ON (high temperature detected)
# else:
# print("Temperature is below threshold. Turning LED OFF.")
# led.value(0) # Turn LED OFF (temperature is normal)
def main():
"""Main function to continuously monitor the float switch and temperature."""
print("Starting float switch and thermostat monitoring...")
while True:
.
Loading
pi-pico-w
pi-pico-w