Tutorial 13: Programming DotStars (APA102)

In the previous tutorial I showed you how to connect DotStars to your Arduino and upload a sketch to light them up for the first time. The tutorial quickly went through the code in the example upload. Great for ensuring we're wired correctly, but we are left not understanding how to make our own patterns on the strips.

In this tutorial I go a bit deeper on how you can write colors to individual LEDs and introduce some programming concepts that allow you to create patterns (including for loops, if statements, and bitwise operations). By the end of this tutorial you will understand how to send the correct color and brightness to a LED such that it can work for your project.


  • Arduino Uno
  • USB-A – USB-B Cable
  • DotStars (APA102) LEDs
  • Wires that bring it all together
DIFFICULTY
MEDIUM
C++ PROGRAMMING
SOME
ABOUT
0
MINUTES
  • How to write the status of a single LED
  • How to perform bitwise operations to achieve correct color codes
  • How to create simple functions
  • How to create a pattern for a light show

You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!

simpleon.ino


#include <Adafruit_DotStar.h>
#include <SPI.h>

#define NUMPIXELS 30

// Create the strip

Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BRG) ;

void simpleOn(uint32_t color, uint8_t head, uint8_t tail) {
  for (int i=(tail-1); i < head; i++){
    strip.setPixelColor(i, color) ;
  }
  strip.show() ;
}

void setup() {
  strip.begin() ;
  strip.show() ; // This turns off the LEDs within NUMPIXELS

}

void loop() {
  uint8_t head, tail ;
  uint32_t color ;
  color = 0x1577D5 ;
  head = 7 ;
  tail = 2 ;
  simpleOn(color, head, tail) ;
  delay(1000) ;

}
//>

lightbounce.ino


#include <SPI.h>
#include <Adafruit_DotStar.h>

#define NUMPIXELS 30

Adafruit_DotStar strip = Adafruit_DotStar(NUMPIXELS, DOTSTAR_BRG) ;

void bounceBetween(uint32_t color, uint16_t start, uint16_t finish, uint16_t len){

  int path_length = finish-start ;
  
  // Get light snake moving forward down the line.
  for (int i=0 ; i < path_length ; i++){
    // Populate the snake
    for (int j=0 ; j < len ; j++){
      if ((j+i+start) <= finish){
        strip.setPixelColor(i+j+start, color) ;
        strip.show() ;
      }
    }
    // Write the end of the snake to be "off"
    if ( (i+start) <= (finish-len) ){
      strip.setPixelColor(i+start, 0) ;
      delay(50) ;
    }
  }

  // Get the light snake moving backwards.
  for (int i=0 ; (-1)*i < path_length ; i--){
    // Populate the snake
    for (int j=0 ; (-1)*j < len ; j--){
      if ((i+j+finish) >= start){
        strip.setPixelColor(i+j+finish, color) ;
        strip.show() ;  
      }
    }
    // Write the end of the snake to be "off"
    if ( (i+finish) >= (len+start) ){
      strip.setPixelColor(i+finish, 0) ;
      delay(50) ;
    }
  }
}

uint32_t randColor(){
  uint8_t red = random(0,255) ;
  uint8_t blue = random(0,255) ;
  uint8_t green = random(0,255) ;

  uint32_t color = 0x000000 ; //GRB

  color = color | (green << 16) ;
  color = color | (red << 8) ;
  color = color | blue ;

  return color ;
}

void setup() {
  strip.begin() ;
  strip.show() ;
}

void loop() {
  uint32_t color = randColor() ;
  bounceBetween(color, 2, 12, 4) ;
}
//>
You shouldn’t need anything else to get going with this tutorial.