Home > Books > Raspberry Pi For Dummies > ScratchGPIO and the CamJam kit

Make a torch and burglar alarm in Scratch with the CamJam EduKit and ScratchGPIO

Sean McManus, co-author of Raspberry Pi For Dummies, shows how you can use Scratch on the Raspberry Pi to code flashing lights and a burglar alarm

When I visited the Raspberry Pi stand at BETT 2015, I had a first chance to try the CamJam EduKit, which provides a pack of components for your first Raspberry Pi electronics projects. The kit is affordable and provides everything you need to get started with connecting and lighting up LEDs, connecting buttons, buzzers and motion sensors.

To prepare for the Scratch hackathon at the Raspberry Pi 3rd Birthday party in February 2015, I had a play with it, in particular looking at how Scratch can be used with it. The extensive documentation for the kit (which is great, by the way) is in Python, so this article shows some Scratch examples you can use with the components in the kit. If you don't want to buy the kit, you can source the components separately elsewhere.

Installing ScratchGPIO

For instructions on installing ScratchGPIO, see Simon Walters' website here.

First steps

Like many projects, this one began with Lego. To stop the breadboard sliding all over the place, I created a Lego housing for it and the Raspberry Pi that would keep them conveniently together.

My Raspberry Pi is in a PiBow case, which has the pin numbers on it. That was extremely helpful when connecting things up.

Photo of my Raspberry Pi and breadboard, joined together with Lego

Photo of my Raspberry Pi and breadboard, joined together with Lego, also showing some of the CamJam EduKit components in place

Connecting up components

To connect up the components, I'll refer you to the CamJam Worksheets for detailed documentation. In particular, I recommend that you build the circuit from Worksheet 6 of Kit 1 (shown below), but I recommend reading the earlier sheets for some context along the way.

Circuit diagram showing LEDs, button and buzzer connected to Pi

Circuit diagram from the CamJam EduKit worksheet

Note that:

  • The resistors in the circuits with the the LEDs are 330 Ohm. If there are four colour bands, they will be Orange, Orange, Brown, and then Gold. If there are five bands, then the colours will be Orange, Orange, Black, Black, Brown.
  • The resistor in the circuit with the button is 4.7 kOhm
  • The long leg of the LED is the one shown with a kink in it in this diagram (the anode).
  • The buzzer's long leg (positive) is shown in red here.

ScratchGPIO example 1: Torch

This simple example turns the Red LED on when you press the button down and turns it off when you release the button.

The sensor value block is used to detect the button press. Broadcasts are used to turn pins on and off.

Note that the pins are numbered differently in ScratchGPIO to how they are numbered in the Python documentation. In Scratch, turn your Pi so the top is facing you and the GPIO pins are in the top right corner, and then count from left to right, top to bottom.

Scratch code

ScratchGPIO example 2: FizzBuzz

Do you know the game FizzBuzz, where you take it in turns to count. If a number is divisible by 3 you have to say 'fizz', and if it's divisible by 5, you have to say 'buzz'. Here's a version of that here, where Scratch counts and turns the red LED on if the number is divisible by 2, the amber light if it's divisible by 3, and the green light if it's divisible by 5.

Scratch code

Adding the motion sensor to your Raspberry Pi

If you have the CamJam EduKit 2 (sold separately to kit 1), you can also use the motion sensor in Scratch. Follow the instructions in worksheet 5 for EduKit 2, and add the motion sensor to your existing circuit:

Circuit diagram showing the Motion Sensor connected to the Pi

Circuit diagram from the CamJam EduKit worksheet

ScratchGPIO example 3: Make a Raspberry Pi burglar alarm

If you have everything connected up, including the motion sensor, here's some code for a burglar alarm, adapted from the code in the CamJam kit. The buzzer can be quite piercing, so I kept some code in the Scripts Area I could click to turn everything off at any time. The amber light flashes when the program is running, the green light is on when no movement is detected. The red light comes on and the buzzer sounds when motion is detected.

Scratch code

What next?

Hopefully, those simple demos will enable you to get going with the CamJam EduKit and ScratchGPIO. What can you build using the kit? Time to start exploring!

Photo of nerdy Lego man walking around my circuit board

Credits

© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Coding Compendium

Coding Compendium

A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.

Web Design in Easy Steps

Web Design IES

Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Newsletter | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About