Simulink

Motion Sensor

This example shows how to use BeagleBoard™ hardware to interface to a motion sensor and control an external LED.

Introduction

BeagleBoard target enables you to create and run Simulink® models on BeagleBoard embedded computers. The target includes a library of Simulink blocks for configuring and accessing BeagleBoard peripherals.

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 sends a high signal to its output pin.

Prerequisites

Required Hardware

To run this example you will need the following hardware:

  • BeagleBoard

  • PIR motion sensor from Parallax

  • 1.8mm Red LED

  • 1 kOhm, 470 Ohm, and 100 Ohm resistors

  • Breadboard wires

  • Small breadboard (recommended)

  • Speakers or headphones (Task 3)

  • A USB web camera (Task 4)

Task 1 - Connect the Motion Sensor and the LED to BeagleBoard Hardware

In this task, you will connect the PIR motion sensor and a red LED to the BeagleBoard 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. The motion sensor will be connected to a GPIO pin on the BeagleBoard hardware. Since the BeagleBoard GPIO pins are only compatible with +1.8 Volt logic levels, a simple voltage divider circuit is needed to interface the OUT pin to the GPIO pin on the BeagleBoard hardware as shown in the following schematics:

In the preceding circuit schematics, 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 1.8 Volts. We recommend using a 1.8mm red LED.

1. Interface the PIR motion sensor to the BeagleBoard hardware as shown in the circuit diagram above.

2. Connect a red LED to the indicated GPIO pin of the BeagleBoard 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 Motion Sensor (Turn on LED)Motion Sensor (Turn on LED) model.

2. Notice the GPIO Read and the GPIO Write blocks. The GPIO Read block reads the output of the motion sensor every 0.1 second. The value read from the motion sensor is written back to the GPIO pin supplying power to the LED.

3. In your Simulink model, make sure that the Simulation mode on the toolbar is set to External. Then, click the Run button on the toolbar to run the model on the BeagleBoard hardware.

4. When the model starts running on the BeagleBoard hardware, double click on the Motion Sensor Output block. This block is a Simulink scope that shows output of the motion sensor.

5. Click Stop button to stop the model.

Task 3 - Say "Motion Detected" when Sensor Output is High

The firmware available for BeagleBoard 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. Connect a pair of headphones or speakers to the AUDIO OUT connector of the BeagleBoard hardware.

2. Open the Motion Sensor (Text to Speech)Motion Sensor (Text to Speech) model.

3. In your Simulink model, make sure that the Simulation mode on the toolbar is set to External. Then, click the Run button on the toolbar to run the model on the BeagleBoard hardware.

4. When the model starts running you will hear your BeagleBoard hardware speak "Motion Detected" whenever the PIR sensor detects motion.

5. Click Stop button 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 USB web camera to one of the USB ports on your board.

2. Open the Motion Sensor CameraMotion Sensor Camera model.

3. In your Simulink model, make sure that the Simulation mode on the toolbar is set to External. Then, click the Run button on the toolbar to run the model on the BeagleBoard hardware.

4. A gray scale image will be captured and saved on the BeagleBoard 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:

h = beagle
connect(h)
getFile(h,['/home', '/', h.UserName, '/img0.dat'])
img = load('img0.dat');
imagesc(reshape(img, 120, 160)), colormap(gray)

5. Click Stop button to stop the model.

6. You can deploy a stand-alone version of this Simulink model by clicking the Deploy To Hardware button on the model toolbar. When you run the model in this way, the Simulink model running on the BeagleBoard hardware will record images to the microSD 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:

h = beagle
h.stop('beagleboard_motion_sensor_camera')

Other Things to Try

Experiment with other blocks in the BeagleBoard 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.

Summary

This example showed you how to use the BeagleBoard target to connect an external motion sensor and an LED to the board. In this example you learned that:

  • GPIO Read block lets you read digital logic levels output by an external device.

  • GPIO Write block lets you output logic high or low values to control external devices.

  • LED block lets you to turn an on-board LED on or off.

  • eSpeak Text to Speech block lets you synthesize speech.