How to Build a Motion Detection System Using Raspberry Pi and Twilio WhatsApp API
Time to read: 4 minutes
Let's say you want to know if there is human movement in your room or house. In this tutorial, you will learn how to build a motion detection system and get notifications when motion is detected in your room via WhatsApp text using the Twilio Programmable Messaging API for WhatsApp. At the end of this tutorial, you will have developed a smart system that will send WhatsApp texts from Raspberry Pi — how awesome!
Prerequisites
To complete this tutorial, you need:
- A Twilio account.
- An active WhatsApp account, to test the project.
- A Raspberry Pi (ideally a 3B+ or newer) running Raspberry Pi OS (formerly Raspbian), powered by either a USB cable or wall adapter.
- A PIR infrared motion sensor (HC-SR501).
- Three female-to-female jumper wires.
The HC-SR501 is one of the most used motion sensors. This sensor has two modes, which are single trigger mode and repeatable mode. You can learn more about the sensor here. In this tutorial, you will be using the repeatable trigger mode.
Connect the HC-SR501 sensor to the Raspberry Pi
Start building your motion detection system by configuring the sensor's settings.
The following picture from the sensor's description explains how to adjust your sensor.
Pick up the sensor and adjust the orange potentiometers, turning both of them fully counter-clockwise. Then, set the jumper set to the repeatable trigger setting. When you are finished, your sensor should look like the photo below:
Now, you are ready to connect the jumpers to the sensor and the sensor to the Raspberry Pi.
This motion sensor has three pins, whose names you can find under the white domed lens. If you remove this dome, you will see that the pins are labeled VCC (power), OUT (output), and GND (ground). Take three jumper wires of different colors. For my sensor, I have chosen blue, white, and green jumpers, but you can select the colors you like.
First, connect the jumpers to the sensor:
- Connect the VCC of the sensor to the blue jumper.
- Connect the OUT of the sensor to the white jumper.
- Connect the GND of the sensor to the green jumper.
Second, connect the above connection to the Raspberry Pi:
- Connect the blue jumper to the GPIO VCC pin.
- Connect the white jumper to the GPIO 23 pin.
- Connect the green jumper to the GPIO GND pin.
If you removed the white lens from the sensor, reattach it now.
The final circuit will look like the image below.
Configure your Twilio WhatsApp Sandbox
Now, log into your Twilio Console to configure your Twilio WhatsApp Sandbox settings. Click on the Messaging tab in the left-side navigation menu. Under Messaging, click on Settings and select WhatsApp Sandbox Settings.
To start using the Twilio WhatsApp Sandbox, use your WhatsApp account to send the unique phrase specified on your console (under the Sandbox Participants header) to the WhatsApp number assigned to your sandbox. Mine is “join found-shadow”.
You should receive a message on your WhatsApp as shown below.
Create the Python script to read the HC-SR501 sensor
At this point, your hardware circuit is ready, and now you just need software.
So now let's write out a script to read data from the HC-SR501 sensor. You can connect to your Raspberry Pi via SSH protocol, but this is not the only way to access Raspberry Pi. If you prefer, you can access the Raspberry Pi's terminal through its Desktop if you have a monitor, keyboard, and mouse connected.
First, install Twilio and RPI.GPO, by running the following commands:
Create a folder for your script, name it motion_detection, and open it by entering the following commands on your terminal:
Now, in the motion_detection folder, create your Python script, name it motion.py, and open it with nano.
sudo nano motion.py
will open your script so you can start coding. If you don't want to use nano, you can use a text editor of your choice.
Import your imports in the beginning of your script:
In this project, we will use GPIO
to access and read the pins on the Raspberry Pi, time
to put some delays in our program, and twilio
to access the Twilio WhatsApp API.
Now that you have your imports, you can access their functions. We will use pin 23 to read the sensor, and for that, enter the following code:
We are done with setting up GPIOs, so let's dive into the Twilio WhatsApp API. For this, you will need your Account SID and Auth Token, which you can find in the Twilio Console under Account Info.
- Account SID - Used to identify yourself in API requests.
- Auth Token - Used to authenticate REST API requests.
Now that you have your account credentials, initialize the Twilio client with the following code, replacing the placeholder text with your values for Account SID and Auth Token, and add your WhatsApp number in E164 format:
Now you are all set and can make a request to the Twilio WhatsApp API. Twilio makes it easy for us, and we will use the following function to send our message:
There are three properties that we need to pass here.
body
- This is your message.from_
- This iswhatsapp:
combined with your sandbox number.to
- This iswhatsapp:
combined with the number for your personal WhatsApp Account.
Now we need to listen to the events from the GPIO pin, checking if the sensor detected any movement. GPIO has made it easy for us — we can just call the built-in function and pass our function (alertUser()
) as a callback. Let's do that by entering the following code:
Line 2, from the above code, is calling alertUser()
once there is movement detected from the sensor. On lines 3 to 4, we add some delay. In line 5, we listen for a keyboard interrupt to exit the program.
The complete code is as follows:
Happy days!! Now, we can run the program, using the following command from within the project directory:
After running this command, wave your hand over your sensor, and after some seconds, you should receive a text message on your personal WhatsApp. Here is the output from mine.
Yes, I did a few tests there…
Now you can put your hardware in your room — just make sure it is connected to the internet so that once motion is detected, you will be notified. Let me know if you have any questions or comments!
Mahlomola Moses Mothogoane is a Developer at Falcorp, a game developer, and a hardware and software developer. Feel free to reach out to me on LinkedIn and Twitter or send me an email at mahlomolamoses@gmail.com.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.