Qwiic LED Stick - APA102C Hookup Guide
Arduino Examples
The Qwiic LED Stick Arduino Library includes eleven examples to get you started with the basics of the board along with some nifty lighting demos. In this section we'll look at a few of the examples and explore how they work.
Before we get into the examples, let's take a closer look at the setup used for all the examples. The code initializes both the serial and wire ports as well as starts communication with the LED Stick over I2C:
language:c
Wire.begin();
Serial.begin(115200);
if (LEDStick.begin() == false){
Serial.println("Qwiic LED Stick failed to begin. Please check wiring and try again!);
while(1);
}
You can open the Arduino serial monitor with the baud set to 115200 to view any serial printouts from the code. If the Qwiic LED Stick fails to initialize on the bus at the default address, the code freezes. The two most common causes of this is a bad connection between the device and controller or the Qwiic LED Stick's I2C address does not match the default value.
Example 1 - Blink
This basic example shows how to initialize the LED Stick along with controlling the LEDs on it all at once. Open the example by navigating to File > Examples > SparkFun Qwiic LED Stick Arduino Library > Example01_Blink. After opening the example, select the correct Board (in our case the SparkFun RedBoard) and Port it enumerated on. Next, click upload and barring any errors, you should see all ten LEDs set to the same color (white) and turn on and off every second.
Try adjusting the values in this line to change the color:
language:c
LEDStick.setLEDColor(50, 50, 50);
Example 3 - Single Pixels2
The third creates three arrays to set the Red, Green and Blue values for each LED. In the global class, the code creates an array for Red, Blue and Green and sets arbitrary values for each pixel.
language:c
// Pixel# 1 2 3 4 5 6 7 8 9 10
byte redArray[10] = {214, 78, 183, 198, 59, 134, 15, 209, 219, 186}; //r
byte greenArray[10] = { 59, 216, 170, 21, 114, 63, 226, 92, 155, 175}; //g
byte blueArray[10] = {214, 147, 25, 124, 153, 163, 188, 33, 175, 221}; //b
After setting up the three arrays and initializing the LED Stick, the code sets the pixels to the color values to the corresponding entry in the array.
language:c
LEDStick.setLEDColor(redArray, greenArray, blueArray, 10);
For example, the fourth LED should match the color (pink/magenta) of the RGB values in the fourth entry in the array.
Example 4 - Set Brightness
The fourth example steps through all the valid brightness settings for the setLEDBrightness();
function with each LED set to a different color rainbow color (plus white) so you can get a quick idea of how each color looks at each brightness level.
Just like Example 3, the code creates three arrays for Red, Blue and Green and assigns values for each LED. After initializing the LED Stick, the main loop steps through each brightness value every second and prints out the brightness value over serial:
language:c
for (byte i = 0; i < 32; i++) {
LEDStick.setLEDBrightness(i);
Serial.print("Brightness level: ");
Serial.println(i);
delay(1000);
}
These examples round out most of the basics of the Qwiic LED Stick Library. The others include a few demos of cool effects you can do with the Qwiic LED Stick along with changing the I2C address and length of LEDs controlled when using a command not pointed at a specific LED pixel. Take a look at them either in the Arduino IDE or the Library GitHub Repository.