Detect Motion Using Passive Infra-Red Sensor Interfaced with Raspberry Pi
This example shows how to use Raspberry Pi® hardware to interface to a motion sensor and control an external LED.
Introduction
In this example you will learn how to create a Simulink® model that reads the output of a motion sensor and performs a series of actions based on the detected motion activity. For this example, we will be using a Passive Infra-Red (PIR) motion sensor from Parallax® Inc. The PIR Sensor is a pyroelectric device that measures changes in infrared heat levels emitted by surrounding objects. When an object, such as a person, passes in front of the PIR sensor, the sensor outputs a logic HIGH on its output pin. If no motion is detected, the sensor sets the output pin to logic LOW.
Prerequisites
We recommend completing Get Started with Simulink Support Package for Raspberry Pi Hardware example.
Required Hardware
To run this example you need the following hardware:
Raspberry Pi board
PIR motion sensor from Parallax
1.8mm Red LED
270 Ohm resistor
Breadboard wires (recommended)
Small breadboard (recommended)
Speakers or headphones (Task 3)
A USB web camera (Task 4)
Task 1 - Connect Motion Sensor and LED to Raspberry Pi Hardware
In this task, you will connect the PIR motion sensor and a red LED to the Raspberry Pi hardware. The sensor has three pins: VCC, GND, and OUT. The VCC pin will be connected to +3.3 Volt voltage rail and the GND pin is connected to the ground. The OUT pin is the logic signal indicating motion. This pin will be connected to a GPIO pin on the Raspberry Pi hardware as shown in the following schematic:
In the circuit schematic above, an LED has been connected to a GPIO pin. This LED will turn on whenever motion is detected. Note that the forward voltage of the LED used must be smaller than 3.3 Volts. We recommend using a 1.8mm red LED.
1. Interface the PIR motion sensor to the Raspberry Pi hardware as shown in the circuit diagram above.
2. Connect a red LED to the indicated GPIO pin of the Raspberry Pi hardware as shown in the circuit diagram above.
NOTE If you do not have the PIR motion sensor available, you can substitute a simple push button, as shown in the simplified circuit diagram below.
Task 2 - Turn ON LED When Motion Is Detected
In this task you will turn ON the LED connected to the board when motion is detected by the PIR sensor.
1. Open the raspberrypi_motion_sensor_led
Simulink model.
Task 3 - Say "Motion Detected" When Sensor Output Is High
The firmware available for Raspberry Pi hardware includes eSpeak text to speech synthesizer. In this task you will use the eSpeak Linux® package to output "Motion Detected" when sensor OUT pin goes high.
1. If you are using the analog audio output of the Raspberry Pi board, connect a pair of headphones or speakers to the analog audio output jack. If you connected your Raspberry Pi to an HDMI monitor or TV, you will hear the audio from the speakers of your display device.
2. Open the raspberrypi_motion_sensor_texttospeech
Simulink model.
3. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune to run the model on the Raspberry Pi hardware.
4. When the model starts running you will hear your Raspberry Pi hardware speak "Motion Detected" whenever the PIR sensor detects motion.
5. Click Stop to stop the model.
Task 4 - Build a Motion Sensor Camera
In this task you will create a Simulink model that captures a picture and saves it on board when motion is detected.
1. Connect a compatible USB camera to the Raspberry Pi hardware.
2. Open the raspberrypi_motion_sensor_camera
Simulink model.
3. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune to run the model on the Raspberry Pi hardware.
4. A grayscale image will be captured and saved on the Raspberry Pi hardware every time the sensor motion pin goes from low to high. The captured frame is displayed on the host computer while the model is running in external mode. The captured image data is saved to a file in the user's home directory on the board. To retrieve the first captured image, execute the following on the MATLAB® command line:
r = raspberrypi; getFile(r,'/img0.dat') img = load('img0.dat'); imagesc(reshape(img,120,160)), colormap(gray)
5. Click Stop to stop the model.
6. You can deploy a stand-alone version of this Simulink model. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start. When you run the model in this way, the Simulink model running on the Raspberry Pi hardware will record images to the SD card. You should stop the model after it runs for some period of time and retrieve the recorder images later as shown in Step 4 above. To stop the model running detached from Simulink, execute the following on the MATLAB command line:
r = raspberrypi; stopModel(r,'raspberrypi_motion_sensor_camera')
Other Things to Try
Experiment with other blocks in the Raspberry Pi and Simulink block library. For example:
Use LED block to turn on an on-board LED when motion is detected.
Use Counter Limited block from Simulink library instead of Counter Free-Running to limit the number of image files to 255, for example.