Intro: GUI Controlled LED Matrix
I recently got an Arduino Uno, and it quickly became my favorite toy. After learning the basics of how the platform operates, I became interested in interfacing the Arduino with personal computers. After seeing a large number of LED matrix projects on Instructables and the rest of the web, the LED matrix seemed like a perfect project to attempt to interface with a computer.
I chose to build a 3 x 3 matrix, because I thought it would be best to start small (and I did not have enough LED's for a larger one). Also, 3 x 3 is about the largest size it is safe to power directly from the Arduino, without using an external power supply. However, the design for the matrix and the software in this Instructable should be fairly easy to scale up to 8 x 8 or so, by using an external 5V power supply and some transistors.
Step 1: Tools, Materials and Resources
To build the matrix, you will need:
- Arduino microcontroller: I used an Arduino Uno, but pretty much any will work.
- 9 LED's: Any sort should work. I had some left over from my bicycle's spoke lights
- 3 resistors: These are to prevent the pins/LED's from burning out. I used the 330 Ohm variety, but anything around there should work
- A breadboard
- 1 Foot of thin gauge electrical wire: To connect the negative ends of the matrix to the breadboard
- 6 breadboard jumpers
- Soldering materials
Step 2: Building the Matrix
The matrix itself is fairly simple to build, and if you are building one as small as 3 x 3, it can be built without any sort of jig to align the LED's or circuitboard to hold the components.
To start out, bend the anodes (the longer leads) of all 9 LED's to a 90 degree angles, perpendicular to the plane the leads are on. Then bend the cathodes (the shorter leads) to 90 degree angles parallel to the plane of the leads. Make sure the leads extend farther out of the LED before bending on the cathodes than the anodes.
Next, divide the LED's into groups of three. Solder the cathodes of all three LED's together, so that the LED's form a chain. The anodes of the chain should all stick out in the same direction. Once you have done this three times, you will have three chains of three LED's. Then you can solder the anodes of each chain to the anodes of the next chain. If all your bends are 90 degrees, and you secure the LED's while you solder them, the LEDs' leads should form a nice grid.
Once all the LED's are soldered together, solder a resistor to the three anode leads that are not connected to other LED's. Finally, solder a short length of wire to each of the three cathode leads protruding from the matrix.
Step 3: Attaching the Matrix
I attached the leads to the Arduino in the following order:
- Left anode: Pin 2
- Center anode: Pin 3
- Right anode: Pin 4
- Top cathode: Pin 5
- Middle cathode: Pin 6
- Bottom cathode: Pin 7
Step 4: Programming the Arduino
Now, I do not know if there is a standard for how LED displays cycle through the rows/columns, but I wrote my POV controlling program from scratch. If you go down to the basics, it works like this:
The Arduino's code contains a set of arrays (one could also use a single 2D array), one for each row, that indicates which LEDs in the row are activated. The program scans through these arrays one at a time, and the Arduino lights only the LED's which are indicated in the one row. It lights the LED's in the row by setting the pin for the row to "LOW" and the pins for the lit LED's to "HIGH." Every cycle, the program also runs a function which gathers input over a serial connection. Inputs are in the form of integers, which correspond to an LED on the matrix. The matrix is arranged as such:
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
If the code receives the number "6" over serial, the state of the bottom left LED in the array is switched.
Due to its length, I will not paste the code into this text, but you can download a commented version of it just below:
Step 5: Programming the User Interface
I programmed the program and GUI that control the LED matrix in Python, using the built-in Tkinter graphics module. The design of the GUI is very simple. It contains nine buttons, each representing one LED. When the buttons are clicked, they turn orange, and when they are clicked again they turn white. As the buttons are clicked, they update the LED matrix in real-time.
In order for Python to communicate with the Arduino, it needs the Pyserial module. Once this is installed, you can read and write to the Arduino over the serial connection.
In my program, whenever a button is clicked, the button calls a function which writes the number of the button to the Arduino. Using that number, the Arduino figures out which LED the number corresponds to, and switches the state of that LED.
The Python file for the UI is attached below. When running it, there are a few things to note:
If you run the file in Windows, it is advisable to simply double click the file instead of opening it in IDLE (the python IDE) and running it from there. Also, you will have to set the serial port to use within the code. By default, it is set to 2, which corresponds to COM3 on Windows.
Mac users: I have not tested the program on a mac. Please let me know if there are any problems with it, so I can try to fix them. Python sometimes behaves differently on Macs, especially when dealing with graphics stuff.
Step 6: Using the Matrix, Conclusion
To test the matrix, plug it in to your computer, and download the program to it from the Arduino IDE. Then run the Serial Monitor. You can use the number keys, 0-8, to test each LED and make sure the program is running correctly. Then you can run the Python GUI program. Before you do so, make sure you have closed the Serial Monitor in the Arduino IDE, or the Python will not be able to connect over Serial, since the port is already in use. When you quit the GUI, you can go ahead and connect to it in the Arduino IDE again, because the GUI closes its connection when it quits.
Have fun playing with your GUI controlled LED matrix! I have found that it is almost infinitely entertaining to small children. If you build your matrix with different color LED's, you can easily change the color of the buttons in the GUI to match your LED's, using hexadecimal color codes. The section of the code that must be changed is clearly commented.
I would like to create a much larger 8x8 version of this matrix. 8x8 is the largest size you can build on an Arduino Uno without using shift registers, charlieplexing, or controlling circuitry other than some transistors for the power. If you build one of these, please post pictures, and I will post them in this last step. The construction of the matrix actually very little work. Programming it took far longer, and I have already done that part for you.