Ambilight Clone with Arduino & WS2801 LED Pixels

Setup Ambilight Clone with Arduino & WS2801 LED Pixels

There are multiple methods and devices to get Ambilight like effects. One of the simplest is to use Arduino with WS2801 or similar lights. Arduino is easy to program with the help of Adafruit library. Boblight Add-On for Kodi (XBMC) along with the Boblight Daemon outputs the required data from the video stream to Arduino via USB or possibly another communication method.

 

Program Arduino

Download Arduino IDE for Windows, Mac or Linux and install it.

 

WS2801 LED modules requires a specific method of communication that would be complicated to to issue commands to directly. Thankfully Adafruit created a library that greatly simplifies this process. Adafruit WS2801 library also requires SPI library which needs to be imported first. SPI library is included by default so it only needs to be imported while Adafruit WS2801 library needs to be added to the Arduino IDE first and then imported to the sketch. Add the library by navigating to Sketch -> Import Library... -> Add Library.... To import a library to the sketch navigate to Sketch -> Import Library..., then select the name of the library to import.

 

Upload the sketch to Arduino Uno or Arduino Pro Micro. Other Arduino models should work too but might require pin connection and possibly other changes to the code below.

#include "SPI.h"
#include "Adafruit_WS2801.h"

#define dataPin 2
#define clockPin 3
#define stripLength 50
#define serialRate 38400
uint8_t prefix[] = {0x41, 0x64, 0x61, 0x00, 0x18, 0x4D};

Adafruit_WS2801 strip = Adafruit_WS2801(stripLength, dataPin, clockPin);

void setup() {
  // initialise our LED strip
  strip.begin();
  // make the first pixel red as an indicator that it is awaiting data
  strip.setPixelColor(0, 10, 0, 0);
  // turn all the other pixels on so we know they're working
  for(uint16_t i = 1; i < stripLength; ++i)
    strip.setPixelColor(i, 5, 5, 5);
  strip.show();
  Serial.begin(serialRate);
}

void loop() {
  // wait until we see the prefix
  for(byte i = 0; i < sizeof prefix; ++i) {
    waitLoop: while (!Serial.available()) ;;
    // look for the next byte in the sequence if we see the one we want
    if(prefix[i] == Serial.read()) continue;
    // otherwise, start over
    i = 0;
    goto waitLoop;
  }
  // read the transmitted data
  for (uint8_t i = 0; i < stripLength; i++) {
    byte r, g, b;
    while(!Serial.available());
    r = Serial.read();
    while(!Serial.available());
    g = Serial.read();
    while(!Serial.available());
    b = Serial.read();
    strip.setPixelColor(i, r, g, b);
  }
  strip.show();
}

WS2801 uses SPI as it’s underlying communication protocol and requires 2 pins to operate. The data pin is set with dataPin variable, while the clock pin is set with clockPin variable. Set the number of LED’s present in WS2801 strip in stripLength variable. Maximum baud rate of the serial port is defined in serialRate variable. This value should match value of rate in boblight.conf configuration file. The prefix[] array sets what Boblight is expected to send before sending pixel data.

Once everything is working correctly it may be a good idea to comment out strip.show(); line within the setup() function to prevent WS2801 strip from lighting up before boblightd has started. This can save power and prevent unnecessary flashing during start up of the computer running Boblight.

 

Wire Arduino to WS2801

Wiring WS2801 LED String can be a bit confusing as there seems to be no standard wire colours. Each string, even from the same manufacturer can have different wire colours. Schematic below shows one possible wire colour combination. One thing that seems to be common is the order of the wires.

The easiest way to connect the string is to connect the voltage (VCC) and ground (GND) and then connect the remaining two by trial and error. There is no danger of damage as long as the VCC is connected to 5V and GND is connected to ground. Data and Clock pins are safe to connect incorrectly.

Arduino Pro Micro connected to WS2801 LED String

Schematic of Arduino Pro Micro connected to WS2801 LED Pixel String

 

Once Arduino sketch has been uploaded successfully it can connected to the system that will control the lights. A USB cable will provide both the data transmission required by Boblight and power required by Arduino. Once this is done it is ready to receive command from Boblight which means it is time to setup Ambilight with Kodi & Boblight Add-On on Arch Linux.

Once everything is functional it may be needed to tinker with the placement of the Arduino as interference may manifest itself as unwanted flickering. Placing Arduino away from WS2801 string and any other electronics is the first thing to try. If flickering persists, shielding in the form of a metal case for the Arduino may be needed.

dom