This tutorial demonstrates how to use a magnetic reed switch. I’ll do a quick overview on how it works and show a project example using an Arduino.
A magnetic contact switch is basically a reed switch encased in a plastic shell so that you can easily apply them in a door, a window or a drawer to detect if the door is open or closed.
The switch that we are going to use has two parts: the switch itself, that usually comes opened, and the magnet. When you buy this switch, it also comes with 4 screws, so that you can attach it to your door.

It’s very very simple.
The electrical circuit is closed when a magnet is near the switch (less than 13 mm (0.5’’) away). When the magnet is far away from the switch, the circuit is open. See the figure below.

These switches are very cheap. You can buy them on ebay here.
In this example, we will turn on a red LED if your door is open and a green LED if your door is closed.
You’ll need the following components:
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Here’s the schematics for this example.

For this example, upload the following code:
/* Created by Rui Santos All the resources for this project: https://randomnerdtutorials.com/ */ int ledOpen=8; int ledClose=10; int switchReed=6; void setup(){ pinMode(ledOpen, OUTPUT); pinMode(ledClose, OUTPUT); pinMode(switchReed, INPUT); Serial.begin(9600); } void loop(){ if (digitalRead(switchReed)==HIGH){ digitalWrite(ledOpen, LOW); digitalWrite(ledClose, HIGH); Serial.println("Your Door is Closed"); } else { digitalWrite(ledOpen, HIGH); digitalWrite(ledClose, LOW); Serial.println("Your Door is Open"); } delay(1); }
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION