Raspberry Pi - Motion Sensor - Relay

This tutorial instructs you how to use Raspberry Pi and motion sensor to control relay. In detail:

By connecting a relay to a light bulb, LED strip, motor, or actuator... We can then utilize Raspberry Pi and the motion sensor to control the light bulb, LED strip, motor, or actuator...

This can be applied in an automation process that triggers actions upon detecting human presence.

Hardware Preparation

1×Raspberry Pi 5
1×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion Sensor
1×Relay
1×Optionally, Solenoid Lock
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Relay and Motion Sensor

If you are unfamiliar with relay and motion sensor (including pinout, functionality, and programming), check out the following tutorials:

Wiring Diagram

The wiring diagram between Raspberry Pi and Motion Sensor Relay

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

Initial Setting

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
arduino motion sensor initial setting

Raspberry Pi Code

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file motion_sensor_relay.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-motion-sensor-relay """ import RPi.GPIO as GPIO # Set the GPIO mode (BCM or BOARD) GPIO.setmode(GPIO.BCM) # Define the GPIO pin number to which the relay is connected RELAY_PIN = 12 # Define the GPIO pin number to which the motion sensor is connected MOTION_SENSOR_PIN = 14 # Set up the GPIO pins GPIO.setup(MOTION_SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Input with pull-up resistor GPIO.setup(RELAY_PIN, GPIO.OUT) # Output try: while True: motion_state = GPIO.input(MOTION_SENSOR_PIN) if motion_state == GPIO.HIGH: print("The movement is detected") GPIO.output(RELAY_PIN, GPIO.HIGH) # Turn the relay on else: print("The movement is stopped") GPIO.output(RELAY_PIN, GPIO.LOW) # Turn the relay off except KeyboardInterrupt: GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the relay GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 motion_sensor_relay.py
  • Move your hand in front of the sensor.
  • Check the relay state, you will see that the relay is turned on when the movement is detected

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Relay vs MOSFET Module

A relay is not the only way to switch a device. If your device runs on DC power, such as a 12V LED strip, a 12V solenoid lock or a 12V fan, a MOSFET module can do the same job.

Relay MOSFET Module
What it can do turn the device on or off turn the device on or off, and set its power (PWM)
Noise when switching a click every time no sound
How fast it switches slow, a few milliseconds very fast
Life the contacts wear out after many switches nothing moves, so it lasts long
Type of power AC or DC DC only
Device power supply any voltage the relay is rated for about 6V or more
Isolation from the Raspberry Pi yes, between coil and contacts yes, by the PC817 optocoupler
Good for AC devices, or a simple ON/OFF DC devices, or dimming and speed control

Both the relay and the MOSFET module turn on when the Raspberry Pi pin is HIGH (GPIO.output(pin, GPIO.HIGH)). So for a simple ON/OFF, the code above stays the same. Only the wiring changes. The 3.3V signal from the Raspberry Pi pin is enough for the MOSFET module. For AC mains devices, always use the relay.

To learn the MOSFET module pinout, wiring, PWM code and safety tips, see the Raspberry Pi - MOSFET Module tutorial.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!