Android App – RGB LED with Arduino and Bluetooth

In this project you’re going to build an Android app to control the color of an RGB LED with a smartphone via Bluetooth.

You’re going to build the Android app using a free web based software called MIT App Inventor 2. This is a great project to learn how to interface the Arduino with a smartphone.

If you’re not familiar with RGB LEDs, read the following post: How do RGB LEDs work?

Download Android Apps for Arduino with MIT App Inventor 2

Parts Required

Here’s a complete list of the parts required for this project:

Additionally, you also need a smartphone with bluetooth.

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Creating the Android App

The Android App will be created using a free web application called MIT App Inventor. MIT App Inventor is a great place to get started with Android development, because it allows you to build simple apps with drag-n-drop.

You need a Google account to sign up for MIT App Inventor and here’s the login page: http://ai2.appinventor.mit.edu.

After login in go to Projects >Import project (.aia) from my computer and upload the .aia file. Click here to download the .aia file.

import

After uploading the .aia file. You’ll see the application on the MIT App Inventor Software.

Designer

With MIT App Inventor you have 2 main sections: designer and blocks.

The designer is what gives you the ability to add buttons, add text, add screens and edit the overall app look. Int the App Inventor software the app looks like this:

The app looks different in the software and in your smartphone. This is the how the app looks in our smartphone:

Blocks

Then, you have the blocks section. The blocks sections is what allows to create custom functionality for your app, so when you press the buttons it actually does something with that information. These are the blocks for this app (click on the image to zoom):

Installing the App

To install the app in your smartphone, go to the Build tab.build

  • You can either generate a QR code that you can scan with your smartphone and automatically install the app in your smartphone.
  • Or you can download the .apk file, connect your smartphone to your computer and move the .apk file to the phone.

Simply follow the installation wizard to install the App and it’s done!

Code

Download or copy the following code to your Arduino IDE, and upload it to your Arduino board. Make sure you have the right Board and COM port selected.

Note: before uploading the code, make sure you have the TX and RX pins disconnected from the bluetooth module!

/*
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */

#define max_char 12
char message[max_char];    // stores you message
char r_char;               // reads each character
byte index = 0;            // defines the position into your array
int i;

int redPin = 11;     // Red RGB pin -> D11
int greenPin = 10;   // Green RGB pin -> D10
int bluePin = 9;     // Blue RGB pin -> D9

int redValue = 255;     // Red RGB pin -> D11
int greenValue = 255;   // Green RGB pin -> D10
int blueValue = 255;     // Blue RGB pin -> D9

String redTempValue;     // Red RGB pin -> D11
String greenTempValue;   // Green RGB pin -> D10
String blueTempValue;     // Blue RGB pin -> D9

int flag = 0;
char currentColor;  

void setup() {
  pinMode(redPin,OUTPUT);
  pinMode(bluePin,OUTPUT);
  pinMode(greenPin, OUTPUT);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  //while is reading the message 
  while(Serial.available() > 0){
    flag = 0;
    //the message can have up to 12 characters 
    if(index < (max_char-1)){         
      r_char = Serial.read();      // Reads a character
      message[index] = r_char;     // Stores the character in message array
      if(r_char=='R'){
         currentColor = 'R';
         redTempValue = "";
      }
      else if(r_char=='G'){
         currentColor = 'G';
         greenTempValue = "";
      }
      else if(r_char=='B'){
         currentColor = 'B';
         blueTempValue = "";
      }
      if(currentColor == 'R' && r_char!='R'){
         redTempValue += r_char;
      }
      else if(currentColor == 'G' && r_char!='G'){
         greenTempValue += r_char;
      }
      else if(currentColor == 'B' && r_char!='B'){
         blueTempValue += r_char;
      }
      index++;                     // Increment position
      message[index] = '\0';       // Delete the last position
   }
   
 }
 
 if(flag == 0){
   analogWrite(redPin, 255-redTempValue.toInt());
   analogWrite(greenPin, 255-greenTempValue.toInt());
   analogWrite(bluePin, 255-blueTempValue.toInt());
   /*Serial.print('R');
   Serial.println(redTempValue);
   Serial.print('G');
   Serial.println(greenTempValue);
   Serial.print('B');
   Serial.println(blueTempValue);
   Serial.print("MESSAGE ");*/
   Serial.println(message);
   flag=1;
       for(i=0; i<12; i++){
      message[i] = '\0';
    } 
    //resests the index
    index=0;  
 }

}

