LED ON/OFF Using Reed Switch

  1. Introduction:

This experiment aims to demonstrate how to control an LED using a Reed switch and the Raspberry Pi Pico microcontroller. A Reed switch is a magnetic switch that changes its state (open or closed) when exposed to a magnetic field. When a magnet is brought near the Reed switch, the switch closes, and the LED connected to the microcontroller will turn ON. When the magnet is removed, the switch opens, and the LED turns OFF.

  1. Scope:

The experiment introduces the basic concept of reading digital input using a Reed switch and controlling an LED based on the input signal. This project is ideal for beginner-level embedded systems enthusiasts who want to understand how switches, sensors, and GPIO pins interact in microcontroller applications.



  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Reed Switch

  • LED

  • Magnet

  • Resistor(220 ohm)

  • Breadboard

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.





  1. Connection Details:

Reed Switch:

  • Connect one terminal of the Reed switch to GPIO Pin 15 on the Pico.

  • Connect the other terminal of the Reed switch to GND.

LED:

  • Connect the anode (longer leg) of the LED to GPIO Pin 14.

  • Connect the cathode (shorter leg) of the LED to a 220-ohm resistor and the other end of the resistor to GND.





  1. Working Principle:

The Reed switch works on a magnetic field. When a magnet is placed close to the switch, it changes from an open state to a closed state. This change can be detected by the microcontroller, which will then turn the LED ON or OFF. When the switch is closed (magnet present), the GPIO pin connected to the Reed switch will read a low value (0), and the LED will turn ON. When the magnet is removed, the GPIO pin reads a high value (1), and the LED turns OFF.



Reed Switch

The Reed Switch is an electrical switch that operates by means of an applied magnetic field. It was invented by Walter B. Ellwood of Bell Telephone Laboratories in 1936 and patented in the United States on June 27, 1940, under patent number 2264746 .

The principle of operation of a reed switch is very simple. Two reeds (usually made of iron and nickel, two metals) that overlap at the end points are sealed in a glass tube, with the two reeds overlapping and separated by a small gap (only about a few microns). The glass tube is filled with a high purity inert gas (such as nitrogen), and some reed switches are made to have a vacuum inside to enhance their high voltage performance.

The reed acts as a magnetic flux conductor. The two reeds are not in contact when not yet in operation; when passing through a magnetic field generated by a permanent magnet or electromagnetic coil, the applied magnetic field causes the two reeds to have different polarities near their endpoints, and when the magnetic force exceeds the spring force of the reeds themselves, the two reeds will be drawn together to conduct the circuit; when the magnetic field weakens or disappears, the reeds are released due to their own elasticity, and the contact surfaces will separate to open the circuit.



  1. Application code:

  • MicroPython:

from machine import Pin

import time


# Initialize LED and Reed Switch

led = Pin(14, Pin.OUT)

reed_switch = Pin(15, Pin.IN, Pin.PULL_UP)


while True:

if reed_switch.value() == 0: # Reed switch closed (magnet present)

led.value(1) # Turn on LED

else:

led.value(0) # Turn off LED

time.sleep(0.1)


  • Embedded C:

#include "pico/stdlib.h"


#define LED_PIN 14 // GPIO pin connected to LED

#define REED_SWITCH_PIN 15 // GPIO pin connected to Reed Switch


int main() {

// Initialize LED pin and Reed switch pin

gpio_init(LED_PIN);

gpio_set_dir(LED_PIN, GPIO_OUT);

gpio_init(REED_SWITCH_PIN);

gpio_set_dir(REED_SWITCH_PIN, GPIO_IN);

gpio_pull_up(REED_SWITCH_PIN); // Enable pull-up for Reed switch


while (1) {

// Read the state of the Reed switch

bool reed_switch_state = gpio_get(REED_SWITCH_PIN);

if (reed_switch_state == 0) {

// Magnet is near, Reed switch is closed

gpio_put(LED_PIN, 1); // Turn ON the LED

} else {

// Magnet is away, Reed switch is open

gpio_put(LED_PIN, 0); // Turn OFF the LED

}

sleep_ms(100); // Small delay to avoid rapid switching

}

}



  1. Hardware Image:



  1. Output:

When the Reed switch comes in contact with a magnetic field (i.e., the magnet is near the switch), the LED connected to the Pico microcontroller will light up. When the magnet is removed, the LED will turn off.

  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

# Define project name and source file

cmake_minimum_required(VERSION 3.12)


include(pico_sdk_import.cmake)


project(reed_switch_led_project)


pico_sdk_init()


add_executable(reed_switch_led

main.c

)


target_link_libraries(reed_switch_led pico_stdlib)


pico_add_extra_outputs(reed_switch_led)

  1. References:



Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)