RGB-LED Lesson 3 (Scratch GPIO) – Some Simple Steps

Wiring up the Hardware

Note: This lesson assumes you have already setup the Scratch GPIO software correctly (otherwise see the previous Lesson 2 – (Scratch GPIO) – Getting Started for details).

BEFORE you switch on your Raspberry Pi, you should wire up your kit to the GPIO ports, and then double check before applying power!

RGB-LED Kit Pin Setup for Scratch GPIO

RGB-LED Kit Pin Setup for Scratch GPIO

Wiring in this way, the pins are mapped as follows:

Pin 21 18 16 15 13 12 11
Function Red (Enable=1) Green (Enable=1) Blue (Enable=1) LED1 (Enable=0) LED2 (Enable=0) LED3 (Enable=0) LED4 (Enable=0)

Note: Using the current Scratch-GPIO software, only 7 pins are available as outputs, so LED5 is left disabled.

Start Scratch

Start up your desktop environment (type “startx” from the command line) if it does not boot automatically into it.

Run “Scratch GPIO” by clicking on the new icon.

When you start Scratch you will have a clear project.

Scratch Screen Layout

Scratch Screen Layout

Note: If you’ve run Scratch using the correct “Scratch GPIO” icon, you will get a message saying “Remote sensor connections enabled” which indicates the GPIO support parts have been started.

Quick Hardware Test

Before we build our script or doing anything else, we should quickly test that everything is working as it should be.

Scratch gets a friend to help with GPIO hardware.

Scratch gets a friend to help with GPIO hardware.

As discussed before, by using the gpio_handler.py, Scratch can shout out commands and they will be carried out by the handler.  Select the “Control” blocks from the top left area and find the “broadcast” block.  Move the block across (it doesn’t need to be attached to anything yet) to the script building area and add a new… message “pinpattern1000000”.

Now, you can quickly test this by clicking on it, and all being well 4 LEDs should turn RED!

Broadcast "pinpattern1000000"

Broadcast “pinpattern1000000” making RED shine

Setting the Stage

If we want Scratch to interact with his virtual environment, then we need something in it (at the moment he is standing in a very minimal looking white space).  To put something in the background, we click on the small white rectangle to edit the “Stage” where the action will take place.

You will notice, the middle section of the screen now changes to allow editing of the “Stage” item, instead of Scratch.  Select the “Backgrounds” tab, and “edit” our background by pressing the edit button.  Use the various paint tools to draw some blocks or shapes, using Red, Green and Blue (the shades won’t matter).

Example Colourful Stage

Example Colourful Stage

When you are done, you will see the new design appear behind “Scratch” in the Display Area.

Now click on Scratch (named “Sprite1” – you can change the name in the top middle part if you wish), and reselect the Scripts tab.  You can start building the script now.

Building the Script

Completed Script

Completed Script

You can build up the parts of the above script by picking up the various elements from the colour coded sections in the top corner.  Each of the block types are colour coded, so you can quickly see that most of the elements in this script are from the Yellow “Control” group, with one from the darker Blue “Motion” group and the remaining items from the lighter Blue “Sensing” group.

Scratch Block Types

Scratch Block Types

Step 1: Moving

Get the starting Flag element from the “Control” section “when [FLAG] clicked” item, and add to that the “forever” loop.  Now we can drop in the “go to” block from the “Motion” section and select “mouse pointer” as the option.

Running this, by pressing the Green Flag on the top right, you find that Scratch will follow your mouse cursor.

Scratch will now follow your mouse!

Scratch will now follow your mouse!

Step 2: Sensing

Now to get Scratch to respond to the background, we can get him to sense the colour he is over.   From the “Sensing” section, pick up the “touching color” block and move it to the script area.

You can test this by clicking on the item and it will respond with “true” or “false” depending on if the colour Scratch is on matches the colour in the little box in the block (if you click on the box, you can grab a particular colour from your background and move Scratch on top until you get “true” next time you click).

Now, we want Scratch to do something “if” he is touching one of the colours.  So from the “Control” section, we can pick up the “if…else” block, and then we can drop the “touching color” block into it.

Scratch will now be able to respond when he is touching the Red blocks of colour.

Scratch will now be able to respond when he is touching the Red blocks of colour.

However, if we run this, there is nothing for Scratch to do when he is touching the Red colour, so we need an action.

Step 3: Doing

You can now attach the broadcast command we had before to the script (or create a new one).  The “broadcast” block is in the “control” section and we want to send “pinpattern1000000” as the message.

We add that as our action, so that when Scratch is touching Red, the LEDs turn Red!

Make the LEDs light up RED when we cross red.

Make the LEDs light up RED when we crossover red.

Step 4: Finishing off

We can now do the same for the remaining colours by placing more “if…else” blocks within the “else” part (there could be times when Scratch is touching both Red and Green, so this ensures Scratch only picks one colour at a time).

We take a specific action if we touch Red, Green, Blue or something else.

We take a specific action if we touch Red, Green, Blue or something else.

You can use broadcast messages “pinpattern0100000” and “pinpattern0010000” for Green and Blue (again you can test them by clicking on them before attaching to the script).

Finally, for the last “else” we can add we can make the LEDs turn white to match with “pinpattern1110000” (so the LEDs are “white” when we are not touching any of the other colours).

Completed Script

Completed Script

Tip: Scratch is quite big on our screen so we can make him a little smaller by right clicking on him and selecting “resize this sprite”.

Well done! Scratch should now make the LEDs change colour as he is moved over them, his world is now part of yours!

Extra Credit:

You can now add some more colours into your background and use a combination of Red, Green and Blue to allow Scratch to jump over all of the colours below.

RGB-LED Colours Table

RGB-LED Colours Table

Remember the pin pattern message is structured as follows:

pinpatternRGB1234

Pin 21 18 16 15 13 12 11
Function Red (Enable=1) Green (Enable=1) Blue (Enable=1) LED1 (Enable=0) LED2 (Enable=0) LED3 (Enable=0) LED4 (Enable=0)

Where RGB (active when 1):

R = Red G = Green B = Blue

And 1234 (active when 0):

These controls each of the LEDs 1 to 4 (if you set any of them to 1 then that one won’t light up).

Have a go and see how you get on.

Comments
  1. […] RGB-LED Lesson 3 (Scratch GPIO) – Some Simple Steps […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s