Raspberry Pi 3×3 LED Cube

Raspberry Pi 3x3 LED Cube

About a month ago my dad bought a soldering iron and we both learnt to solder by making an LED cube controlled by an Arduino. It was a cool project to show my friends and really easy to build so I have decided to make one for the Raspberry Pi and an Adafruit Cobbler because there are not many LED cubes for the Pi.

Bits needed

  • Breadboard
  • 27 LEDs
  • Soldering iron and solder
  • Wires
  • 3 transistors
  • Any Raspberry Pi
  • Adafruit Cobbler (optional)

Step 1: Building the LED Cube

Building the LED Cube

Start by measuring the length of the shorter wire coming out of the LED. This gives you the maximum distance between the LEDs in the cube so that you can draw a 3×3 grid on a piece of plywood and drill 9 holes the same size as the LEDs e.g. 3mm or 5mm.

Test each LED works by running a wire from the Raspberry Pi’s 5v and ground pins and touching the LED wires.

Put a LED in each hole and bend the shorter wire flat as shown in the diagram. Each wire should touch the shorter wire of the next LED but should not touch the longer wire that points straight up.

Now solder the short wires together to create one layer of the cube. I then tested the layer by putting the ground wire against any of the shorter pins that were just soldered. By touching the longer vertical wires with the 5v wire then the LED should light up. Ease the LED layer out of the holes and create 2 more layers exactly the same.The first time I made a cube I got my dad to cut me some wood the right thickness to hold one layer of LEDs above the other. The second cube I made at Coventry Makerspace so used some of their 3D printed pens and they were the perfect size. So one layer goes back into the plywood jig, the pens went down to support the next layer and I soldered the vertical wires to the layer above. I moved the pens up a layer and finished the soldering. Finally a test of the LEDs by putting the ground wire on a layer and completing the circuit by touching the other pins with the 5v wire. If everything is working then the corresponding LED should light up.

The final bit of soldering is to attach a wire to each layer of the cube so that they can be linked to the breadboard.

Step 2: Building the Circuit

Building the Circuit

The next bit is fiddly and involves you placing the 9 pin of the cube into the breadboard such that each pin is separate from the others. I did it by twisting the cube slightly.

The Adafruit Cobbler is attached to the breadboard and it is simply a case of connecting 9 GPIO output pins to the 9 LED cube vertical wires. Putting power to one of more of these pins will allow the corresponding column of 3 LEDs to potentially light up. To complete the circuit we need to attach each of the 3 layers to ground. If we attach them directly to ground then all three layers will always be connected to ground and you will only be able to light up a column of LEDs instead of individual ones.

So this is where we use the transistors. They were described to me as little switches with 3 pins. The 2 pins on the left and right are like the ends of the wire and for this circuit you attach 1 wire to ground and the other to the wire leading from the layers. If we put power to the middle pin then the switch is turned on and the other 2 wires become connected. So by using 3 more GPIO output pins we can connect them to the middle pin of the 3 transistors.

The electronics part of this build is now complete and we have the ability to send power to each column of LEDs and can turn on and off the connection of the levels to ground. We can now turn on any LED with a combination of LED column and layer being activated.

Step 3: Programming

Programming

The following simple Python program demonstrates how to light up the LEDs in pretty patterns:

#**************************************************************************************

#*

#* Title: 3×3 LED Cube #* #* Description: Simple LED cube for soldering practice and demonstrate some #* array handling with GPIO pins #* – Pins P1 to P9 are connected to LED Cathode (-) #* – Layers are connected to LED Anode (+) #* #* Author: Jacob Groves, Coventry Makerspace #* #**************************************************************************************

import RPi.GPIO as GPIO from time import sleep

# Assign the individual LED pins a variable name . . . P1 = 13 P2 = 19 P3 = 26 P4 = 23 P5 = 24 P6 = 25 P7 = 16 P8 = 20 P9 = 21

# Assign the layers to a nice variable . . . Top = 17 Middle = 27 Bottom = 22

# LED sequences represented by an array . . . aPins = [P1, P2, P3, P4, P5, P6, P7, P8, P9] aPinsReversed = [P9, P8, P7, P6, P5, P4, P3, P2, P1] aPinsStar = [P1, P2, P3, P6, P9, P8, P7, P4] aPinsStar2 = [P9, P8, P7, P4, P1, P2, P3, P6] aPinsSpiral = [P1, P2, P3, P6, P9, P8, P7, P4, P5] aPinsSpiralReversed = [P5, P4, P7, P8, P9, P6, P3, P2, P1] 

aLayers = [Top, Middle, Bottom]

# Time delay built in between pin output . . . delay = 0.15

#************************************************************************** #* setupColumns - Initialisation of the pins . . . #************************************************************************** def setupColumns():

# GPIO pin addressing will use the virtual number . . . GPIO.setmode(GPIO.BCM)

# Initialise the pins so we can output values to them . . . GPIO.setup(aPins, GPIO.OUT) GPIO.setup(aLayers, GPIO.OUT)

GPIO.output(aPins, False) GPIO.output(aLayers, True) #************************************************************************** #* cyclePins - Receive array of pins & light them up one by one . . . #************************************************************************** def cyclePins(pPins):

for iPin in pPins: GPIO.output(iPin, True) sleep(delay) GPIO.output(iPin, False)

#************************************************************************** #* cyclePinsPair - Receive 2 arrays of pins & light them up in pairs . . . #************************************************************************** def cyclePinsPair(pPins, pPins2):

for iPin in range(len(pPins)): GPIO.output(pPins[iPin], True) GPIO.output(pPins2[iPin], True) sleep(delay) GPIO.output(pPins[iPin], False) GPIO.output(pPins2[iPin], False)

#************************************************************************** #* Mainline logic . . . #************************************************************************** try: print('Press CTRL+C to stop script') i = 0 while(true):

# Spinning star . . . GPIO.output(aLayers, True) GPIO.output(P5, True) cyclePinsPair(aPinsStar, aPinsStar2) cyclePinsPair(aPinsStar, aPinsStar2) GPIO.output(P5, False)

# Zig-zaggy columns . . . GPIO.output(aLayers, True) cyclePins(aPins) cyclePins(aPinsReversed) GPIO.output(aLayers, False)

# Spiral columns . . . GPIO.output(aLayers, True) cyclePins(aPinsSpiral) cyclePins(aPinsSpiralReversed) GPIO.output(aLayers, False)

# Zig-zaggy layers . . . GPIO.output(Top, True) cyclePins(aPins) GPIO.output(Top, False) GPIO.output(Middle, True) cyclePins(aPinsReversed) GPIO.output(Middle, False) GPIO.output(Bottom, True) cyclePins(aPins) GPIO.output(Bottom, False) sleep(delay) except KeyboardInterrupt: GPIO.cleanup()

finally: GPIO.cleanup()

de>

 

Source: Raspberry Pi 3×3 LED Cube


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top