Raspberry Pi

Proximity alarm with the Raspberry Pi

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 !

What You'll Need

ComponentQty
Raspberry Pi board1
Small breadboard and jumper wires1
VCNL4000 proximity sensor1
Red LED1
470 ohms resistor1
External speakers1
Debian Linux OS SD card1

Step by Step

1

Software Installation and Setup

<h3 style="text-align: justify;">Software requirements</h3><p style="text-align: justify;">For this project, you will need <a href="https://projects.drogon.net/raspberry-pi/wiringpi/">the Gordon's wiringPi library</a> 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:</p><p style="text-align: justify;">sudo apt-get install python-dev<br>sudo apt-get install python-rpi.gpio</p><p style="text-align: justify;">You will also need tools to control the I2C communication via Python. For this purpose, just type these commands:</p><p style="text-align: justify;">sudo apt-get install python-smbus<br>sudo apt-get install i2c-tools</p><p style="text-align: justify;">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:</p><p style="text-align: justify;">git clone <a href="https://github.com/openhomeautomation/raspberry-pi-alarm">https://github.com/openhomeautomation/raspberry-pi-alarm</a></p><p style="text-align: justify;">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.</p>

2

Hardware Configuration - Proximity Sensor

<h3 style="text-align: justify;">Hardware configuration</h3><p style="text-align: justify;">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.</p><p style="text-align: justify;">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.</p>

Hardware Configuration - LED Setup
3

Hardware Configuration - LED Setup

<p style="text-align: justify;">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:</p><p style="text-align: justify;"><img src="https://s3-us-west-2.amazonaws.com/purepress/files/gwPGi8yz7o3TAz5yt-original.png"><br></p>

Testing the Proximity Sensor
4

Testing the Proximity Sensor

<h3 style="text-align: justify;">Testing the individual parts</h3><p style="text-align: justify;">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:</p><p style="text-align: justify;">sudo i2cdetect –y 0</p><p style="text-align: justify;">If the sensor is correctly connected, you should see the corresponding address in the terminal output:</p><p style="text-align: justify;"><img src="https://s3-us-west-2.amazonaws.com/purepress/files/ee8YHntDcB3irjbRb-original.png"><br></p><p style="text-align: justify;">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:</p><p style="text-align: justify;">sudo python VCNL4000_example.py</p><p style="text-align: justify;">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:</p><p style="text-align: justify;"><img src="https://s3-us-west-2.amazonaws.com/purepress/files/5jeyx4CZ3XikNar4K-original.png"><br></p>

5

Testing the LED

<p style="text-align: justify;">Testing the LED is much easier. To test the LED connected to the GPIO pin number 18, we can run a simple Python script:</p><p style="text-align: justify;">This should put the LED on if it is correctly connected.</p>

6

Assembling the Complete Alarm System

<h3 style="text-align: justify;">Putting it together</h3><p style="text-align: justify;">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:</p><p style="text-align: justify;">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.</p><p style="text-align: justify;">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:</p><p style="text-align: justify;">sudo python pi_alarm.py</p><p style="text-align: justify;">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.</p>

Wrapping Up

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 !