Arduino Uno Reed Switch Module: Industrial Magnetic Position Sensing
Professional reed switch module contains hermetically sealed glass-enclosed ferromagnetic contacts that close (<0.5Ω) in 0.2ms when N52 neodymium magnets pass within 1-5cm detection envelope. Unlike Hall sensors, zero power consumption in open state enables battery-powered door alarms with 10+ year life.
Digital pin 2 interrupt captures instantaneous state changes with LM393 comparator providing clean 5V TTL output. Sensitivity pot adjusts 10-50 gauss trigger threshold. 3.3-5V operation, <1μA quiescent current.
How It Works
A reed switch is two ferromagnetic blades sealed inside a glass tube, their ends overlapping but not touching. Bring a magnet close and the blades magnetise with opposite polarities, attract each other and snap together, closing the circuit. Remove the magnet and their own springiness pulls them apart.
Unlike a Hall-effect sensor, a reed switch is entirely passive — it needs no supply to operate, because the magnet provides the energy. That makes it ideal for battery-powered and intrinsically safe applications, and it is why door sensors in alarm systems have used reed switches for decades.
It is also omnipolar: either pole of a magnet closes it, unlike a unipolar Hall switch that responds to only one. That removes a whole class of "my sensor does not work" problems.
The limitations are mechanical. The blades are glass-sealed and break if the leads are bent close to the body, they bounce on closing like any mechanical contact, and switching life is finite — though in the millions of operations for light loads.
Components Needed
- Arduino Uno
- Reed Switch Sensor Module (glass reed)
- N52 Neodymium test magnet (10x3mm)
- Male-to-male jumper wires (3 pieces)
- Arduino Uno
- 220Ω alarm LED + buzzer driver
Wiring to the Arduino Uno
For a bare reed switch, wire one leg to D2 and the other to GND, then declare the pin INPUT_PULLUP. No supply or external resistor is needed — the switch is passive.
For a carrier module such as the KY-021, connect S to D2 plus VCC and GND for its indicator LED. Some carriers invert the output, so check the sense before relying on it.
Never bend the leads close to the glass. Hold the lead with pliers between the body and the bend, or the seal cracks and the contacts corrode. This is the most common way these parts are destroyed during assembly.
| Connection | Arduino Uno pin | Function |
|---|---|---|
| Reed leg 1 | D2 | Input pin, with internal pull-up |
| Reed leg 2 | GND | Pulls the pin low when the magnet is near |
| Module VCC | 5V | Only needed on carrier boards with an LED |
Example Code
Door-contact monitoring with debouncing and open-duration tracking. Upload it with the board set to Arduino Uno and open the Serial Monitor at 9600 baud.
const int REED_PIN = 2;
const unsigned long DEBOUNCE_MS = 30;
int stable = HIGH, lastRead = HIGH;
unsigned long lastChange = 0, openedAt = 0;
void setup() {
Serial.begin(9600);
pinMode(REED_PIN, INPUT_PULLUP); // LOW = magnet present = closed
}
void loop() {
int reading = digitalRead(REED_PIN);
if (reading != lastRead) { lastChange = millis(); lastRead = reading; }
if (millis() - lastChange > DEBOUNCE_MS && reading != stable) {
stable = reading;
if (stable == HIGH) { // magnet moved away
openedAt = millis();
Serial.println("DOOR OPENED");
} else {
Serial.print("door closed after ");
Serial.print((millis() - openedAt) / 1000.0, 1);
Serial.println(" s");
}
}
}
Applications
A reed switch module turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:
- Door and window contacts in security systems
- Lid and enclosure interlocks on equipment
- Water, gas and electricity meter pulse counting
- Rotation and speed sensing with a magnet on a wheel
- Float switches for liquid level, with a magnet in the float
Working with the Arduino Uno
The Arduino Uno is built around the ATmega328P and runs on 5 V logic with 2 KB of SRAM and 32 KB of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.
The Uno runs at 5 V, so most hobby sensor modules connect directly with no level shifting.
With only 2 KB of SRAM, avoid large buffers and prefer the F() macro for constant strings.
The single hardware UART is shared with the USB connection, so heavy Serial printing competes with uploads.
| Arduino Uno characteristic | Value | Why it matters here |
|---|---|---|
| Logic voltage | 5 V | Matches most hobby modules directly |
| ADC resolution | 10-bit (0–1023) | Sets how finely an analog reading can be resolved |
| Analog inputs | A0–A5 (six channels) | Determines how many analog sensors can share the board |
| PWM outputs | D3, D5, D6, D9, D10 and D11 | Needed for brightness, speed and tone control |
| I²C pins | A4 (SDA) and A5 (SCL) | Fixed by hardware — wiring copied from another board may not match |
| Interrupt pins | D2 and D3 only | Required for counting fast or asynchronous events |
| Serial | a single hardware UART shared with USB | Monitor runs at 9600 baud by default |
Troubleshooting
Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:
- It never closes — the magnet is too weak or too far. Reed switches typically need the magnet within 10–20 mm.
- The state flickers — mechanical bounce. Debounce as the sketch does.
- It closed once and stayed closed — the contacts have welded, usually from switching too much current. Reed contacts are rated for very light loads only.
- The switch broke during mounting — the glass seal cracked when the leads were bent too close to the body.
- It triggers from a nearby motor — stray magnetic fields affect it. Move it away or shield it.
- Code written for an ESP board gives odd analog values — the Arduino Uno uses a 10-bit ADC returning 0–1023; rescale any constant taken from a 12-bit example.
- An I²C sensor is not found after copying wiring from another Arduino — on the Arduino Uno I²C is on A4 (SDA) and A5 (SCL).
Taking It Further on the Arduino Uno
Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the Arduino Uno specifically:
The Uno’s shield ecosystem is its real advantage. Once the circuit works on a breadboard, a prototyping shield turns it into something permanent that still stacks with a data-logging or Ethernet shield without rewiring.
Logging to an SD card via a shield is the natural next step, since the Uno has no onboard storage and no network. Timestamp each reading with a DS3231 real-time clock so the log survives power cuts with correct times.
Because SRAM is limited to 2 KB, keep logged strings short and write them out immediately rather than buffering. Building a long String in memory is the most common cause of an Uno sketch that runs for hours and then freezes.
Notes and Practical Limits
Never switch a load directly with a reed switch. Contact ratings are often well under 500 mA, and inductive loads weld them instantly. Use the reed to signal the microcontroller, and let a relay or MOSFET carry the current.
Compared with a Hall sensor, the reed wins on power — zero quiescent current — and on polarity tolerance, but loses on lifetime and vibration resistance. For a battery door sensor the reed is the right answer; for a wheel spinning at speed, use a Hall sensor.