from machine import Pin
import time
# 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
def check_float_switch():
"""Checks the state of the float switch and controls the LED."""
if float_switch.value() == 0: # Float switch closed (liquid level detected)
led.value(1) # Turn LED ON
else: # Float switch open (no liquid level detected)
print("Float switch is OPEN: No liquid level detected")
led.value(0) # Turn LED OFF
def main():
"""Main function to continuously monitor the float switch."""
print("Starting float switch monitoring...")
while True:
check_float_switch() # Continuously monitor the float switch
time.sleep(2) # Delay for stability and readability
# Run the main function
if __name__ == "__main__":
main()
Loading
pi-pico-w
pi-pico-w