WS2801LED Arduino Sketch¶
This sketch is used by Exercise: WS2801 RGB LED SPI Module.
Full Source Code¶
The full code is all in one file WS2801LED.ino.
1// -*- mode:c++; -*-
2/// @file WS2801LED.ino
3///
4/// @brief Example for driving several WS2801 RGB LED modules over SPI on an Arduino UNO.
5///
6/// @author Garth Zeglin
7/// @date 2014-09-13
8///
9/// @remarks The WS2801 LED driver has three current controlled LED outputs with
10/// 8-bit precision (256 levels). It is controlled over SPI by sending 24 bits
11/// of pixel data at up to 25MHz rate. Additional data is then relayed to the output
12/// SPI pins to feed daisy-chained drivers. Once the bus is quiescent for 500
13/// microseconds, the data is applied to the outputs and the chip is ready to
14/// receive more values.
15///
16/// So a large number of of drivers can be fed on each channel, limited only by
17/// the overall refresh rate. But the data must be fed without interruption to
18/// avoid prematurely ending the cycle.
19///
20/// The actual color data sequence depends upon the wiring of the module; on one
21/// particular strip light tested the actual sequence was blue-red-green.
22///
23/// Note that the relay scheme means that first three bytes output feed the
24/// first module, the second three the second module, etc. In other words, the
25/// strand is not a shift register. Extra data has no effect.
26
27/// This sketch assumes the following electrical connections from the Arduino to
28/// the first module in a chain:
29
30/// PIN11 (MOSI) -> DAT
31/// PIN13 (SCK) -> CLK
32/// GND -> GND
33
34// Include the SPI library.
35#include <SPI.h>
36
37
38void setup()
39{
40 SPI.begin(); // initialize SPI hardware
41}
42
43void loop()
44{
45 static int cycle = 0;
46
47 // on each iteration, shift out RGB data for several pixels, then delay to allow the device to update.
48 for (int pixel = 0; pixel < 3; pixel++ ) {
49 uint8_t red = 3 * (cycle + 32*pixel);
50 uint8_t green = 5 * (cycle + 32*pixel);
51 uint8_t blue = 7 * (cycle + 32*pixel);
52 SPI.transfer( blue );
53 SPI.transfer( red );
54 SPI.transfer( green );
55 }
56
57 delay(1); // delay 1 millisecond to allow outputs to update
58 cycle++; // update the overall animation
59
60 // add a delay to control the overall frame rate
61 delay(20);
62}