Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

Connect a multicolor LED strip to your smartphone to make a touch-controlled light show! You can attach the color-changing LEDs to the underside of a coffee table, your bike, or anything else you think needs a bit more color.

There are many ways to connect your smartphone to an embedded microcontroller. In this project you’ll use an Android phone in USB host mode. This way, the Android phone both powers and communicates with the Arduino! Even though the Arduino is connected via USB, the communication happens via serial, just like when an Arduino is connected to your computer.

Once you’re able to make your phone talk to an Arduino, a whole world of new projects opens up!

image14

Download the code

CAUTION: Build this project at your own risk. We fried a phone during our test build. Not all Android phones are compatible with USB Host Mode. Use only compatible phones, with the proper Android operating system and USB Host Mode drivers. To learn more, visit How can I determine if my device has USB host mode (OTG) support?

Steps

Step #1: Build the circuit.

PrevNext
Android-Arduino LED Strip LightsAndroid-Arduino LED Strip LightsAndroid-Arduino LED Strip LightsAndroid-Arduino LED Strip Lights
  • RGB LED strips usually have 4 wires: one for power, and one each for red, green, and blue control. When the strip is powered, pulling any of the control lines to ground will cause that color LED to light up at full brightness. Using pulse-width modulation (PWM) on those control lines allows you to modulate the brightness of the lights.
  • A strip 1m long can draw nearly 1A when the red, green, and blue LEDs are on full brightness. An Arduino’s output pins can only supply around 40mA each, so you’ll have to help it out with a driver circuit to boost the power. This circuit takes 3 PWM signals from the Arduino and uses them to drive 3 transistors supplying power to the red, green, and blue LEDs — giving you full control over the brightness of each color, so you can mix them to create any color in the spectrum.
  • NOTE: Want to power 1 or 2 LEDs instead of a whole strip? Ignore the transistors and connect the LEDs directly to the Arduino’s output pins.
  • The driver circuit (1st photo) is a basic transistor amplifier repeated 3 times. A 5V, low-current PWM signal comes from the Arduino to the transistor’s base (B) via a 1K resistor. This signal switches the transistor, allowing it to conduct higher current at 12V across the collector (C) and emitter (E) through the LEDs. The transistor can switch fast enough so that the LED power is PWM’ing just like the input signal, resulting in the desired brightness control.
  • NOTE: Take care to keep straight which transistor will control which LED color, and connect the transistors the right way around, according to the pinout of the TIP31 transistor (2nd photo). And don’t forget to connect the Arduino’s ground pin to the 12V power supply’s ground!
  • Your completed circuit should look like the 3rd photo. The outputs of the circuit go to a 4-pin header on the right, ready to accept an RGB LED strip.

Step #2: Hack a cable (optional).

PrevNext
Android-Arduino LED Strip Lights

Many Android phones are USB On-The-Go (OTG) devices, which means they can act as the USB host (providing power) or slave (receiving power). In order for your phone to host a USB device such as an Arduino, you need a USB OTG cable. You can buy these for a few bucks online, but if you have old USB cables around it’s more fun to make one Follow the instructions here .

Step #3: Install the Android app.

PrevNext
Android-Arduino LED Strip Lights
  • By adding a few lines of code to an Android app, you can send any kind of data to the Arduino via your USB OTG cable. In this case, you’ll send brightness values between 0 and 255 for the red, green, and blue LEDs. The critical piece of the puzzle here comes from Mike Wakerly. He wrote a fantastic driver for the USB-to-serial chips used in Arduino boards, called usb-serial-for-android. Sending data to an Arduino from your phone is as simple as calling device.write() within your Android app!
  • One great example of this is Katie Dektar’s open-source app called Color Namer (github.com/kaytdek/ColorNamer). For this project, I simplified Color Namer into a version called Arduino Color. Download the app and installer at trevorshp.com/creations/ArduinoColor.apk. You can also grab the complete source code at github.com/trevorshannon/ArduinoColor.

Step #4: Code your own app (optional).

PrevNext
Android-Arduino LED Strip Lights
  • If you’re developing your own app, download Wakerly’s driver from code.google.com/p/usb-serial-for-android and copy the usb-serial-for-android JAR file into the [your_app_root]/libs directory. This is a pre-compiled library that includes all the necessary functions to open, close, write to, and read from a Serial device from your Android app.
  • Next, copy the device_filter.xml file into the [your_app_root]/res/xml directory. This file acts as a lookup table for USB devices connected to your phone. When you connect an Arduino, its device ID (set by the manufacturer of the USB-to-serial chip) is sent to the phone. If this device ID appears in your app’s device_filter.xml file, then your app will automatically open. (The Arduino’s ID is already in Wakerly’s device_filter.xml file, so you shouldn’t need to make any changes to that file.)
  • Finally, make your app aware that it should be looking for USB devices in the first place. Open your app’s AndroidManifest.xml file and add the appropriate information within your tags: example code 1.
  • To integrate Wakerly’s library, you first have to write code that opens the serial device when the app begins and closes the device when the app stops. Here is some code from Arduino Color as an example: example code 2.
  • Then, you can send data to the opened serial device using the write() function. For example, the Arduino Color app will send color data via serial when the user touches the color picker: example code 3.

Step #5: Upload the Arduino firmware.

