In this lesson we will use a microcontroller to control a strip of LEDs. The microcontroller will be able to control the color and brightness of every LED on the strip. You will need the following equipment
Computer with Arduino IDE
Sparkfun Redboard
LED Strip
Here are the steps to control the LED strip
[ ] Log in to your computer
[ ] Start the Arduino IDE by selecting the Raspberry icon in the upper left hand corner, then Programming, then Arduino IDE
[ ] Select File | Examples | FastLED | DemoReel100. The DemoReel100 program will appear in the editor.
[ ] Count the number of LED elements on your LED strip. An element looks like this:
[ ] Edit the DemoReel100 program so that the constant "NUM_LEDS" reflects the number of LEDs on your strip.
#define NUM_LEDS 12
[ ] Use a jumper wire to connect from a ground connection on the microcontroller board to a ground connection on the LED strip. The traditional color for a ground wire is black, so that is a good choice if you have one in your pile of wires. On the Redboard, the ground connections are labeled as "GND". Push the pin into the socket that is next to this label. On the rectangular LED strip it should be easy to find another "GND" label. In fact there are two of them, and you can plug into either one. On the circular strip, the labels may be hard to read. With the circular strip turned with the LEDs laying down, the "GND" connection is the one on the right end of the connector.
[ ] Use a jumper wire to connect from a 5 volt power connection on the microcontroller board to a a power connection on the LED strip. The traditional color for a power connection is red. On a Redboard, the power connection we want to use is labeled "5V", meaning 5 volts. On the rectangular LED strip, the power connection is labeled "4-7VDC", meaning that any voltage between 4 volts and 7 volts should work. On the circular LED strip, the power connection is the one in the middle.
[ ] The last connection is called the "data line". This is the connection that the microcontroller uses to send commands to the LED strip. The microcontroller side of the connection is specified in the Arduino program.
#define DATA_PIN 3
Find the connection labeled three on the microcontroller, and connect this to the "DIN" or "IN" connection on the LED strip. "DIN" means data in, since the microcontroller sends data in to the board that holds the LEDs to control them.
[ ] Use the USB cable to connect the computer to the microcontroller.
[ ] Under the Arduino IDE, select Tools | Board | Arduino/Genuino Uno
[ ] You may have to adjust the selection under Tools | Port, but the current setting may also work without change. A typical setting is "/dev/ttyUSB3"
[ ] Select the blue arrow icon that appears under the Edit menu. When you click this, the computer should process your program, and transfer the program to the microcontroller over the USB cable. Status messages will appear at the bottom of the Arduino IDE window when this is happening. When the program is being downloaded to the microcontroller, some LEDs on the microcontroller board will blink rapidly for a few seconds. Then, the LEDs on the strip should start blinking!
Here are some changes you can make to the program.
Change the value of NUM_LEDS. Will the program still work?
Change the setting for DATA_PIN. Does the program still work? If the program isn't working now, can you make a change to the wiring so that it will work?
You can probably guess what changing BRIGHTNESS will do. Are you right?
After you make a change to the program, click the blue arrow to load the new version of the program into the microcontroller.
What do the other examples under File | Example |FastLED do? Can you get all of them to work?
Now that we see that the example program works, it's a good time to load a simpler example of our own. If you select File | Open..., you can follow the path /home/pi/Arduino/led_array and find a file named led_array.ino. By convention, files designed to run under the Arduino IDE end in ".ino". If you double click this file, the IDE will load this simple program.
/* Light LEDS on LED Array */
#include <FastLED.h>
#define DATA_PIN 3
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 8
#define BRIGHTNESS 20
CRGB leds[NUM_LEDS];
void setup() {
delay(3000);
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
int position;
void loop() {
int i;
for (i = 0; i < NUM_LEDS; i++)
leds[i] = CRGB::Black;
leds[position] = CRGB::Red;
position = position + 1;
if (position == NUM_LEDS)
position = 0;
FastLED.show();
delay(200);
}
The top of the program is similar to the DemoReel100 program we were running earlier, but the bottom of the program, starting at the line that reads
void loop() {
is intended to be simpler, and easy to experiment with. Let's go through this section line by line, or maybe two lines at a time, to figure out what this part of the program is doing. The very first line...
void loop() {
is something we don't have a lot of control over, since all Arduino IDE programs have this line in them. This is the start of the main loop for the program. The processor on the microcontroller will run every line between here and the end of the program, over and over again. Almost all Arduino IDE programs try to have some useful statements in this loop, but it isn't necessary. For example, you could have the program do all of it's work in the set up section of the program (can you find that section?) and have an empty loop that looks like this.
void loop() {
}
We have some statements in our loop though, so let's continue looking at them.
int i;
This line is a variable declaration. It sets up a variable named "i" which will store an integer value. An integer is a number that is a positive whole number, a negative whole number, or 0. Once we put this line in our program, we can use "i" to keep track of a number, which we are about to do.
for (i = 0; i < NUM_LEDS; i++)
leds[i] = CRGB::Black;
The next two lines in the program make a "for loop". This loop uses our variable "i" to count through all of the LEDs in our LED array, and turn them off. Inside the parentheses on the first line, the expression "i = 0" starts us off by setting variable "i" to 0.
Next on this line there is a semicolon to indicate the beginning of the next section, which is a "loop test". The loop test is a check that is made repeatedly to see if the loop should be performed again. The loop test "i < NUM_LEDS" means that the processor should continue running the loop so long as the value of "i" is less than NUM_LEDS, which is the constant that we were experimenting with earlier. NUM_LEDS represents the number of LEDs in our array.
Next on this line there is another semicolon, and then a funny little expression "i++". This part of the for loop definition provides an action for the processor to take after it is done executing the loop. "i++" means that the variable "i" should be incremented, or bumped up by one. Since we set an initial value of 0 for "i", if the processor executes this section, it will change the value of "i" to 1.