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.
We will require below components (apart from Raspberry Pi obviously :D ) to make this work.
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.
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.
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.
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.
If motion is not detected, please make sure connections are correct. Try tuning the two knobs on PIR sensor.
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.
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.