In this article I will show you how to create a very simple alarm using your Raspberry Pi, a proximity sensor, one LED and some speakers. What you will build in this tutorial is an alarm that sets off with a visual and audio alert whenever a movement is detected by the proximity sensor. For example, you can see this tutorial as a toy example of a system that will detect if a door has been opened. As it uses the I2C interface, it is easy to extend this tutorial to more sensors. Let’s dive in !
For this project, you will of course need a Raspberry Pi board. You can get a board here. You also need a Debian Linux OS installed on an SD card to operate your Pi board. You can get an already programmed SD card here.
You will also need a breadboard to plug the LEDs, as well a way to connect your Raspberry Pi to the breadboard: you can for example use female to male jumper cables. For the sensor, I used a VCNL4000 proximity sensor. You can find a nice board with this sensor here. Finally, you will need one LED for visualization. I also used a 470 ohm resistor along with the LED.
To hear the audio file that will be played whenever a movement is detected, you will also need to connect the Raspberry Pi board to some external speakers.
For this project, you will need the Gordon’s wiringPi library to control the GPIO connector of the Raspberry Pi board, as well as the Python interface for this library. Just type this in the terminal:
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio
You will also need tools to control the I2C communication via Python. For this purpose, just type these commands:
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
Finally, you will also need a modified version of the Adafruit I2C Python repository for the Raspberry Pi. The original repository did not include code for the sensor we will use, so I put a modified version on my account. You can clone this git repository by typing:
Also, don’t forget to set the volume of your speakers via the alsamixer software. It is set to 0 by default, so check that if you can’t hear any audio.
The hardware configuration of this project is relatively simple: you need to connect the LED to the GPIO connector, as well as the proximity sensor to the I2C interface of the Raspberry Pi.
Let’s start by connecting the proximity sensor. As many I2C device, there are four pins you have to connect. First, connect the power supply: connect the Vin pin to the Raspbery Pi 5 V pin of the GPIO connector, and the GND pin to the GND pin of the GPIO connector. Then, connect the 2 I2C pins: the SCL pin of the sensor to the SCL0 pin of the GPIO connector, and in a similar way the SDA pin to the SDA0 pin.
For the LED, connect the GPIO connector number 18 to a resistor plugged into the breadboard, followed by the anode of the LED (the longest part). Finally, connect the cathode of the LED to the ground of the GPIO connector. At the end, it should be similar to the following picture:
The most important part of this project is the proximity sensor, so we will test it first. To see if it is correctly connected, you can type the following command:
sudo i2cdetect –y 0
If the sensor is correctly connected, you should see the corresponding address in the terminal output:
Once this is working, we can actually test the sensor and read some proximity data. For this, go into the folder your clone from the Github repository, in the folder called VCNL4000. This folder includes the library to easily access your sensor via I2C, so you don’t have to care about the low-level functionalities. Just type in the terminal:
sudo python VCNL4000_example.py
This will start a measurement loop, displaying the value of the sensor every 100 ms. Try to put your finger above the sensor, and you should see the value change:
Testing the LED is much easier. To test the LED connected to the GPIO pin number 18, we can run a simple Python script:
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(18,GPIO.OUT) GPIO.output(18,True)
This should put the LED on if it is correctly connected.
Now that we tested all the components separately, we can build our simple alarm. The main idea is to continuously read out the value from the proximity sensor, and activate a set of actions whenever the output of the sensor reaches a given value. In our case, when this threshold is crossed we will play a sound and put the LED on. This is done by the following piece of code:
# Get data from the sensor proximity = vcnl.read_proximity() # If the threshold is crossed, start the alarm if (proximity > 3000): GPIO.output(gpio_pin,True) os.system("aplay alarm_sound.wav") GPIO.output(gpio_pin,False) time.sleep(0.1)
This part of the code basically read the data from the sensor, via the object vcnl. Then, if the threshold is crossed, the LED is turned on, the sound “alarm_sound.wav” is played (don’t hesitate to use your own sounds!) and finally the LED is turned off again.
The code for the whole tutorial is in a file named pi_alarm.py, which is located in the same folder as the other files we used so far. You can start the alarm by typing:
sudo python pi_alarm.py
You can now play with it and put your finger just in front of the sensor, and if your speakers are correctly configured, the alarm should activate itself. Here is a summary of all the components you will need for this project:
This is the end of this tutorial, hope you enjoyed it. You can extend what you learned in this tutorial to build your own, customized alarm for your home !
I wrote a free step-by-step tutorial to show you how to build a simple thermostat with the Raspberry Pi. It will show you everything you need to build this simple project & also teach you how to use the Raspberry Pi at the same time. To get this free guide, simply click on the button below: