How to Test Reed Switch with Raspberry Pi

MechatronicsLAB
3 min readJun 9, 2021

--

A reed switch detects the presence of a magnet. A cylindrical glass tube that detects the current magnet field from a magnet is the main feature of a reed switch. In this guide, I show you how to use the raspberry pi to test a reed switch.

Parts List

if you want to complete How Test Reed Switch in Raspberry pi Tutorial, you have to need the following component

  1. Raspberry Pi — — — amazon/ banggood/ aliexpress
  2. Reed switch — — — amazon/ banggood/ aliexpress
  3. Breadboard — — — amazon/ banggood/ aliexpress
  4. Connecting Wires — amazon/ banggood/ aliexpress

Schematic Diagram

Reed Switch in Raspberry pi
Reed Switch in Raspberry pi

Raspberry Pi— — — — Reed switch Connection

3.3v — — — — — — VCC
GND — — — — — — -GND
GPIO18 — — — — -D0

Programming

# Reed Switch Test
import RPi.GPIO as GPIO

ReedSwitchPin = 14
RawValue = 0

# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(ReedSwitchPin, GPIO.IN)
print(“Reed Switch Test ….”)

try:
while 1:
RawValue = GPIO.input(ReedSwitchPin)
if (RawValue == 0):
print(“Reed Switch Detects Magnet
… “)
else:
print(“No Magnet Detected …”)

# 1 is off, 0 is activated by magnet
print(“ , RawValue: “, RawValue)

except KeyboardInterrupt:
pass
print(“Exiting Reed Switch Test …”)
GPIO.cleanup()

Start the Raspberry Pi program. It would help if you displayed the current status of the magnetic field detection and the corresponding raw value read from the reed switch. Close a magnet to the reed switch. The readings on the terminal should change from the absence of a magnetic field to the presence of a magnet field. Remove the magnet from the reed switch and revert to the original reading that no magnetic field is detected.

Program Analysis

import RPi.GPIO as GPIO
Imports the RPi.GPIO library into the program to be accessed as GPIO.

ReedSwitchPin = 14
Assigns pin 14 of the Raspberry Pi as the pin connected to the output from the reed switch.

RawValue = 0
Assigns pin 14 of the Raspberry Pi as the pin connected to the output from the reed switch.

GPIO.setmode(GPIO.BCM)
Sets the pin numbering system of the Raspberry Pi to GPIO.BCM.

GPIO.setup(ReedSwitchPin, GPIO.IN)
Sets the pin on the Raspberry Pi connected to the reed switch as an input pin to read the voltage value on that pin.

print(“Reed Switch Test ….”)
Print a message to the terminal that initialized the program.

try:
while 1:
Does the following until the user presses CTRL-C, which generates a keyboard interrupt:

RawValue = GPIO.input(ReedSwitchPin)
Reads the output value generated by the reed switch sensor.
if (RawValue == 0):
print(“Reed Switch Detects Magnet … “)
If 0, what detected a magnetic field; otherwise, what detected no magnetic field. Prints a text message to the terminal that indicates the result.

else:
print(“No Magnet Detected …”)

print(“ , RawValue: “, RawValue)
Prints out the raw value read from the sensor to the terminal.

except KeyboardInterrupt:
pass

print(“Exiting Reed Switch Test …”)
Prints out a text message to the terminal indicating that the program is exiting

GPIO.cleanup()
Deallocates resources related to the GPIO pins.

More Complete Guide to Raspberry Pi4

--

--

MechatronicsLAB
MechatronicsLAB

Written by MechatronicsLAB

0 Followers

Our main focus will be on Mechatronics Technology where we will work in #Industrial_Automation, #PLC, #IoT, #Raspberry_Pi, #Arduino, #Microcontroller

No responses yet