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.
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.
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.
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.
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
.
Pages: 4
Price $15.99
(incl. VAT)