Introduction: Raspberry Pi - Simple 2x2x2 Led Cube

About: Not much to say. I'm a student, learning about computers, and I like to tinker with electronics, 3D-Designing, prototyping and machining

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

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

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

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.

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

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.

Raspberry Pi Contest

Participated in the
Raspberry Pi Contest

Make It Glow Contest

Participated in the
Make It Glow Contest