Controlling a RGB LED attached to a Raspberry Pi through Android

Project Description

I need to be able to control turning on and off an RGB LED utilizing the Raspberry Pi.  I also should be able to turn it on and off using an Android device.


The design is made up to utilize three different components: Raspberry Pi / LED Hardware, Web Service, and Android device.

Controlling a RGB LED attached to a Raspberry Pi through Android

Raspberry Pi

All the gear that was used was:

  • Raspberry Pi – Model B
  • 5mm High Brightness Full-Color LED
  • Breadboard
  • Resistors
  • 3 x Transistors – 2N3904

Some of the constraints I also have to work with are:

  • Each of the 3.3V GPIO pins can handle a maximum current of 16mA.  They might be able to do more, but from what I read, it would not be for long.
  • The Pi takes about 700mA of the total power without anything plugged in (USB, HDMI, etc), so depending on the power adapter used, there might not be enough power.  In this case, I used a 2A plug.
  • Since each color will require more than 16mA of power to turn on, I need to utilize transistors and the 5V pin from the Pi.  I will use the GPIO to handle closing the circuit on an NPN transistor.  I believe I need to use NPN due to the fact that the LED has a common anode.

Controlling a RGB LED attached to a Raspberry Pi through Android schematicThe R1 resistors are a complete bit of voodoo to me.  I just don’t know enough to do the calculations correctly, so I know they are under powered.  I also took readings and they are no where near high enough to be a concern.  The best I could come up with was that the β / H(fe) for each is about 60 or 54.  That is probably wrong though, but I’m not sure how to follow the white sheet correctly.  I also wasn’t sure what or how to find what the voltage drop is (I hear .7V or .9V), which goes into the calculation.  I checked with the multimeter afterwards and all pieces were very low.

Web Service

To handle allowing an android device to turn the LED on and off, I needed to create a web service that runs on the Raspberry Pi that would allow easy operations to occur.  Due to the fact that I needed to stay a little more light weight, not to mention I wanted to learn Python, I wrote the quick REST service using Python.  To get the work done, I used the following libraries / packages:

I utilized Flask to quickly prototype up a REST service and inside that service, I’m using the gpio-python to handle turning on and off the GPIO pins.

I did go through this quick, so the REST service really isn’t pretty.  It should handle cleanup still and also considerations if it is used in a multi-threaded fashion for keeping track of resource setup, teardown, and usage.

The LED colors are red, green, or blue.  The response code is just plain old text.

Source Code

ndroid Device

Nothing special here.  Just used the Android SDK and Eclipse.  I then used a Nexus 7 to handle playing with it.  Again, this should have been done a little different if real life, but for now it was quicker to use AsyncTask.  As long as I don’t turn it before I get a response (really hard to do), then I am fine.  There are different opinions on the correct way, which is either an invisible fragment or using a command system to a service running in the background (I prefer the latter, but more complex).




Source: Controlling a RGB LED attached to a Raspberry Pi through Android

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top