DotStar rainbow on an Arduino Uno
A rainbow flows along a 30-LED APA102 (DotStar) strip on a data and a clock pin, and the Serial Monitor shows how long each show() takes. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Lights
- Using the APA102 (DotStar) LED strip
- Reading and changing a sketch
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
A rainbow flows along a 30-LED APA102 (DotStar) strip on a data and a clock pin, and the Serial Monitor shows how long each show() takes.
Arduino Uno R3 runs the sketch below. The circuit around it: APA102 (DotStar) LED strip. delay waits 20 milliseconds. Serial.begin opens the serial port at 9600 baud.
Parts list
3 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × APA102 (DotStar) LED strip
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| APA102 (DotStar) LED strip pin CI | Arduino Uno R3 pin 5 |
| APA102 (DotStar) LED strip pin DI | Arduino Uno R3 pin 4 |
| APA102 (DotStar) LED strip pin VCC | Arduino Uno R3 pin 5V |
| APA102 (DotStar) LED strip pin GND | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// DotStar rainbow: a rainbow flows along a 30-LED APA102 (DotStar) strip,
// and the Serial Monitor shows how long each show() takes.
//
// strip APA102 strip, 30 LEDs: DI <- pin 4, CI <- pin 5, VCC to 5V, GND
//
// An APA102 has a clock wire as well as a data wire. The LED reads the data
// on every rising edge of the clock, so it does not care how fast or slow the
// edges come: a plain digitalWrite() loop is fine, and so are interrupts in
// the middle of a frame. A WS2812 needs its bits timed to a fraction of a
// microsecond instead.
//
// What show() sends, 32 bits at a time:
// start frame 32 zeros
// each LED 111, a 5-bit global brightness, then blue, green and red
// end frame ones, one clock for every two LEDs, to push the last
// frames down the strip (each LED delays the data half a clock)
//
// The colors go out blue first, so the strip is made with DOTSTAR_BGR, the
// order in the APA102 datasheet.
//
// Things to try. Change SPEED. Change the brightness. Make the strip with no
// pins, Adafruit_DotStar strip(LEDS, DOTSTAR_BGR), and wire DI to pin 11
// (MOSI) and CI to pin 13 (SCK): hardware SPI sends the same frames much
// faster, and the show() time drops.
#include <Adafruit_DotStar.h>
#include <SPI.h>
const int LEDS = 30;
const int DATA_PIN = 4;
const int CLOCK_PIN = 5;
const uint16_t SPEED = 512; // how far the hue moves each frame, of 65536
Adafruit_DotStar strip(LEDS, DATA_PIN, CLOCK_PIN, DOTSTAR_BGR);
uint16_t first = 0;
unsigned long frames = 0;
void setup() {
Serial.begin(9600);
strip.begin();
strip.setBrightness(96);
strip.show(); // every LED off
Serial.println("DotStar rainbow: 30 LEDs on pins 4 (data) and 5 (clock)");
}
void loop() {
// One turn of the color wheel spread over the strip, starting at first.
strip.rainbow(first);
unsigned long t = micros();
strip.show();
t = micros() - t;
first += SPEED;
frames++;
if (frames % 50 == 1) {
Serial.print("show() took ");
Serial.print(t);
Serial.println(" us");
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(9600);
strip.begin();
strip.setBrightness(96);
strip.show(); // every LED off
Serial.println("DotStar rainbow: 30 LEDs on pins 4 (data) and 5 (clock)");
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
// One turn of the color wheel spread over the strip, starting at first.
strip.rainbow(first);
unsigned long t = micros();
strip.show();
t = micros() - t;
first += SPEED;
frames++;
if (frames % 50 == 1) {
Serial.print("show() took ");
Serial.print(t);
Serial.println(" us");
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.