What are we making?
We are going to create a controllable 6x7 LED grid with minimal components.
Charlieplexing is a method to individually control more LEDs than pins used. This method is popular since it doesn't require any additional components like shift registers.
It mostly works by changing the mode of two pins to an input and an output to light a specific LED. By cycling through and lighting up each LED at a time quick enough it gives the illusion of multiple LEDs being lit at once.
The formula for working out the amount of LEDs you can control from the amount of pins (N) you want to use is:
So 3 pins = 6 LEDs, 4 = 12, 5 = 20, 6 = 30, 7 = 42, 8 = 56, 9 = 72 and so on.
For this tutorial we are going to use 7 pins, so that means we have 42 LEDs that we can control.
The Adafruit Huzzah is an ESP8266 breakout board - I used it for this project its easier to program and power compared to a bare ESP8266, but still is still super cheap. This board is great because it has built in WiFi, so we can add the ability to change the grid over the Internet.
Step 1: Parts Needed
FTDI Cable - Needed to program the Huzzah via USB
42 LEDs - 42 matching LEDs, and not too big
7 Resistors - Find some resistors that match your LEDs
Some small wires to connect stuff on the breadboard - stubby little ones rather than long ones, so that they don't get it the way of the LEDs
3.7v LiPo Battery - Optional to make it portable.
PHP server - We need somewhere to put our code that the device can access and you can visit to change the grid.
The Huzzah comes with headers unsoldered, so unfortunately you'll have to solder them yourself. This tutorial from Adafruit is super useful.
Also download the attached code.zip which contains the PHP code, test sketch and final WiFi sketch.
Step 2: Set Up the Arduino IDE
First off you'll need to add support for the board following this tutorial is you haven't already.
You'll also need to install the pfodESP8266WiFi library. This library seemed to fix most of the flickering by not blocking the main loop as much the default esp8266wifi one when it was making network requests.
Step 3: Place Wires on Breadboard.
Step 4: Place LEDs on Breadboard
Once you have placed the wires on the bread boards, then start placing the LEDs.
For rows 1,3,5 the LEDs must all have the anode (longer leg) on the left side.
For rows 2,4,6 the anode is on the right.
Step 5: Add Resistors and Connect to the Huzzah
The value resistors you want to use will probably depend on the LEDs you use, I've used 10Ω (Brown Black Black) for this.
Then connect each of the resistors to the Huzzah (Which we've placed on it's own breadboard).
1st -> pin 5
2nd -> pin 4
3rd -> pin 15
4th -> pin 16
5th -> pin 14
6th -> pin 12
7th -> pin 13
Step 6: Download and Run Test Sketch
Try running this sketch to test out the different pins, if all is well then it should cycle through each row one by one. If not, then check your wiring.
Step 7: Set Up PHP Script
Once you've checked that cycling through each LED works, the next step is to add an interactivity.
We're going to use a PHP script to manage our LEDs, since it's easier to get online.
Create a folder on your server and move the two files in the PHP folder of code.zip
We have one page, index.php and if called with GET parameters, will do other stuff. We have an accompanying text file.
I've made a basic API to control the LEDs with these methods, and all of the methods return a JSON state of the LEDS, except ?read=simple which returns a simplified version with less punctuation for the ESP8266 to parse.
?light=[number / all]
The API is a okay, but things can go wrong occasionally. It things don't seem right check state.txt isn't broken or missing.
Try it out
You should be able to click different circles on and off and refresh the page to check it's all working. The next step is to upload a new sketch to the device that loads the state of the LEDs from the server.
Step 8: Edit and Run WiFi Sketch
First of all add your WiFi details to the sketch.
Then change the variables at the top of the sketch to match the server and location of your PHP script.
At every interval (default 500ms) the script will make a call to the server and load the current state of the dots, it will then parse the result and updates the ledState variable, which is used by the script to light up the LEDs.
Try going to your PHP page to test if its all working and you can control the LEDs.
Step 9: Experiment!
It should be possible to make a 72 LED version with the 9 regular GPIO pins on the Huzzah!
Also, try playing around with the code to see if you can develop it further, or create a PHP script to change the state.txt depending on an API
You could also not use WiFi at all and instead create a cool animation that loops.
Participated in the
Arduino All The Things! Contest