Magnetic Reed Switches

Magnetic Reed Switches

June 6, 2025·
Brigit Quezada

magnetic reed switches

Introduction

This tutorial is aiming to accomplish a better understanding of the basic functionalities of magnetic reed switches. In this tutorial we will learn about reed switches, how they work and how to integrate them using an ESP32 microcontroller.

Learning Objectives

  • Understand reed switch operation (NO vs NC)
  • Wire reed switches directly to ESP32-S3
  • Program basic switch detection
  • Monitor switch states via Serial output

Background Information

A magnetic reed switch is an electronic sensor that acts like a button or a switch. It is activated by the presence by a nearby magnetic field. It consists of two very thin ferromagnetic metal plates (reeds) sealed within a glass envelope filled with inert gas. In a normaly open switch, there is a small gap in between the two plates, so they are not touching eachother. In the presence of a nearby magnetic field, the two plates will bend sligly and come in contact with each other and then close the circuit. IN a normally clsoed switch, the two reed switches naturally have contact with eachother when there is no magnetic field present (this is opposite from the normally open switch). When a magnetic field is applied to these kinds of reed switches, they become magnitied by the same polarity and the magnetic force causes the reeds witches to oull apart, thus openeing the circuit and sopping the current flow.

Reed switches are used commonly in security systems, door/window sensors and proximity detection. They offer several advantages including no mechanical wear (contactless operation), hermetically sealed contacts that resist corrosion, and long operational life.

Pros:

  • No physical contact required for activation = Very reliable and long-lasting
  • Low cost and simple to implement
  • Works through non-magnetic materials (plastic, wood, glass)
  • No power consumption when inactive

Cons:

  • Limited switching speed compared to solid-state sensors
  • Can be affected by strong external magnetic fields

Key Concepts:

Normally Open (NO): Contacts are open when no magnetic field is present Normally Closed (NC): Contacts are closed when no magnetic field is present Actuation Distance: The distance at which the magnet activates the switch Hysteresis: The difference between activation and deactivation distances

Getting Started

This tutorial uses the ESP32 S3 mini microcontroller platform with the Arduino IDE. The ESP32 provides built-in WiFi capabilities and multiple GPIO pins, making it ideal for IoT sensor projects.

Required downloads and instillations:

Required Components:

Required Equiptment:

  • Computer with USB Port
  • USB-C cable for ESP32-S3 Development board

Part 01: Basic Reed Switch Circuit

Introduction

Build a simple circuit that detects reed switch activation using only the ESP32-S3 and Serial monitor output.

Objective

  • Connect reed switch directly to ESP32-S3 GPIO pins
  • Read and interpret digital switch states in code
  • Test switch functionality with magnetic activation

Background Information

In this tutorial, I learned basic C++ syntax for Arduino IDE, Digital input/ oputput pin configuration, conditional logic. I understood basic pull-up resistsor concepts, microcontroller programming fundamentals and digital signal interpretation.

Components

  • ESP32-S3 development board
  • Gebildet pre-wired reed switch (NO/NC configurable)
  • USB-C cable for programming and power
  • Computer with Arduino IDE installed

Instructional

For this tutorial, we start with connecting the reed swwitch to the ESP32 S3 development board. To do this, take the blue wire wich is the common termainal and connect it to the GPIO Pin 2 on the ESP32 S3 Development board. Connect the black wire to GND.

Next, open the Arduino IDE and create a new sketch with this test code:

const int REED_PIN = 2;

void setup() { Serial.begin(115200); pinMode(REED_PIN, INPUT_PULLUP); Serial.println(“Reed Switch Test Starting…”); }

void loop() { int switchState = digitalRead(REED_PIN);

if (switchState == HIGH) { // Magnet present (NC switch opens) Serial.println(“SWITCH OPEN - Magnet detected!”); } else { // No magnet (NC switch closed) Serial.println(“SWITCH CLOSED - No magnet”); }

delay(500); // Check every half second }

This configures GPIO 2 as an input with the internal pull-up resistor enabled using INPUT_PULLUP mode. This internal resistor makes it so that when the reed switch opens (magnet present), the GPIO pin is pulled HIGH to 3.3V, while when the switch is closed (no magnet), current flows through the switch to ground, making the pin read LOW. The main loop continuously reads the digital state of GPIO 2 using digitalRead(). When the function returns HIGH, it means the magnet has opened the normally closed switch, so we display “Magnet detected!” to the Serial monitor.

Upload this code to the ESP32-S3, open the Serial Monitor at 115200 baud rate, and test the system by bringing the reed switch pair close togther. The Serial output change from “SWITCH CLOSED - No magnet” to “SWITCH OPEN - Magnet detected!” as you bring them togther and then pull them apart.

Analysis

My example shows some interesting concepts that are important to understanding reed switches. First it incorperates pin configurations of the blue and black wires. Second it incorperates state change detection.

Additional Resources and Useful links

https://www.youtube.com/watch?v=iBjNpcQ4fG8

https://www.samgalope.dev/2025/01/18/using-a-reed-switch-sensor-with-esp32-magnetic-field-detection-made-easy/

https://mm.digikey.com/Volume0/opasdata/d220001/medias/docus/761/XBee_Cellular_Garage_Door_Sensor.pdf?_gl=11omkzh6_upMQ.._gs*MQ..&gclid=CjwKCAjwo4rCBhAbEiwAxhJlCYtfdXweoyJKg7wQ0OKW3vffvfQxPLxx54j6O2Ih62xsWlhw6O1W7BoCz88QAvD_BwE&gclsrc=aw.ds&gbraid=0AAAAADrbLljsNAWqyFGkERwFblLMZBnEk