Introduction: Raspberry Pi - Simple 2x2x2 Led Cube

Picture of 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)
- 1x breadboard
- 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)
- some breadboard wires

Step 1: Making the Led Cube

Picture of 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

Picture of Wiring It Up

WARNING! ALWAYS TEST YOUR CIRCUITS BEFORE YOU CONNECT THEM TO YOUR RASPI (burnt pi is bad)
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

Picture of 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

Picture of 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

Picture of 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.

Download wiringpi (so you can use the ledcube without sudo) and follow the instructions from here:

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...

Download ledcube.sh from here and put it in your pi3:

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

Download ledcube.php and put it into the /var/www in your pi3:

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

Now open a new browser window in your computer, then go to:
"your pi's IP-Address"/ledcube.php

example:

192.168.0.2/ledcube.php

then click "Blink the ledcube"

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:

"your routers name" DMZ host

or

"your routers name" port forwarding

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

Picture of 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.

Comments

PauloB17 made it! (author)2015-10-24

Hi! I was able to successfully follow all your instructions... and it worked!!! but only the first time... The following day I went back to run the script one more time and I get this:

Mickey_Boy (author)PauloB172016-01-29

you're trying to sun a .sh (shell) script with python.


run .sh files with:
sudo sh file.sh

CharlesC63 (author)2015-12-03

What happens i fyou don't use transistors?

Mickey_Boy (author)CharlesC632016-01-29

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

AtulA (author)2014-08-05

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

Mickey_Boy (author)AtulA2015-01-06

GPIO25, not pin 25 ;)

AlexP7 (author)2015-01-06

a short question, which OS did you used or better which terminal, and can I add this to an autostart, I need that it runs like this.

thanks

Mickey_Boy (author)AlexP72015-01-06

raspbian w/ standard raspbian terminal.

AlexP7 (author)2015-01-06

a short question, which OS did you used or better which terminal, and can I add this to an autostart, I need that it runs like this.

thanks

AlexP7 (author)2015-01-06

a short question, which OS did you used or better which terminal, and can I add this to an autostart, I need that it runs like this.

thanks

acano4 (author)2014-07-27

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

Mickey_Boy (author)acano42014-07-27

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.

William06171 (author)2014-05-18

I did it! It is so cool! Thank you so much for help and this project!

William06171 (author)2014-05-05

Where did you get the leds? Amazon?

Mickey_Boy (author)William061712014-05-05

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

William06171 (author)2014-05-03

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?

Mickey_Boy (author)William061712014-05-03

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.

William06171 (author)Mickey_Boy2014-05-04

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. -_-

Mickey_Boy (author)William061712014-05-04

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

William06171 (author)2014-05-02

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 :)

Mickey_Boy (author)William061712014-05-02

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.

William06171 (author)2014-05-01

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!

Mickey_Boy (author)William061712014-05-01

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

Droxz made it! (author)2014-02-23

Thx for this great tutorial!
I'm a just a beginner, but this is great!

1 little thing, in your example above:
"Say you have a lead from the cube connected to GPIO8 and one of
the NPN-transistors connected to GPIO-25."
But what you build, and what we can see on the pictures,
you are using GPIO 8 and 18... so it was a bit confusing for me (as a noob) :D

After building this cube, i build the 3x3x3 cube, see the picture below.
Maybe you can help us out with making a bigger one? :)

Mickey_Boy (author)Droxz2014-02-25

I'm glad you found this helpful :)
Sorry for the inconsistency of the 'ible :P I'm a noob myself, and it was my first one :P
I don't have enough leds or enough time to make a bigger one. Sorry :(
There are lots of other bigger led-cube projects on instructables, I'm sure you can figure it out ;)

Michael_Bell (author)2013-11-19

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.

atomic3 (author)Michael_Bell2013-11-21

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.

Mickey_Boy (author)atomic32013-11-21

yes, the gpio from the raspi are 3.3v. Except for the 5v pin ofc :P I used 5v leds because that was all I had in my arsenal at the time :P

Michael_Bell (author)atomic32013-11-21

I imagine that it would still light up, it just would not be as bright, and yes it should be a .sh file, I have python on my mind and I typed .py instead.

Mickey_Boy (author)Michael_Bell2013-11-20

Thanks for your feeback :)
Web interfacing is on the list, but I'll make a 3x3x3 (12 pin total) or 3x4x4 (16 pin total) cube first. Just added a the last step with the sloppy code as an afterthought, I know its ugly :P But i will format it so it looks slightly nicer ;)

yokozuna (author)2013-11-19

Good project, would love to see a video of it in action.

Mickey_Boy (author)yokozuna2013-11-20

Video is up now ;)

Mickey_Boy (author)yokozuna2013-11-20

Hmm, I just might make one ;)

About This Instructable

20,033views

41favorites

License:

Bio: Not much to say. I'm a student, learning about computers, and I like to tinker with electronics, 3D-Designing, prototyping and machining
More by Mickey_Boy:Raspberry Pi - PiFMPlay - FM-Broadcasting SimplifiedRaspberry pi - simple 2x2x2 led cube
Add instructable to: