Raspberry Pi Reed Switch Door Alarm

MechatronicsLAB
3 min readJun 15, 2021

in this tutorial, I will show you how to build a Raspberry Pi Reed Switch Door Alarm.

Parts List

if you want to complete Raspberry Pi Reed Switch Door Alarm 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

Raspberry Pi — — — — Reed switch Connection

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

Program

import RPi.GPIO as GPIO
import pygame
from time import sleep

ReedSwitchPin = 14
RawValue = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(ReedSwitchPin, GPIO.IN)

pygame.init()
pygame.mixer.init()
Alarm = pygame.mixer.Sound(“match1.wav”)
print(“Reed Switch Door Alarm ….”)

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

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

You will need to determine which sound file you want for the program before running the program. The file I used was a WAV sound file in the directory of pigames that was installed by default in Raspberry Pi. You will need to copy any sound file to the same directory from which the program is running. You also need to change the name of the sound file to match the name of the sound file you copied.

Start the Raspberry Pi program with Python. On the terminal should be displayed the current status of magnetic field detection and the respective 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 absence of a magnetic field. A sound effect should also start playing through the speaker of your monitor. Remove the magnet from the reed switch, and the readings should be returned when no magnetic field is detected. The previously played sound effect should stop now.

Program Analysis

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

import pygame
Imports the pygame library into the program so that it can be used.

from time import sleep

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.

pygame.init()
Initializes the pygame library.

pygame.mixer.init()
Initializes the sound mixer.

Alarm = pygame.mixer.Sound(“match1.wav”)
Specifies a sound file and creates an object called Alarm from that sound file. The specific sound file we used is called match1.wav and is one of the demo programs that have been installed using the standard Raspberry Pi.

print(“Reed Switch Door Alarm ….”)
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 a magnetic field is detected, then the program plays the sound effect created previously by calling the Alarm.play() function. See Listing 5–4.

Alarm.play()
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.

--

--

MechatronicsLAB
0 Followers

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