Introduction: Cycle an RGB LED Through the Colour Spectrum Using a Raspberry Pi 2 and Scratch

About: I am an ICT and Mechatronics teacher. Feel free to link to me in Linkedin, a fella can never have too many connections :)

Update Notes Thur 25th Feb, 2016: I have improved the Scratch program and redesigned my instructable.

Hi guys, with this project I wanted to use Scratch to cycle an RGB LED through the colour spectrum.

There are a load of projects doing this with the Arduino, so I was curious to see if I could get a decent result with the Raspberry Pi.

My first attempt at this instructable wasn't very good, so I have done a bit more research and think I have something that works better. When I was looking over some Arduino projects to try to understand where I went wrong in my original program, I stumbled upon an absolutely excellent Arduino script, which I will link you to at the end. My friend Andrew and I spent the afternoon converting it to Scratch. We have done our best with it and I hope you will give it a try.

This project is a follow-on from my instructable about altering the brightness of an LED using buttons and Scratch which you can find here:

https://www.instructables.com/id/PWM-Based-LED-Cont...

Link to the original Arduino Sketch I based my Scratch program on:

https://www.arduino.cc/en/Tutorial/DimmingLEDs author Clay Shirky

Step 1: Collecting Together the Things You Will Need for This Project

Components you will need:

A Raspberry Pi with a Raspian operating system and internet connection

1 x Breadboard

1 x RGB LED (common cathode)

3 x 330 ohm resistors ( orange orange brown)

4 x male/female breadboard cables

1 x male/male breadboard cable (or a small single core jumper cable if you have one)

Step 2: Understanding What the Legs on the RGB LED Do

Take your RGB LED and look at the legs, you will notice that one leg is longer than all the others. Orientate the LED so that this longest leg is to the left.

Pin 1 is used to make the LED shine RED

Pin 2 is the ground pin

Pin 3 makes the LED shine GREEN

Pin 4 makes the LED shine BLUE

The RGB LED I am using has a common cathode, which basically means you connect its ground leg to a Raspberry Pi ground pin to make it work.

Step 3: Inserting the 330 Ohm Resistors and the Ground Jumper Cable Into the Breadboard

To keep things easy to see on the diagram we can place the resistors and the ground cable where they need to be first. Resistors don't have polarity so it doesn't matter what way round they go.

Note: Why do we need three resistors for one LED?

Think of an RGB LED as 3 different LEDs bundled into one. If we had 3 individual LEDs in a circuit we would use a resistor for each one, and so we need a resistor for each colour leg of the RGB LED.

Step 4: Adding the LED to Our Circuit

Now we have the resistors and ground cable in place, we can install our LED into the breadboard circuit. Orientate the LED so the longest leg is to the left.

Gently part the legs a little bit to allow them to plug into the breadboard, making sure each leg is on the same line as a corresponding resistor.

The longest leg (leg 2) should line up with the black ground cable.

Step 5: Attaching the Cables to the Breadboard Part 1: Connecting Ground

First let's connect the ground from the Raspberry Pi to the ground leg on the LED.

In my diagram I have connected the male/female cable from pin 6 on the Raspberry GPIO to the ground rail of the breadboard to connect the LED's ground leg to the Raspberry Pi.

The reference card shows you the pin layout for the Raspberry Pi GPIO. The 40 pin GPIO to the right of the picture is for the Raspberry Pi 2, which I am using to do this project.

Step 6: Attaching the Cables to the Breadboard Part 2: Connecting the Red LED Leg

Push the male end of the cable into the hole just above the resistor on the left, and push the female end of the cable onto GPIO17 (pin11) on the Raspberry Pi.

The reference card for the GPIO pins will help guide you to the correct pin.

Step 7: Attaching the Cables to the Breadboard Part 3: Connecting the Green LED Leg

Push the male end of the cable into the hole just above the resistor in the middle, and push the female end of the cable onto GPIO18 (pin12) on the Raspberry Pi.

The reference card for the GPIO pins will help guide you to the correct pin.

Step 8: Attaching the Cables to the Breadboard Part 4: Connecting the Blue LED Leg

Push the male end of the cable into the hole just above the resistor on the right, and push the female end of the cable onto GPIO27 (pin13) on the Raspberry Pi.

The reference card for the GPIO pins will help guide you to the correct pin.

Step 9: Programming in Scratch: Circuit Check

When I first cabled up this project I was a bit careless and mixed up my colour cables, which meant when I wanted red to come on, green came on instead, so I wrote a simple program to verify that everything was wired up correctly.

The LED test is controlled by 3 pairs of keys.

A and Z control RED, A switches red on, Z switches red off

S and X control GREEN, S switches green on, X switches green off

D and C control BLUE, D switches blue on, C switches blue off

Setting a pin to high makes the LED light up, setting it to low switches the LED off.

Download the program and test your circuit if you want to be sure it's wired up properly.

Step 10: Programming in Scratch: What I Wanted to Do With the RGB LED

Programming in Scratch is a nice experience. It has a click and drag interface and is pretty intuitive. Although it was primarily created to introduce children to programming I actually think it is a pretty useful programming environment as I think is shown in the code which controls the LED in my project.

So here is what I wanted to happen:

The colour changes would be done in three phases:


In the first phase we start off with red at maximum and green and blue set to a very small level.

We then started to reduce red brightness by -1, while increasing green's brightness by 1.

We used a loop counter to limit how many times this happened.

Once the loop counter reached 255 we started the second phase.

In the second phase green will be at maximum, red and blue set to a low level.

We lower green brightness by -1 while increasing blues brightness by 1.

Our loop counter for the second phase was set to 509.

Once it reached 509 we would start phase 3.

In phase three, blue is at maximum brightness and green and red are at low levels.

We start to lower blue brightness by -1 while increasing red brightness by 1.

Once the loop counter reached 763, the cycle would start over at phase 1.

We have three variables redVal, greenVal and blueVal to hold the values of each colour's brightness level and these values are then sent out to the correct GPIO pins to power to the legs of the LEDs to set each colour's brightness value, which in turns gives us the colour mix we want.

And that's my attempt to cycle through the colour spectrum using an RGB LED and Scratch.

If you have an Arduino and you run the sketch I linked that inspired me to write the Scratch version, you will see there is no colour flickering at all. I am not entirely sure why the Scratch version flickers so much. I suspect that the Arduino is better at handling PWM, but if you see something in my code that needs improved, I would really be grateful if you took the time to tell me.

Thank you for reading my instructable and I hope you have a great day!

Step 11: Screen Capture of the Scratch Program

If you want to have a go at programming it yourself here is a close look at the layout.