Home > TKJ Electronics > Bluetooth controlled RGB light strip

Bluetooth controlled RGB light strip

In December last year we developed a small Bluetooth controlled RGB light strip for the christmas tree, to be controlled with your Android smartphone

The demonstration of the project and the smartphone control can be seen in the video below.

The main aspect of the project is to use an Arduino to parse incoming Serial commands to enable and set different effects for the attached RGB strip.
The code for the project, including the Arduino code and the Android application project, can be found on GitHub: AndroidControllableLights

Another interesting aspect of the project was to enable wireless update of the Arduino sketch, using the Bluetooth serial connection. Scroll down in this post if you are only interested in figuring out how this can be done.

RGB Strip
The RGB strip contains 50 pixels, each with an individual WS2801 chip. These chips makes it possible to control the colors of each pixel on the strip using serially clocked and latched data with only 2 pins. The data pin of the strip is connected to Arduino pin 8, while the clock pin is connected to pin 9.
By using the WS2801 library by Adafruit the control of the colors on the strip is fairly easy task.

setStripColor(R, G, B);
strip.show();

Animations and effects
The next step is the control of the lights and some different effects and animations.
In this project I have decided to make the following effects:

  • Set color directly
  • Fading between set colors
  • Snap between set colors
  • Scroll of set color pattern
  • Running fade og set colors

Parsing of Serial commands
We are now ready to parse incoming Serial data and interpret it as commands to enable, set or disable the effects.
I decided to make the following commands which is then parsed when received Serially, either from the Bluetooth module or from a PC connected using USB.
The first byte of a command package is the command byte, descriping the command of the package. The next bytes are all parameters as described below. Each parameter is terminated with a ‘;’ character, also when only one parameter is needed.

The project contains the following commands:

  • 0x0F – Set color [Parameters: 3 bytes of 3 char each]
  • 0x01 – Add fade color [Parameters: 3 bytes of 3 char each]
  • 0x02 – Reset fade colors [Parameters: None]
  • 0x03 – Set Speed/Delay [Parameters: 1 integer of 5 chars]
  • 0x04 – Enable fade effect [Parameters: None]
  • 0x05 – Add snap color [Parameters: 3 bytes of 3 char each]
  • 0x06 – Reset snap colors [Parameters: None]
  • 0x07 – Enable snap effect [Parameters: None]
  • 0x08 – Add scrolling color [Parameters: 3 bytes of 3 char each]
  • 0x09 – Reset scrolling colors [Parameters: None]
  • 0x0A – Enable scrolling effect [Parameters: None]
  • 0x0B – Add running fade color [Parameters: 3 bytes of 3 char each]
  • 0x0C – Reset running fade colors [Parameters: None]
  • 0x0C – Enable running fade effect [Parameters: None]
  • 0x0E – Disable any effect [Parameters: None]

Android Application
The most difficult part for me was the Android application.
I managed to put down a relatively simple but powerfull application to control send the specific Serial commands over Bluetooth to the Arduino.
The Android project can also be found on GitHub: ColorLightsController

“Color Lights Controller” – the Android control application


To help me get started with the Bluetooth communication I have used the following tutorial: ANDROID AND ARDUINO BLUETOOTH COMMUNICATION
And for the color selector I found this great AmbilWarna Color Picker together with a tutorial on how to use it in your own application.

OBS. The current version doesn’t have any device selection dialog for the Serial Bluetooth module, so the module name is “pre-programmed” into the application: if(device.getName().equals(“quadcopter”))

So you will have to change this accordingly before starting the application on your own phone.

Wirelles (Bluetooth) update of the Arduino
One final aspect of the project, was the ability to update the Arduino sketch wirelessly, so I didn’t always have to connect a USB cable.
As the Bluetooth module I am using doesn’t have any DTR pin for resetting the board, starting the Arduino bootloader, I had to improvise and implement this “reset into bootloader” myself.

I started trying the most known reset solution, the Watchdog. But the problem with our goal in resetting into the bootloader is, that when you do a Watchdog reset an internal flag is set which then skips the bootloader.
So a real “hardware” reset is necessary.

A solution for this was to connect an analog output pin (PWM pin) to the Reset pin of the Arduino. I decided to use pin 7.
By using an Analog output pin the state of it is left unchanged while the Arduino is booting, making sure that it doesn’t go into a “restart-loop”.

We are now able to hardware-reset the board by writing 0 to that pin (GND), which sets it up and drives the reset pin low!

void resetIntoBootloader(void)
{
  analogWrite(7, 0);
}

While we have now overcome the issue of the DTR connection we still need to know when to execute the resetIntoBootloader function.
This is where the specific Arduino bootloader character sequence comes in handy.

When you press the upload button of your Arduino IDE your board is reset due to the DTR pin and the IDE then starts spitting out “0 0 0 0 0 “.
We can use this sequence to check for a zero and then a space, and if that is recognized then execute the resetIntoBootloader function.

