Raspberry Pi Pico
Reed Switch
A reed switch is a switch that is actuated by the presence of a magnetic field. When you bring a magnet close to the switch, you do the equivalent of pressing a button. Depending on the exact reed switch you are using, the switch is either opened or closed when the magnet is introduced.
A reed switch can be used just like you would a button, making it quite a simple component to work with. Unlike a button, it is very difficult to press accidentally. In the photograph, the magnet (blue) is just close enough to trigger the reed switch to turn the built-in LED on.
Here's the same thing in a diagram,
from machine import Pin from time import sleep reed = Pin(16,Pin.IN,Pin.PULL_UP) led = Pin(25,Pin.OUT) def btn_handler(pin): if reed.value()==0: print("Magnet") led.value(1) else: print("No magnet") led.value(0) reed.irq(trigger=Pin.IRQ_RISING|Pin.IRQ_FALLING,handler=btn_handler)
I used interrupts to capture when the switch is opened and closed. Note the use of the OR operator with the two IRQ edges.