Building a Motion Detector using Scratch GPIO server

Detecting Motion

The PIR sensor reports a value of   (no motion is detected) or 1 (motion detected). The script checks for movement by evaluating the sensor value of GPIO21, and when it's equal to 1, it turns on the red LED and records an entry in the motion log list (Figure 5).

Figure 5: Script to monitor the PIR sensor value and detect movement.

If the sensor value is not 1, then turn on the green LED to signify there is no motion. You'll also notice that when the green LED is lit – that is, broadcast(gpio14on) – the script turns off the red LED – broadcast(gpio15off) – and vice versa. This ensures that both LEDs are never lit simultaneously.

GPIO input pins will display as variables in the ()sensor value block located in the Sensing palette. Before the pin will show as a sensor value, you first need to run the broadcast block that configures the pin as an input. For example, create the broadcast(config21in) block and then run it. After you run the block (e.g., by double-clicking the block), the pin will show in the list of sensor values.

Before I move on to the new non-GPIO commands you see in the script, I want to draw your attention to the wait until() block that runs when the sensor detects motion. This block provides some additional control by waiting until there is no more movement (the GPIO21 value is not 1). That keeps the script from continually evaluating to true and writing consecutive entries in the motion log for the same motion event.

Non-GPIO Commands in Scratch Server

The Scratch GPIO server introduces three new commands that interact with other parts of the Pi but are not connected to the GPIO. Those new broadcast commands are getip, gettime, and photo. In the script, I use photo and gettime; getip returns the IP address of the Pi. The photo command integrates with the Raspberry Pi camera. When you use the photo command, the camera takes a picture and adds the image as a costume to the sprite.

The gettime command creates the following sensor variables: fulldatetime, hours, and minutes. The script in Figure 4 initializes the motion log list with a beginning timestamp using the fulldatetime value. Then, each time the script (Figure 5) detects motion, it records the event by adding the hours, minutes, and seconds to the motion log. Whereas hours and minutes variables are default sensor values, seconds is a variable created by me from the fulldatetime value in the set() line.

When you examine the fulldatetime value, you will note a 12-digit number in the format year+month+day+hour+minute+second, which can also be represented as YYMMDDHHMMSS. As you see in the script, you can extract the 11th and 12th values of fulldatetime and join them together to create a standalone seconds value.

If you are fortunate enough to have a camera board attached to your Pi, then this script will correlate the entry in the motion log to the costume number. For example, the first detected motion will be in list position two and the first photo would be costume two (assuming you start with a one-costume sprite).

The broadcast(gettime) block retrieves the date information and makes it available via the sensor values, but the time values are not continuously updated after you send the gettime broadcast one time. Each time you want to use the date and time, you need to run broadcast(gettime) to ensure you get the current values.

If your time values are not correct, you can set your time zone by using the raspi-config utility. Run it from a terminal window with the command sudo raspi-config. The Raspberry Pi defaults to the UTC (Coordinated Universal Time).

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Stop-motion animation

    Stop-motion animation doesn't require expensive cameras and movie editing software. With Scratch and a Pi camera, you can become your own director, producer, and publisher.

  • Creating a multiplayer quick-reaction game

    The quick-reaction game provides an introduction to building simple circuits with the Raspberry Pi and controlling those circuits with ScratchGPIO, an advanced version of Scratch.

  • Diving Deep

    The past year has seen a continued emphasis on STEM (science, technology, engineering, and mathematics) education in English-speaking countries.

  • PHP on Raspberry Pi

    Getting started with PHP on Raspberry Pi is easy. We show how to build a simple PHP app to control an LED.

  • Building a video-enabled nest box

    We show you how to build a completely wired nest box that charts the movements of nesting birds and delivers photo images of hatchlings.