# Raspberry Pi - Simple 2x2x2 Led Cube

20,632

42

26

## Introduction: Raspberry Pi - Simple 2x2x2 Led Cube

I built this pretty simple 2x2x2 led cube for my raspberry pi, and wanted to share what i learnt.
This is my first time using transistors so the circuit is probably not perfect, but it works as i intended it to.

Here is what you will need to make one:
- 1x raspberry pi
- 8x led's that can run on 3.3v (mine were rated for 5v)
- 2x NPN transistors (i used two 2n3904, but many others will work too)
- 2x 560ohm resistors (green blue brown gold, this will probably work with more more resistive resistors too)
- 1x 61ohm resistor (blue brown black gold, this is so we don't burn out the led's, doesn't hurt if this is even bigger)
- a raspberry pi breakout board & ribbon-cable, or a hacked ribbon cable from an old cd/floppy drive (i recommend getting a cheap breakout board and cable from ebay. It's alot easier to wire up)

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Making the Led Cube

You can make the cube freehand. But if you want to make it straighter and nicer looking, you should make a jig out of some wood.

The idea behind the led cube is that the grounds are connected together in each layer/level of the cube.
This lets you control what layer gets lit up by grounding it by switching on the NPN transistors.
The positive leads of the led's controls which column to light up.

To make the cube just:
- bend the short(negative) leads of all the led's 90 degrees to the side
- solder the first four negative(short) leads of 4 led's together so they make a square
- solder the last four negative(short) leads of 4 leds' together so they make another square
- solder the positive leds of the second square to the positive leads of the first square
- solder a wire to the ground of each layer
- your 2x2x2 led cube is now complete!

## Step 2: Wiring It Up

And if you know better, you should also calculate the drain of the circuit on full load and make sure it does not exceed 50mA!!!(supposed limit of the raspberry pi's GPIO pins)

The NPN-transistor has three pins (or legs if thats what you're into).
These pins are called emitter, base and collector.
When looking at the flat side of the npn transistor, with the pins facing down,
the left pin is the emitter, the middle pin is the base and the right pin is the collector.
I will use the base pin to switch the ground of the two layers in the led-cube on/off!

Now that your cube is done, you will need to wire it up, so:
- stick the four positive leads of the led cube into the breadboard
- connect gpios from the raspi to each of the 4 positive leads of the led cube
- connect the current limiting resistor (61ohm) to the ground on the pi and to the rail
- connect the right pin (collector) on both npn transistors to the grounded rail
- connect a 560ohm resistor to the middle pin(base) of each npn transistor
- then connect a gpio from the raspi to each of the 560ohm resistors
- connect the emitter(the left pin) on each npn transistor to one of the negative leads of the led cube
- and you're done!

## Step 3: Write the Code

To make everything as simple as possible, i will script in bash.
In the next step you can see my code, but first.
here is the basics.

Say you have a lead from the cube connected to GPIO8 and one of the NPN-transistors connected to GPIO-25.
- open a terminal on your pi
- create the script with: "touch ledcube.sh"
- edit the script with: "nano ledcube.sh"
- write this into the script:

#!/bin/bash
#preparing gpio 8 and 25 for use
echo 8 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio8/direction;
echo 25 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio25/direction;
#turning on an led
echo 1 > /sys/class/gpio/gpio8/value;
#turning on the ground
echo 1 > /sys/class/gpio/gpio25/value;
#waiting a little
sleep 0.05s;
#turning off an led
echo 0 > /sys/class/gpio/gpio8/value;
#turning off ground
echo 0 > /sys/class/gpio/gpio25/value;
#closing gpio
echo 8 > /sys/class/gpio/unexport;
echo 25 > /sys/class/gpio/unexport;

- run the script with "sudo sh ledcube.sh"
- modify it so you can control all your led's and layers and ENJOY!

You can also build a 3x3x3 led cube this way, just expand the cube and add another npn-transistor. Any more than that and you will need some other parts.

Any and all feedback is appreciated. And please tell me if I'm spreading misinformation, so i can correct it.

## Step 4: Moar Code

Some of my sloppy code to get you going ;D
Save this code to ledcube.sh and change the variables GPIONR# and LAYER# in script so they match the pins you have connected the led cube to on your pi.

#!/bin/bash
# This scriptwill flash a 2x2x2 led cube with gpio, using two 2n3904 npn
#VARIABLES:
#GPIO numbers for LED's
GPIONR1=7;
GPIONR2=8;
GPIONR3=24;
GPIONR4=25;
#GPIO FOR transistors to control the layers
LAYER1=23;
LAYER2=18;
#number of times to cycle
FLASHTIMES=10;
#how long to pause between flashes
SLEEP1=0.1s;
SLEEP2=0s;
SLEEPSHORT1=0.00005s;
SLEEPSHORT2=0.0010s;
SLEEPSHORT3=0.0025s;
SLEEPSHORT4=0.0050s;
SLEEPSHORT5=0.0100s;
#layer to light up
LAYER=\$LAYER1;

#Initializing GPIO
#LEDS:
echo \$GPIONR1 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio\$GPIONR1/direction;
echo \$GPIONR2 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio\$GPIONR2/direction;
echo \$GPIONR3 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio\$GPIONR3/direction;
echo \$GPIONR4 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio\$GPIONR4/direction;
#LAYERS(NPN-TRANSISTORS)
echo \$LAYER1 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio\$LAYER1/direction;
echo \$LAYER2 > /sys/class/gpio/export;
echo out > /sys/class/gpio/gpio\$LAYER2/direction;

#FUNCTIONS:
alloff() {
#turns all leds off
echo 0 > /sys/class/gpio/gpio\$GPIONR1/value
echo 0 > /sys/class/gpio/gpio\$GPIONR2/value
echo 0 > /sys/class/gpio/gpio\$GPIONR3/value
echo 0 > /sys/class/gpio/gpio\$GPIONR4/value
}
layersoff() {
#turns all layers off
echo 0 > /sys/class/gpio/gpio\$LAYER1/value
echo 0 > /sys/class/gpio/gpio\$LAYER2/value
}
switchlayer() {
#switches layer
if [ "\$LAYER" -eq "\$LAYER1" ]
then
LAYER=\$LAYER2
echo 1 > /sys/class/gpio/gpio\$LAYER2/value
echo 0 > /sys/class/gpio/gpio\$LAYER1/value
else
LAYER=\$LAYER1
echo 1 > /sys/class/gpio/gpio\$LAYER1/value
echo 0 > /sys/class/gpio/gpio\$LAYER2/value
fi
}
#LED CUBE FLASHSEQUENCES
ledspin() {
echo 1 > /sys/class/gpio/gpio\$GPIONR1/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
echo 1 > /sys/class/gpio/gpio\$GPIONR2/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
echo 1 > /sys/class/gpio/gpio\$GPIONR3/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
echo 1 > /sys/class/gpio/gpio\$GPIONR4/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
}
ledbackspin() {
echo 1 > /sys/class/gpio/gpio\$GPIONR4/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
echo 1 > /sys/class/gpio/gpio\$GPIONR3/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
echo 1 > /sys/class/gpio/gpio\$GPIONR2/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
echo 1 > /sys/class/gpio/gpio\$GPIONR1/value
sleep \$SLEEP1
alloff
sleep \$SLEEP2
}
ledflashall() {
for i in `seq 1 4`;
do
echo 1 > /sys/class/gpio/gpio\$GPIONR1/value
echo 1 > /sys/class/gpio/gpio\$GPIONR2/value
echo 1 > /sys/class/gpio/gpio\$GPIONR3/value
echo 1 > /sys/class/gpio/gpio\$GPIONR4/value
echo 1 > /sys/class/gpio/gpio\$LAYER1/value
echo 1 > /sys/class/gpio/gpio\$LAYER2/value
sleep \$SLEEP1
alloff
sleep \$SLEEP1
switchlayer
switchlayer
done
}
ledawesomeflash() {
AWESOMEFLASHTIMES=10;
for i in `seq 1 \$AWESOMEFLASHTIMES`;
do
echo 1 > /sys/class/gpio/gpio\$LAYER1/value
echo 1 > /sys/class/gpio/gpio\$GPIONR1/value
sleep \$SLEEPSHORT1
echo 1 > /sys/class/gpio/gpio\$LAYER1/value
echo 1 > /sys/class/gpio/gpio\$GPIONR1/value
sleep \$SLEEPSHORT1
alloff
layersoff
done
for i in `seq 1 \$AWESOMEFLASHTIMES`;
do
echo 1 > /sys/class/gpio/gpio\$LAYER1/value
echo 1 > /sys/class/gpio/gpio\$GPIONR2/value
echo 1 > /sys/class/gpio/gpio\$GPIONR4/value
sleep \$SLEEPSHORT1
alloff
layersoff
echo 1 > /sys/class/gpio/gpio\$LAYER2/value
echo 1 > /sys/class/gpio/gpio\$GPIONR1/value
sleep \$SLEEPSHORT1
alloff
layersoff
done
for i in `seq 1 \$AWESOMEFLASHTIMES`;
do
echo 1 > /sys/class/gpio/gpio\$LAYER2/value
echo 1 > /sys/class/gpio/gpio\$GPIONR2/value
echo 1 > /sys/class/gpio/gpio\$GPIONR4/value
sleep \$SLEEPSHORT1
alloff
layersoff
echo 1 > /sys/class/gpio/gpio\$LAYER1/value
echo 1 > /sys/class/gpio/gpio\$GPIONR3/value
sleep \$SLEEPSHORT1
alloff
layersoff
done
for i in `seq 1 \$AWESOMEFLASHTIMES`;
do
echo 1 > /sys/class/gpio/gpio\$LAYER2/value
echo 1 > /sys/class/gpio/gpio\$GPIONR3/value
sleep \$SLEEPSHORT1
echo 1 > /sys/class/gpio/gpio\$LAYER2/value
echo 1 > /sys/class/gpio/gpio\$GPIONR3/value
sleep \$SLEEPSHORT1
alloff
layersoff
done
}

#FLASHSEQUENCE GOES HERE!!!!! THA MAIN LOOP Y'ALL!!
echo "Flashing LED's...";
for i in `seq 1 \$FLASHTIMES`;
do
#Flashsequence
ledawesomeflash
switchlayer
switchlayer
ledspin
ledspin
switchlayer
ledspin
ledspin
#countdown (doesnt work)
FLASHLEFT=\$((\$FLASHTIMES-1))
echo -n "Flash-cycles remaining: \$FLASHLEFT \r"
done
echo "\nSequence Complete!"

#Turning off and Unexporting GPIO
alloff;
layersoff;
echo \$GPIONR1 > /sys/class/gpio/unexport;
echo \$GPIONR2 > /sys/class/gpio/unexport;
echo \$GPIONR3 > /sys/class/gpio/unexport;
echo \$GPIONR4 > /sys/class/gpio/unexport;
echo \$LAYER1 > /sys/class/gpio/unexport;
echo \$LAYER2 > /sys/class/gpio/unexport;

## Step 5: Optional: Enable Use of the Ledcube Via a Web-browser

Doing this will enable you to use the ledcube with a web-browser.

To do this you have to install wiringpi, you also have to set up your pi as a webserver with php.

https://projects.drogon.net/raspberry-pi/wiringpi/...

Set the pi up as a web-server with php:

There is a plethora of Instructables showing you how to set it up.

Search and you will find ;)

or be lazy:

https://www.instructables.com/id/Raspberry-Pi-Web-S...

(optional) you can also be be somewhat secure:

https://mattwilcox.net/archives/setting-up-a-secur...

also set up static ip:

http://www.suntimebox.com/raspberry-pi-tutorial-co...

https://mega.co.nz/#!75xAmQzB!h0FeZRN_2jTV6ItMGImf...

https://mega.co.nz/#!a0YB2SKJ!2_yR5EHpdmnNw9vys5b...

Now open a new browser window in your computer, then go to:

example:

192.168.0.2/ledcube.php

To be able to connect to your server from the internet, and not just via (W)LAN you must configure your router so it lets traffic through.

Since this can be potentially dangerous without a properly secure setup I don't recommend doing this unless you feel comfortable with the risks.
Disclaimer: If your home network gets hacked, and you somehow blame me; I will only laugh at you.

Setup is individual for every kind of router, so google around for something like:

or

and you should find something to get you started.

But please be careful, the internet can be a a dangerous place. Stay secure!

## Step 6: Add a Case to Make It Less Fragile

I put the cube into a little transparent plastic box i found.

I modified the lid to make room for the circuit board, then i glued it in and taped both parts of the box together.

Participated in the
Make It Glow Contest

Participated in the
Raspberry Pi Contest

21 2.7K
34 3.3K
45 5.8K

## 26 Discussions

What happens i fyou don't use transistors?

the transistors are used to switch the grounds between the different layers, without them it wouldn't work.

I'm not much of a pro,but isn't 25 a ground pin?

is a nice project,any guide on transistors and resistors to use ? im building an 4x4x4 cube using my pi,just waiting my parts to be delivered..any help on code and parts would be great :)
Thnx

Thanks :) For finding proper resistor values, I recommend using a led resistor calculator like this one: http://led.linear1.org/1led.wiz
Just make sure the forward voltage of your LEDs is lower than 3.3V else it wont light up.

Back then I bought them of a norwegian company called elfaelektronikk.no,
But now I buy all my electronic components in bulk on ebay.com, because its cheapest :P

I need help! BAD! I have this project due on Monday May6! Where did I go wrong! I finally got all the correct transistors and resistors, except one. RadioShack didn't have the 61 ohm resistor, so I got a 68 instead because that was all they had. If you could tell me what I did wrong, that would be great! I don't have a 5v power supply so I couldn't do what you told me I could do. Any suggestions?

From the pictures you sent it looks like you have connected everything correctly.
Of course you have a 5v psu ;) just take an old usb cable, cut it in half, then find the black(ground) and red(+5v) wires.
Then use it with a usb wall charger(a phone charger), just be careful not to short circuit anything.

