Controlling a RGB LED attached to a Raspberry Pi through Android

This was a bit of a project that I used to learn some new technology.  Note: I am not an EE and I am just learning how to do this.  Proceed with caution if you want to repeat.

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.

Design

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

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.

The 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.

raspberry-pi-rgb-controller

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.

EndpointMethodDescription
/led/<color>GETGet the status of the current LED color (On or Off)
/led/<color>/onPUTTurn the led color on
/led/<color>/offPUTTurn the led color off
/led/<color>/togglePUTToggle the LED from off to on or vise-versa.

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

Source Code

Android 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).

Results

 

 

  1. Code? please from your android app

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>