Reed Switch with Raspberry Pi, interfacing and Python Programming
Table of Contents
Reed Switch with Raspberry Pi:
Reed Switch with Raspberry Pi– A very simple, but quite useful sensor is the reed switch or contact. The reed switch is also known as the magnetic reed switch. The reed switch has the contacts enclosed in a glass tube which can be controlled using a permanent magnet or electromagnet. When the magnet is near to the reed switch the contacts remain closed and when the magnet is away the contacts are open. This opening and closing of the contacts can be used as the signal, which can be used to perform some predefined functions. I have been using reed switches for quite a long time in different Arduino, ESP8266, ESP32, and Raspberry Pi based projects. I also have a very detailed tutorial now how to use a reed switch with the Arduino. Conceivable application possibilities are:
- Mounting on a door for open / closed detection
- Proximity sensor
- model maker
- Rotation sensor
- Detection of open windows or doors
Amazon Purchase Links:
Wireless Keyboard and Mouse for raspberry pi:
Night vision Camera for Raspberry Pi:
Oled HDMI touch display for raspberry pi:
Other Tools and Components:
Super Starter kit for Beginners
PCB small portable drill machines
The reed switch is available in different designs, however its functionality is always identical. Inside the switch there are two closely spaced metallic contact flags. If you approach the reed switch with a magnet or magnetic object closes the Contact inside and a conductive connection is created. You can say this is modified version of the mechanical switches or buttons, unlike the other buttons and switches, reed switches don’t need any physical contact as they work on the magnetic field.
The reed contact basically works like a button. A reed switch can be easily used with Raspberry pi. Seriously if you know how to use a pushbutton with the Raspberry Pi then there is no need to be worried about. Reed switch is just like the pushbutton. The only difference is that, the pushbutton ned a physical contact while the reed switch is operated with the help of a magnet. You can use the same program that you have written for the pushbutton, with the reed switch as well. The reed switch just like the pushbutton does not give you measured values, but only a logical 1 or 0. The contact is connected as usual to a free GPIO pin, which is used as an input. Place the other side of the reed contact against ground or 3.3 V. It depends on whether the logical 1 or the logic 0 in the closed or open state is recognized. In the circuit diagram (see Figure 1) the contact is on GPIO pin 15 connected. R1 serves as a pull-up resistor and is optional when used in Python Pull-up is activated for this pin. R2 is used to limit the current when the contact is closed. The sensor is queried in Python as usual by reading back the GPIO status of pin 15:
Reed Switch programming in Raspberry pi:
1 2 3 4 5 6 7 8 9 10 |
#! / usr / bin / python3 # Reed.py file import time, sys import RPi. GPIO as GPIO GPIO.setmode (GPIO. BOARD) GPIO setup (15, GPIO .IN, pull_up_down = GPIO.PUD_UP) while True: input = GPIO.input (15) print (input) time.sleep (1) |
The little Python program indicates the status of the reed contact every second. If it is not actuated, 1 appears, in the actuated state a 0 is output. Here, too, you can use events or interrupts as with the PIR sensor.