View raw code

Schematics

Follow the schematic diagram in the following figure to wire your circuit.

Note: If you’re using an RGB LED common cathode, you need to connect the longer lead to GND.

Important: Here’s the bluetooth module connections to the Arduino:

  1. Bluetooth module TX connects to Arduino RX
  2. Bluetooth module RX connects to Arduino TX

Here’s how your circuit should look:

Launching the App

If you haven’t generated the .apk file in a previous step, you can click here to download the .apk file (which is the Android App installation file). Move that file to your smartphone and open it. Follow the installation wizard to install the app.

Turn on your smartphone’s Bluetooth.

screenshot_2016-10-09-15-14-52-copy

Make sure you pair your smartphone with the bluetooth module – search for paired devices in your smartphone’s bluetooth settings.

Then, open the newly installed app. Tap on the Connect bluetooth button to connect via bluetooth to the arduino bluetooth module.

Select your Bluetooth module (it should be named linvor).

Now, it’s ready to use!

Demonstration

Here’s your app in action. Move the sliders and click on CHANGE COLOR to set your RGB LED color to the selected color.

Troubleshooting

1.  I can’t upload code to my Arduino board.

Check if you have the TX and RX cables from the bluetooth module disconnected.

When you’re uploading code to your Arduino you should disconnect the TX and RX cables from the bluetooth module. These pins are needed for serial communication between the Arduino and your computer.

 2. I can’t find my bluetooth module device.

Make sure you have paired your smartphone with your bluetooth module. Go to your bluetooth settings and search for the available devices. Your bluetooth module device should appear (it’s often called: linvor, HC-06, HC-04, HC-05 …). Pair with it. If it asks for a password, it’s 1234.

3. The app doesn’t interact with the Arduino.

If your Android app is connected to your bluetooth module, it should display the “Connected” message (as shown below). Otherwise, press the “Connect Bluetooth” to establish a bluetooth communication.

Double-check your bluetooth module connections:

  1. Bluetooth module TX connects to Arduino RX
  2. Bluetooth module RX connects to Arduino TX

4. My bluetooth module is asking for a password.

If your bluetooth module asks for a password, type 1234.

Wrapping up

In this project you learned how to control the color of an RGB LED with an Android App built with the MIT App Inventor 2 software.

Now, feel free to change how the app looks and give it more functionalities.

If you like this project, make sure you check our latest course: Android Apps for Arduino with MIT App Inventor 2.

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

9 thoughts on “Android App – RGB LED with Arduino and Bluetooth”

  1. Is there the possibility of using a ‘colour wheel’ within AppInventor 2?

    Will you be doing a similar project with the ESP-32, as that has built-in Bluetooth and works with the Arduino IDE?

    Reply
      • I tried in on the DOIT ESP32 Dev Kit V1 (36 Pins) and it works well.
        Now I have a Geekreit ESP32 Dev Kit V1 (30 pins) and the blue tooth is not there, I could not detect it on my phone. Any thought?

        Reply
  2. Thank you very much for a great program. With small changes I was I able to control 3 servos using my tablet.

    A small thing: I do not understand the need for the following 3 lines:

    int redValue = 255; // Red RGB pin -> D11
    int greenValue = 255; // Green RGB pin -> D10
    int blueValue = 255; // Blue RGB pin -> D9

    Just out of curiosity I commented them out and the sketch still works.

    Thanks.

    Reply
    • Hi.
      You are right.
      Those variables aren’t being used, so you can delete them.
      Thanks for noticing.
      I’ll update the code.
      Regards,
      Sara

      Reply
  3. Thank you for this very useful tuto, I wonder, would it be possible to use similar sliders to control how long and how often the LED would turn on and off, like with the “blink” script?

    … and then maybe do the same with a relay, with many possible applications (timer, etc.).

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.