Motion
Interfacing Reed Switch Motion with Raspberry Pi
What is the Reed Switch?
Reed Switch practical reference
A reed switch is a simple electromechanical switch made of two ferromagnetic metal reeds sealed inside a glass tube. The reeds snap together to close the circuit whenever a magnet passes near them, and spring apart when the magnet moves away. With no power draw and no moving parts beyond the reeds themselves, it's the classic low-cost sensor for detecting whether a door or window is open or closed in DIY security systems, and for simple proximity or rotation-counting triggers.
Key specifications
- Switch type: Normally-open (NO) magnetic reed switch — normally-closed (NC) variants also exist
- Operating voltage: Passive component — rated for typical logic-level or low-voltage switching, commonly up to 100-200V depending on part
- Switching current: ~0.5A max typical for small glass reed switches
- Actuation distance: ~10-15mm typical, depends on magnet strength and reed sensitivity rating
- Response time: Sub-millisecond mechanical closure, effectively instantaneous for most microcontroller polling rates
- Interface: Simple digital I/O — read as a plain HIGH/LOW switch input with a pull-up or pull-down resistor
- Package: Often sold pre-wired in a two-part door/window sensor housing with the matching magnet included
Pinout and connection notes
Wire one lead to a digital input pin and the other to GND, then enable the microcontroller's internal pull-up resistor (INPUT_PULLUP) so the pin reads HIGH when open and LOW when the magnet closes the switch — this avoids needing an external resistor for a simple door/window sensor build.
- Lead 1: One side of the switch contact — wire to a digital input pin
- Lead 2: Other side of the switch contact — wire to GND (or VCC, depending on pull configuration)
Integration checklist
- Confirm the exact part number and breakout-board revision.
- Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
- Check controller pin assignments and bus address or interface mode against the example code.
- Power up with external loads disconnected, then test signals before full integration.
- If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.
Troubleshooting and reproducibility
Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.
Setting Up Reed Switch with Raspberry Pi
Library
gpiozero
Usually preinstalled on Raspberry Pi OS; otherwise: pip3 install gpiozero
Wiring
| Component pin | Raspberry Pi pin | Note |
|---|---|---|
| Lead 1 | GPIO17 (physical pin 11) | gpiozero enables the internal pull-up automatically for Button |
| Lead 2 | GND |
Swipe sideways to see every column →
- gpiozero's Button class is a natural fit here even though this isn't a pushbutton — "pressed" just means the circuit is closed, which for a reed switch means the magnet is in range.
- Same door/window sensor NO-vs-NC caveat as the Arduino Uno if you bought a pre-wired kit.
- For a battery-powered project, reed.when_activated / reed.when_deactivated callbacks are more efficient than polling in a loop.
Related Components
IR Obstacle Sensor
Short-range IR proximity sensor with an adjustable threshold — perfect for line-following robots.
RCWL 0516
Microwave radar motion sensor that detects movement through walls, glass, and plastic enclosures.
TTP223
Touch-sensitive pad for replacing mechanical buttons — perfect for clean, sealed enclosures.
Related Guides & Projects
Raspberry Pi Pico W and Pico 2 W: Install MicroPython with Thonny or UF2
Hold BOOTSEL, drag the correct UF2 file, open the REPL and blink the onboard LED. Includes the Pico vs Pico W vs Pico 2 W firmware differences and what to do when the drive does not appear.
Raspberry Pi Pico W Web Server in MicroPython: Control an LED and Read a Sensor
Connect the Pico W to WiFi, serve a responsive control page, switch an LED and display the onboard temperature or an external sensor, with timeouts and reconnect handling.
Raspberry Pi Pico Oscilloscope with MicroPython
Build a Raspberry Pi Pico MicroPython waveform viewer with ADC sampling, an SSD1306 OLED, and optional USB serial plotting on a computer.
Self-Hosted Nextcloud NAS on Raspberry Pi 4
Build a private, durable Nextcloud NAS on a Raspberry Pi 4 -- SSD boot instead of a failure-prone SD card, Docker Compose with Redis caching, Tailscale for remote access with zero ports forwarded, and an automated backup strategy that actually runs.