Raspberry Pi - Motion Sensor - LED

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

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

Hardware Preparation

1×Raspberry Pi 4 Model B
1×HC-SR501 Motion Sensor
1×220 ohm resistor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
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. We appreciate your support.

Overview of LED and Motion Sensor

If you are not familiar with LED and motion sensor (including pinout, how it works, how to program, etc.), the following tutorials will help you:

Wiring Diagram

The wiring diagram between Raspberry Pi and Motion Sensor LED

This image is created using Fritzing. Click to enlarge image

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_led.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-led import RPi.GPIO as GPIO import time # Set the GPIO mode to BCM (Broadcom SOC channel numbering) GPIO.setmode(GPIO.BCM) # Define the GPIO pins for the HC-SR501 PIR motion sensor and LED PIR_PIN = 17 LED_PIN = 18 # Set up the PIR sensor and LED pins GPIO.setup(PIR_PIN, GPIO.IN) # PIR sensor as input GPIO.setup(LED_PIN, GPIO.OUT) # LED as output try: while True: # Check if the PIR sensor detects movement (1 represents HIGH signal) if GPIO.input(PIR_PIN) == GPIO.HIGH: print("Motion detected!") GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED else: GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED if no motion time.sleep(0.1) # Add a small delay to avoid excessive looping except KeyboardInterrupt: GPIO.cleanup() # Clean up GPIO on program exit
  • Save the file and run the Python script by executing the following command in the terminal:
python3 motion_sensor_led.py
  • Move your hand in front of the HC-SR501 motion sensor and check out LED's state.

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!

Video Tutorial


  • 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!