Tested on:Raspbian Jessie; RPi 3
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Hardware:5V or 3.3V passive infrared sensor (PIR)
Led with a resistor fitting for 3.3V and 20mA
NPN-Transistor BC547 or similar
1k Ohm resistor
breadboard and jumper wires
self-oscillating Piezo buzzer

Intrusion detection with a Raspberry Pi and a smartphone over bluetooth

The Raspberry Pi enables building sophisticated alarm systems for a low price and with comparably low effort. The alarm system presented herein detects if a specific bluetooth device (e.g. smartphone) is present. If a PIR (passive infrared) motion sensor detects movement and the smartphone is not present, an piezo-buzzer is activated. This is easily extendable, for example by triggering sms alerts. Thanks to Rob Zwetsloot and the MagPi for the idea!
As is, this tutorial uses the bluetooth capabilities of the Raspberry Pi 3; other models would require an USB bluetooth dongle.

After installing Raspbian Jessie on the SD card and booting the RPi, the following steps have to be performed:

In the now open file, replace


Save, then reboot with

sudo reboot

Log in again and enter

in the terminal. Now, the bluetooth drivers and the demo script are installed. Shutdown the Raspberry Pi and assemble the circuit from the next section.

Breadboard schematic
Here is the breadboard schematic for the intrusion detection system. The PIR sensor used in this tutorial runs natively on 5V; with the 3.3V supplied in this schematic, the sensors range is smaller.

Mobile Bluetooth Alarm System

Smartphone Bluetooth alarm system – schematic

Running the script
Attach the circuit and boot the Raspberry Pi. Enable Bluetooth on your device and make the device visible. Then, start the script with

After the script prints the MAC address of your device next to its name like in the image below, kill the script with CTRL+C.

Mobile MAC address

Mobile MAC address

Open the script with

and enter your MAC address in the line

Save and exit, make your device visible and rerun the script. After a few seconds, the LED turns on and stays that way, as long as the smartphone is detected. Moving something in front of the PIR sensor shows no result. But, if you disable the mobiles bluetooth, the LED goes off after a few seconds and movements in front of the sensor yields a beep from the buzzer.

Code details
For the PIR sensor, event detection is applied with

This sends an interrupt if a raising edge (resulting from a movement in front of the sensor) is detected on pin 7 and calls the function action then. The global variable at_home is true if your mobile was detected; if it is false, an alarm is fired.
The bluetooth interface itself is very comfortable:

The function search returns a list of MAC addresses and the corresponding device names.

The full script
Click on Full Script to show all of the code.


Motion sensor tutorial: http://www.knight-of-pi.org/a-passive-infrared-sensorpir-lets-your-raspberry-pi-detect-motion/
Rob Zwetsloot on MagPi: https://www.raspberrypi.org/blog/author/robzwetsloot/
MagPi website: https://www.raspberrypi.org/magpi/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


  1. Hello, I am trying to find any information on a project that would include bluetooth detection.. I am trying to find, specifically a device that will alert by LED if a bluetooth device is detected in the area for a given amount of time.. Thanks

  2. hi there i was looking for something similar to this project. i am totally new to Bluetooth programming. what i want to know if there are more then 1 phones i need to check. i mean let say there are 4 family members and if any of them are at home then i dont want the sensor to go active when i am not at home. how can i add 4 more mac address of phone to system and check if any of them is available if none of the phones available then turn on the sensor. please advice me

    • You have the list of found devices called devices. You can iterate over that list and perform special actions for all kinds of device constellations.


Time limit is exhausted. Please reload CAPTCHA.