PrevNext
Android-Arduino LED Strip Lights
  • The Arduino code for this project is fairly simple; download it at trevorshp.com/creations/android_leds.ino, then open it in the Arduino development environment (IDE) and upload it to your Arduino board.
  • The code uses the Serial library, which is provided with the Arduino IDE and makes it easy to read data sent via serial to the microcontroller. The code endlessly loops while querying the number of available bytes on the serial port. When there are at least 3 bytes available, the bytes are read and stored in an array as red, green, and blue brightnesses. Those brightness levels are then used to set the PWM outputs accordingly.
  • Notice that there are constants called max_red, max_green, and max_blue. You may find that your LED strip is not well color-balanced. In theory, if you turn on the red, green, and blue LEDs at max brightness, the resulting light should be white. In practice it may have a slight color cast (generally green or blue). You can compensate for this by tweaking the max value constants that are called by the map function. See example code 4.

Step #6: Connect it all together.

PrevNext
Android-Arduino LED Strip LightsAndroid-Arduino LED Strip LightsAndroid-Arduino LED Strip LightsAndroid-Arduino LED Strip Lights
  • You’re ready to begin your light show! Connect the Arduino to your phone and you should see a notification pop up asking if you’d like to open the Arduino Color app. Yes, please!
  • Connect the LED strip to your driver circuit, and turn on the 12V power supply. Using the Android Color app on your phone, you should now be able to adjust the color of your light strip in real time.
  • Attach the light strip to the underside of a coffee table, bring it to a nighttime party, or stick it on your bike!

Conclusion

Other Explorations

Now that you understand how to talk to an Arduino with an Android app, you can take this project further in all kinds of ways:

  • Go wireless: add a Bluetooth or Wi-Fi module to your Arduino and get rid of the cable.
  • Make lights that change color based on the orientation of your phone.
  • Use the GPS in your phone as well as some fancy sensors connected to the Arduino to make a location-aware data logger.
  • Use an individually addressable LED strip and adjust the code for more control over the light show.

To learn more about getting started with Arduino, check out our best-selling book Getting Started with Arduinoand our Arduino projects and tutorials, from beginner to advanced.

Comments

  1. Billy Plante says:

    Is it possible to use more than just 1 meter of LED lights? How would I go about adding more?

    1. 44 says:

      search for higher voltage mosfet

      1. Trevor Shannon says:

        to power more LED’s (i.e. a longer strip) you’ll need a higher *current* transistor. The voltage specs of the transistor can stay the same as the TIP31.

  2. Catz says:

    I would like to know how I can control the arduino if I don’t have an android phone / tablet. This is the only reason why I wouldn’t build this. I’ve got an Nokia Lumia 920, iPad 2 WiFi and Macbook Pro. Can I use any of these devices?

    Even better would be a button that will allow me to turn it going from 0 – 255 changing colours.

  3. Dan Nixon says:

    This is cool, but I think a better version would allow control over a network, that way almost any device can control the lighting.
    Here’s my take on it anyway: http://www.instructables.com/id/Arduino-Web-Enabled-RGB-Lighting/

    1. Mazeyar Moeini says:

      Wow Dan Nixon. I made your project on instructables I think it is better than this. Can you make a second version were you can play a song on it, while the LEDs get brighter with the music.

      1. Dan Nixon says:

        Well I did do the simple one which used Processing and Minim, but for a more useful one I wouldn’t know where to start.
        I could make a WinAmp visualisation plugin for it but other than that I wouldn’t know, for Android I assume you would have to make the entire media application as I doubt the type of information you need to make a visualisation would be provided to 3rd party apps by a media player.

  4. Kerimil says:

    Or better yet use bluetooth and appinventor to create your own custom apps, example apps here ->

    http://www.youtube.com/watch?v=cwFBOEtaORM

    http://www.youtube.com/watch?v=Y-iDuUQCxQA

  5. brian van vlymen says:

    its so awesome for moderate level. it was so excellent video! thanks you so much

  6. Jhon says:

    My HTC DESIRE HD is not supporting OTG host cabel.
    What can I do????
    HELP!!!!

  7. ron says:

    I also have the same problem.help.

  8. efrain says:

    hey excuse me but i’m trying to make this proyect and i have a problem at the time of compiling the arduino program :

    sketch_oct02a.cpp: In function ‘void loop()’:
    sketch_oct02a:32: error: ‘class HardwareSerial’ has no member named ‘readBytesUntil’

    what do i do to solve this? please help me.

    1. Kerimil says:

      You’re using older version of arduino IDE. Download the newest version here -> http://arduino.cc/en/Main/Software

      also so as not to double post I made a similar app but using bluetooth for communication so you don’t need an OTG USB compatible phone for it to work – pretty much any phone with bluetooth should work

      more info:
      http://forum.arduino.cc/index.php?topic=148853.msg1408773#msg1408773

  9. rey says:

    When i connect my HTC One(M7) with Arduino. it shows the USB device is not supported, can anyone help me with that? and I try not to root my phone so would be great if there is other solutions other than rooting the phone

  10. Sina says:

    hey there, I have followed the step precisely and yet i cant get it to work. i have checked the circuit for over 100 times but everything is correct . is it possible that the arduino isnt sending pulses to the transistors???

  11. Frank Nativi says:

    HELP! the LED i got from radio shack doesn’t have the ground o power wire. it only has 3 wires rgb. what do i do. i have all the parts but i don’t want to blow this whole thing up.

  12. Mayen says:

    i cant run the app on my Motorola Photon 4g, (com.trevorship.arduinocolor).stopped unexpectedly. Help pls.

  13. jbachalo says:

    This is great Trevor, but would you know of a similar example that uses the Android Open Accessory protocol, so that the Android device receives power thru USB?

In the Maker Shed