Building a Motion Detector using Scratch GPIO server

Lead Image © kudryashka, 123RF.com

In Motion

As of the Raspbian Jessie release, Scratch provides easy access to the Raspberry Pi's GPIO pins. This project incorporates the new GPIO server to build a motion detector.

The release of Raspbian "Jessie" introduces a new version of Scratch (September 2015) that enables access to the Pi GPIO (general purpose input/output) pins by default. I'll demonstrate the new built-in features by creating a motion detector so you can sense when something or someone moves; it's perfect for keeping tabs on your bedroom or the cookie jar, or catching your dog eating off the counter when you're not home. In addition to an updated version of Scratch, you will need two LEDs, resistors, a passive infrared (PIR) sensor, and your breadboard to build a motion detector.

Accessing the Pi's GPIO pins from within Scratch has been possible for several years, but it previously required a separate installation of Scratch called ScratchGPIO, which I've used in previous Raspberry Pi articles, from Simon Walters (aka cymplecy). The Scratch GPIO server now enables access from the default installation of Scratch. In addition to the GPIO pins, you gain access to several other commands to get the time, IP address, and photos from the camera module.

The Scratch GPIO server – like the ScratchGPIO and Mesh features I've covered in previous Raspberry Pi Geek articles – rely on broadcast messages to communicate outside of the Scratch interface. This puts a special emphasis on using carefully constructed commands (syntax) to make Scratch do what you expect. In a Scratch programming environment, the focus on syntax creates a more advanced deviation from the drag-and-drop interface that makes Scratch so easy to understand.

Upgrading Scratch and Raspbian

Before you can build this project, you'll need to install or upgrade to Raspbian Jessie [1]. To complete the project, it's important that you are using the September 2015 version of Scratch that was introduced in Jessie.

Turning on the GPIO Server

When you create a new project, the GPIO server is not turned on, but it's the GPIO server feature that will provide the communication between Scratch, the Pi, and the pins. The easiest way to activate the server is to click on Scratch's Edit menu and chose Enable GPIO server.

However, you can also control the state of the server by sending the message gpioserveron in a broadcast() block, as seen in Figure 1. This ensures that the GPIO server is always enabled when the project runs, assuming the person running the project has the appropriate version of Scratch.

Figure 1: A script to turn an LED on and off using the Scratch GPIO server.

Scratch will remember the state of the GPIO server. If you enable it in a project, the next time you open the project, the GPIO server will be on. However, it's always a good idea to turn on the server as part of the project initialization if the functionality is required for the project. If you want to turn off the server, broadcast a message named gpioserveroff.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
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.