Sunday, June 29, 2014

LPD8806 RGB Light Strips - Using an Arduino Mega to count the pixels

The goal of this project is to count the number of LED pixels on a  LPD8806 light strip using SPI MISO and the DO (Data Out) pin at the end of the strip. I used the hardware SPI pins on the Arduino Mega 2560 because it is faster and more reliable than a bit banged software SPI. The code will send data to the light strip increasing the number of pixels on the strip each time until it reaches a maximum number of pixels specified in the code, or until data is found in the Arduino's SPI MISO buffer.  I used 100 as the maximum but this could be changed to any number.

This technique can be used to connect a strip of any length and dynamically determine the number of LED pixels on the strip at start up. This allows for trading out light strips of different lengths without changing the code, as long as the maximum number of LED pixels set in the code is greater than the number of the longest light strip that might be used.

The disadvantage of this technique is that you need a physical connection to the end of the light strip. It would be really cool if they added a 5th connection to the strip that was pass through. Then the DO at the end of the strip could be connected to the pass through line and all the connections here could be at the beginning of the strip.

Click Here to get the Code

LPD8806 RGB Light Strips and SPI 

The LDP8806 light strips are used as a slave device on a SPI bus to transfer data to each pixel in the strip.The hardware SPI (Serial Peripheral Interface) available on the Arduino Mega 2560 is on Pins 50, 51, and 52. The Arduino is the master device, and the light strip is the slave device.

As shown in the image below, pin 52 is the CLK (Clock), pin 51 is the MOSI (Master Out Slave In), and pin 50 is the MISO (Master In Slave Out). The MOSI and CLK pins are used to send data to the light strip. The MISO pin will be used to receive data from the light strip.

Arduino Mega 2560 Hardware SPI Pins

The light strip has 4 connections at the beginning of the strip labeled GND, DI, CI, and +5V and 4 connections at the end of the strip labeled GND, DO, CO and +5V. The connections at the beginning of the strip are used to send data to the light strip and power the light strip. The DO pin at the end of the strip is used to receive data back from the strip.


LPD8806 RGB Light Strip Pins
Light Strip Data
At the beginning of the light strip, the DI and CI pins are the Data In and Clock In pins. The DI pin is connected to the Arduino MOSI (pin 51) and the CI pin is connected to the Arduino CLK (pin 52).
The DO pin at the end of the strip is connected to the MISO (pin 50) on the Arduino. If this pin is not connected, you will never receive data back from the strip!

Powering the Light Strips
The light strip requires an external 5 volt power supply. The Arduino cannot supply enough power to run these strips.


Step 1: Making the Hardware Connections

Parts Needed:


  1. Arduino Mega 2560 ($16)
  2. LPD8806 RGB LED Light Strip ($30)
  3. Power Supply to supply 5 Volts to light strip - I used an old PC power supply but this one will also work, Power Supply + 5 volts ($8) 
  4. You may also need connectors to connect the power supply to your bread board depending on your setup
  5. A bread board 
  6. Some connection wires
  7. A Soldering Iron  (to make connections to the light strip)
  8. A voltage meter to measure supply voltages and ensure they are at or around 5 volts. 


Here is the diagram of the circuit I used: 
Circuit Diagram: LPD8806 RGB LED Light Strip and Arduino Mega

Connections:

Connections at the START side of the Light Strip
  1. Identify the START side of the light strip, this is the side with the labels DI, and CI on the two center connection pads. Connect the GND on START side of the light strip to the power source GND on the bread board. 
  2. Connect the +5V pad on the START side of light strip to the power source +5V on the bread board.
  3. Connect the DI pad on the START side of the light strip to the Arduino Mega's MOSI (pin 51). 
  4. Connect the CI pad on the START side of the light strip to the Arduino Mega's CLK (pin 52). 

Connections at the END side of the Light Strip
  1. Connect the DO pad on the END side of the light strip to the Arduino Mega's MISO (pin 50).
  2. Optionally, you can also connect the GND and +5V on the END side of the light strip to the power source GND and +5V.

Additional Arduino Connections
  1. Connect the Arduino Mega GND pin to the GND from the power source. 
  2. Plug in the USB connection from the Arduino to your Computer.
Make sure the power to your power source is turned on. 

Step 2: Loading and Running the code

  1. Click here to download the sketch for the Arduino
  2. Open the Sketch in the Arduino IDE. 
  3. Select the Arduino Mega 2560 under Tools->Boards in the Arduino IDE
  4. Make sure the Port for the Arduino is selected under Serial Ports
  5. Change  the maxLEDs value in the code to your preferred maximum. This should be a number larger than the largest number of LED pixels any of your LED strips might have.
  6. Compile and Upload the sketch. The code will start running immediately and the LED's on the light strip should turn blue one by one. 
  7. Open the the Serial Monitor from the Arduino IDE to see the output from the code. If everything is wired correctly, you should see the following line at the end of the output with the correct number of pixels for your light strip:
             "Found 64 LED pixels in the strip.
  8. If the MISO/DO connection is not connected, then the following line will be seen at the end of the output:
    "No Data received after testing the maximum number of LEDs 100 Is the DO on the light strip connected to the MISO pin?"