🐱 Tutorial 8 - Reed Switch

A single reed switch is a type of electrical switch that is operated by a magnetic field. It consists of a pair of ferromagnetic metal contacts, or reeds, that are sealed inside a glass tube or capsule. The reeds are positioned close to each other, with a small air gap between them.

When a magnetic field is applied to the switch, the magnetic force overcomes the spring force holding the reeds apart, and they come into contact, closing the switch. When the magnetic field is removed, the reeds spring back to their original position, opening the switch.

Single reed switches are commonly used in a variety of applications where a simple and reliable switch is required, such as in security systems, door and window sensors, and automotive electronics. They are also used in level sensors, proximity sensors, and other types of sensing devices.

Single reed switches are available in various sizes and sensitivity levels, with some switches able to sense magnetic fields at distances of several inches. They are designed to be durable and long-lasting, with a high number of operating cycles and a wide range of operating temperatures.

Overall, single reed switches are a widely used and versatile component in electronic circuits and devices, providing simple and reliable switching for a wide range of applications.

Components Needed

ComponentQuantity
Raspberry Pi Pico W1
Micro USB Cable1
Breadboard1
WiresSeveral
Resistor1 - 10KΩ
Reed Switch1

Fritzing Diagram

Code

import machine
import utime
reed = machine.Pin(14, machine.Pin.IN)
while True:
    if reed.value() == 1:
        print("There are magnets here!!")
        utime.sleep(1)

Code Explanation

  1. Importing necessary modules:
import machine
import utime

The code imports the machine module, which is typically used for interacting with hardware on microcontrollers, and the utime module, which is used for time-related operations.

  1. Setting up the reed switch pin:
reed = machine.Pin(14, machine.Pin.IN)

The code defines a reed variable and assigns it to the Pin object with the pin number 14 and the mode set to machine.Pin.IN, which means the pin is configured as an input pin to read the state of the reed switch.

  1. Looping indefinitely for reed switch state checking:
while True:

The code starts an infinite loop that will continuously check the state of the reed switch.

  1. Checking reed switch state and printing message:
if reed.value() == 1:
    print("There are magnets here!!")
    utime.sleep(1)

Inside the loop, the code checks the value of the reed switch using the reed.value() method. If the reed switch is closed (i.e., its value is 1), it prints the message “There are magnets here!!” to the console using the print() function. It then pauses the code execution for 1 second using utime.sleep(1) to avoid multiple prints in a short duration.

The code will continue to loop indefinitely, checking the state of the reed switch and printing the message whenever the switch is closed, allowing for a simple reed switch functionality.