If you have more of the same leds you should test them first.

Also try using a lower value resistor with the transistors, the ones you are using might be too resistive to "switch the transistor on" enough to let the current flow through.

I tried the 5v psu test! Big mistake. It burnt out the leds... so what kind of resistors do you recommend for these leds? They are 3.0-3.2v. Also, when you come up with the resistors, can you tell me the color code? Im a noob like I said before. -_-

Auch :/

For picking the proper resistors for leds i use a "led resistor calculator" if you google it you will find one ;)
as for resistor colors you can google "resistor color chart" or "resistor lookup table".

Also you should double check that your raspi ribbon cable is connected the right way :P

YAY! THE CODE IS WORKING! but now, the leds are not. xD i used the following: 1x brown black brown gold resistor, 2x yellow purple brown, and I did not use the same NPN as you used. I'm going to pick up the ones you used. Thanks :)

Depending on your leds they might not be very bright with 3.3v. Turn off the light and check if you can see a faint glow from the leds. If there is no glow at all, try to testing the cube without the pi using a 5v powersupply. Then it should be easier to figure out whats went wrong.

Can someone please help me? I got all of the wiring down, but not the code. If someone could give me the code that he used, that would be great. PLEASE RESPOND SOON!!! I NEED HELP!

The code is supplied ;) Just copy all the code from the last step and paste it into a shell script.

Nice project, I've built some led cubes, but I never would've thought to use a Raspi to control them. You really should try expanding on this, maybe try setting up a web interface to control the current animation, you would need a web framework for this. Also, pasting raw, unformatted code is  ugly, you should upload the main script in a .py file, and just paste the important parts into the body :)

You've got my vote though.

Doesn't Raspberry Pi use 3.3v on it's GPIO pins? In the tutorial you call for 5v leds.

Also, the script is written in Bash, not python so he needs to save it in an .sh file.