Skip to content

Raspberry Pi

#11-Making an Alarm Circuit using Raspberry Pi 4

by Robotistan Shopify 27 Apr 2023

Making an Alarm Circuit using Raspberry Pi 4 

Hello friends, in this project, we will make an alarm circuit using a motion detector and a relay module to trigger the Raspberry Pi. Let's get started if you are ready!


Required Materials:



What is a Relay?

A relay is an electromagnetic device for automatic or remote control. When energy is supplied to the relay coil, the magnetized coil moves the armature to connect the contacts with each other, and transmission is established in the circuit.



There is a copper coil around an iron core inside the relay. Current flows through the coil, and it generates a magnetic field. This circumstance causes the armature to pull, and because of this process, the contact opens or closes.

Inside of the Relay


 You can check the connections of the relay module below. If you need a relay module, you can visit this page.

Relay Module Connections


How Does a relay Work?


When a relay module receives a logic 0 signal (0 V) from the “IN” pin, the NO and COM terminals complete the circuit. If it is desired to use it in reverse, the NC (normally closed) terminal would be used instead of the NO (normally open) terminal.

The Logic Behind Relay


Raspberry Pi Motion Sensitive Relay Module Wiring Diagram

Relay Module Wiring Diagram

Raspberry Pi Motion Sensitive Relay Module Python Code

import time

import RPi.GPIO as io



pir_pin = 24   # GPIO pin

power_pin = 23 # Signal pin

io.setup(pir_pin, io.IN)  # PIR is connected to the input.

io.setup(power_pin, io.OUT) # Relay is connected to the output.

io.output(power_pin, True) # To keep the relay closed.

while True:

    if io.input(pir_pin): #We check if the sensor detects motion

        print("POWER ON")

        io.output(power_pin, False)

        time.sleep(20) # If it detects motion, it triggers the relay and keeps it open for 20 seconds.

        print("POWER OFF")

        io.output(power_pin, True)

        time.sleep(5) # After, it closes for 5 seconds.



Thanks to this code, the PIR sensor triggers the relay after detecting any motion and stays open for 20 seconds. Later, the relay closes, and after 5 seconds, this loop starts again. This simple motion detector can be used as a security system by connecting the output of the relay to an alarm or video recorder, etc.


Keep Learning More about Raspberry Pi 4


Raspberry Pi is a platform with many advantages and is admired by other developers. With the support and convenience in electronic projects, computer science, and robotics studies, Raspberry Pi makes learning enjoyable, and this module is developing day by day. Don’t you also want to improve further? We take you on a journey with Raspberry Pi to improve your Linux knowledge, dive into the electronics world, and carry out fun projects. Keep following the Raspberry Pi training series that has been prepared for you.


You can visit our online store and meet your material needs.

The Raspberry Pi model we used in this project can be easily accessed from this link.


Prev Post
Next Post
Someone recently bought a
[time] ago, from [location]

Thanks for subscribing!

This email has been registered!

Shop the look

Choose Options

Have Questions?
Back In Stock Notification
this is just a warning
Shopping Cart
0 items