Programming On Raspberry Pi With Python: Activate LED and Buzzer on Motion Detection

In previous post we started exploring hardware side of Raspberry Pi. We learned how to make connections with GPIO pins and control the LED. Lets go one step further and write program to detect motion and send the notifiction on telegram channel.


In this article we will cover below things:

- Printing message on terminal when motion is detected
- Connecting PIR motion detection sensor, buzzer and LED to GPIO pins
- Activating the buzzer and LED on motion detection


We are using Raspberry Pi 3 B+ model.


Requirements:

We will require below components (apart from Raspberry Pi obviously :D ) to make this work.

- Breadboard
- Jumper Wires
- Buzzer (Optional)
- LED 
- Resistors
- PIR motion detection sensor



Printing message on terminal when motion is detected:

Here we will print a simple message 'motion detected' on the screen when motion is detected by PIR motion detection sensor. To know more about working of PIR motion detection sensor please refer this wikipedia article.


Connections:

PIR motion detection sensor have three pins. If you remove the plastic cover from the PIR sensor, you will be able to see which pin needs to be connected to where. Refer the picture below.


pir motion sensor


Left pin needs to be connected to 5v pin, middle pin will be send the output when motion is detected and right pin will be connected to ground pin.

So we connect the left pin to 2nd pin of GPIO set on Raspberry Pi. Middle one to any pin which can be set to receive the output, 11 in our case and right pin to 6th pin. See the image with connections below.


pir motion sensor connections

Connection diagram:

pir motion sensor connections


Python Program:

Now lets understand the code which print message on screen when motion is detected.

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# Read output from PIR motion sensor
GPIO.setup(11, GPIO.IN)  

while True:
    inp = GPIO.input(11)
    # When output from motion sensor is HIGH
    if 1 == inp:               
        print("motion detected")

    time.sleep(0.1)

As discussed in previous article as well, we set the mode to GPIO.BOARD which means pin number is its position on board. We set pin number 11 to read the output from PIR sensor.

We check if output from pin number 11 is high, if yes print message on screen. Then sleep for 0.1 seconds and repeat it.


Activate the python3 virtual environment. Install RPi.GPIO package is not installed already. pip install RPi.GPIO.

Save the above program and run it. Move your hand in front of sensor and you will see the output on screen. 

python pir_basic.py


AAAAAElFTkSuQmCC

If motion is not detected, please make sure connections are correct. Try tuning the two knobs on PIR sensor.



Connecting Buzzer and LED:

This time when motion is detected we will activate a buzzer and turn on the LED. 

This is the first time we will be connecting more than one component on breadboard. Please follow below instructions to make connections.

- Connect pin 2 (5v) to live column of breadboard.
- Connect pin 6 (ground) to ground column of breadboard.
- Connect left pin (5v) of PIR sensor to live column of breadboard.
- Connect middle pin (output) to pin 11 of GPIO pin set of Raspberry Pi.
- Connect right pin (ground) to ground column of breadboard.
- Connection of PIR sensor is done using breadboard. Now lets connect the LED and buzzer.
- Connect the positive end of LED (longer leg) to pin 36 but with 220 ohm resistor in between. Refer previous article
- Connect the neutral end of LED (smaller leg) to ground column on breadboard.
- Connect the Positive terminal of buzzer to pin 36. Connect negative terminal to ground column of breadboard.


Refer below diagram and picture for connections.


pir motion sensor connections


motion detection


Python Code:

Python code for these connections will be almost same with only difference of sending output to pin 36 whenever motion is detected.

Use below code. Comment has been added with each line to make it easy to understand.


#
# Motion detection with buzzer and LED
# Author - https://www.pythoncircle.com
#

import RPi.GPIO as GPIO
import time

LED_PIN = 36
GPIO.setwarnings(False)
# refer pins by their sequence number on board
GPIO.setmode(GPIO.BOARD)
# Read output from PIR motion sensor
GPIO.setup(11, GPIO.IN)
# LED output pin
GPIO.setup(LED_PIN, GPIO.OUT)

while True:
    i = GPIO.input(11)
    # When output from motion sensor is LOW
    if i == 0:
        print("No motion")
        # Turn OFF LED and buzzer
        GPIO.output(LED_PIN, 0)
    # When output from motion sensor is HIGH
    elif i == 1:
        print("motion detected")
        # Turn ON LED and buzzer
        GPIO.output(LED_PIN, 1)

    time.sleep(0.1)


When you run the above code and motion is detected, LED is turned on and buzzer is activated. 

If something is not working for you, feel free to comment.




Related Articles:
Programming on Raspberry Pi with Python: Raspberry Pi Setup
How to setup Raspberry Pi. Installing Raspbian on Raspberry Pi 3 B+ model and configure settings. Starting with Raspberry Pi programming with Python....
Programming on Raspberry Pi with Python: WIFI and SSH configuration
How to setup Raspberry Pi. Installing Raspbian on Raspberry Pi 3 B+ model and configure settings. Starting with Raspberry Pi programming with Python. Configuring WIFI on Raspberry PI, Enabling SSH on Raspberry PI....
Programming on Raspberry Pi with Python: Sending IP address on Telegram channel on Raspberry Pi reboot
How to setup Raspberry Pi. Starting with Raspberry Pi programming with Python. Sending IP address to Telegram channel on Raspberry Pi Boot up. SSH using Raspberry Pi...
Programming on Raspberry Pi with Python: Controlling LED
Starting with Raspberry Pi programming with Python. Controlling LED with Python Program, Blinking LED with Raspberry Pi, Using GPIO pins to control the LED on Raspberry Pi with Python Program...

1 thought on 'Programming On Raspberry Pi With Python: Activate Led And Buzzer On Motion Detection'
Hamza Ata :
hi I just had a question I am not running the program from command line but from IDLE (3.5.3)i suppose it t should be the same thing however when i just connected my motion sensor befor even connecting my LED or the buzzer I tested the program and it runs but the output shows motion detected constantly and keeps looping until i exit the program I tried tuning the knobs all the way down the PIr is on a chair nothing is in motion it just keeps detecting motion is it because its a faulty PIR ?

Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:






© pythoncircle.com 2018-2019