char bootloaderPrelimary = 0;
void checkBootloader(char serialChar)
{
    if (bootloaderPrelimary == 0) {
      if (serialChar == '0')
        bootloaderPrelimary = 1;
    } else {
      if (serialChar == ' ')
        resetIntoBootloader();
      else
        bootloaderPrelimary = 0;
    }
}

Everytime we receive a character we need to check it with this ‘checkBootloader’ function first, to see if it is some bootloader characters.

I have implemented this in the top of the main loop, before doing any of the command parsing.
So for everytime we receive a character we check it to see if it is a bootloader character, and if not, we are going to parse it as a command byte.

You can see the lines of code you will have to add to your loop function here: Bootloader check sequence

Finally you will have to remember to set your Bluetooth module to a Baud-rate of 57600 – as this is the rate the Arduino bootloader is using.
That is also why the sketch is set to use 57600 as the buad-rate.

I guess this is it for now.
I hope you enjoyed this small but handsome Christmas lighting project.

Happy new year everyone!

Categories: TKJ Electronics Tags:
  1. James
    February 3rd, 2013 at 06:46 | #1

    Hi Thomas,

    Congrats, that’s an excellent project and a great idea! 😉

    Well, since you have share all this information know-how about this project, then I got an Arduino UNO, an HTC Sensation Android phone, 100 led pixels with the WS2801 driver and I’m waiting for the bluetooth adapter HC-06, however, when I tried to mount your Android project on Eclipse, it shows 59 errors (only 4 different errors), but in the Arduino it’s all fine with the code!

    The errors are:

    – AmbilWarnaDialog cannot be resolved to a type
    – OnAmbilWarnaListener cannot be resolved to a type
    – R cannot be resolved to a variable
    – The import yuku cannot be resolved

    I suppose that if we resolve each one of these, all the rest is gonna be solved too. Can you help me on this, please? Could be the way that I mounted the project in Eclipse? I would love if I could run this on my phone, it would be very very handy and helpful.

    Thanks very much,
    James

  2. February 3rd, 2013 at 21:35 | #2

    @James
    The reason for the errors you are getting is due to the missing AmbilWarna library.
    I have now updated the Git repository to include this library the right place.

    So what you should do now is to pull the latest files from the repository and import both projects by using “File->Import->Existing Projects into Workspace” and then select the main cloned Git repository folder.
    You should then see two projects being imported – this is OK and you press Finish.

    Now the errors you are referring to should disappear.

    Best Regards
    Thomas Jespersen

  3. James
    February 3rd, 2013 at 23:48 | #3

    Thanks very much Thomas, that was the piece of the puzzle that faulted. It works perfect now!

    Cheers,
    James

  4. Reader
    February 4th, 2013 at 19:36 | #4

    Hi,
    the APP crash on my Phone Galaxy S3. The original an the modified.
    Can you post the Final Code from this Project who the mods are inside.

    Greets from Germany

  5. Taylor
    May 2nd, 2013 at 23:10 | #5

    Hi Thomas,
    the android APP in Git repository compile and run in Android >=4.1.2 always Crash when click on the Select Color.
    Have you an updated version of the app?

    Best regards,
    Taylor

  6. JM
    July 9th, 2013 at 19:46 | #6

    Very cool project do you have updates that dont crash for android 4.1 ?

  7. September 24th, 2013 at 08:00 | #7

    Sorry, but we haven’t been working on this project for a while, so support for >=4.1 has not been added yet.
    Feel free to fork the code from Github and make the necessary changes to add this support, and then send us a pull request.
    Regards Thomas

  8. December 29th, 2013 at 23:20 | #8

    I have just updated the Android app, so it works with newer devices as well. The source is available at Github: https://github.com/TKJElectronics/AndroidControllableLights/tree/dev.

  9. Med
    May 11th, 2017 at 16:48 | #9

    Hi, i have a problem. The arduino code doesn’t compile.
    this is what i get “fatal error: EEPROMAnything.h: No such file or directory

    #include “EEPROMAnything.h” ”
    What is the problem ?

  10. May 11th, 2017 at 21:04 | #10

    @Med
    Have you downloaded the whole repository including the EEPROMAnything.h file found here: https://github.com/TKJElectronics/AndroidControllableLights/blob/dev/EEPROMAnything.h

  11. Med
    May 11th, 2017 at 22:36 | #11

    @Thomas Kølbæk Jespersen
    I found what was my problem, i didn’t add the “EEPROMAnything.h” file in the IDE. Thanks TKJ and great job on the work.

  12. Hari
    October 31st, 2018 at 18:13 | #12

    how to get apk of project help

  13. November 4th, 2018 at 21:30 | #13
  14. Raj
    November 6th, 2018 at 09:47 | #14

    Bro please give me the APK file I can’t make the APK from the source code please give me fast. It’s showing alot of errors

  1. No trackbacks yet.