Setting up a HC-05 Bluetooth module with Arduino to control an RGB LED

Updated again (19/Sept/15): Aaagh! I had the resistors in the voltage divider part other way around. I’ve edited the diagram to give the correct way around to ensure only 3.3V of the Arduino TX signal makes it into the Bluetooth module.  Interestingly enough even with the resistors the wrong way around, it still worked!

Update: 4th Sept 2015. I re-pasted GreatScott’s code below, as somehow and somewhere I may have accidentally inserted a ‘stray’ backslash, i.e. a ”\”   into the code; The code now verifies perfectly. End of update.

) First off Respect to GreatScott for his fantastic video from where I leaned of this. His video is here: https://www.youtube.com/watch?v=x3KAXjnP06o

Here’s a suggested schematic for the breadboard set-up to be able to control an RGB LED from a smartphone to a bluetooth HC-05 module which is connected to an Arduino microcontroller.

Click image to enlarge.

RGB LED Bluetooth and arduino resistors revised

GreatScott uses pin 8 for the red PWM, pin 9 for the green PWM and pin 10 for the blue PWM, and his code is set up for that. Here’s a copy of his code (as KYUEM blocks the place where he stored the file 😦  so just copy and past the below code into the Arduino IDE).

//http://www.youtube.com/user/greatscottlab
int ledred=8;
int ledgreen=9;
int ledblue=10;
int tx=1;
int rx=0;
char inSerial[15];

void setup(){
Serial.begin(9600);
pinMode(ledred, OUTPUT);
pinMode(ledgreen, OUTPUT);
pinMode(ledblue, OUTPUT);
pinMode(tx, OUTPUT);
pinMode(rx, INPUT);
allpinslow();
}

void loop(){
int i=0;
int m=0;
delay(500);
if (Serial.available() > 0) {
while (Serial.available() > 0) {
inSerial[i]=Serial.read();
i++;
}
inSerial[i]=’\0′;
Check_Protocol(inSerial);
}}

void allpinslow()
{
digitalWrite(ledred, HIGH);
digitalWrite(ledgreen, HIGH);
digitalWrite(ledblue, HIGH);
}

void Check_Protocol(char inStr[]){
int i=0;
int m=0;
Serial.println(inStr);

if(!strcmp(inStr,”red”)){      //Ledred ON
allpinslow();
digitalWrite(ledred, LOW);
Serial.println(“Red ON”);
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}

if(!strcmp(inStr,”green”)){      //Ledgreen ON
allpinslow();
digitalWrite(ledgreen, LOW);
Serial.println(“Green ON”);
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}

if(!strcmp(inStr,”blue”)){      //Ledblue ON
allpinslow();
digitalWrite(ledblue, LOW);
Serial.println(“Blue ON”);
for(m=0;m<11;m++){
inStr[m]=0;}
i=0;}

else{
for(m=0;m<11;m++){
inStr[m]=0;
}
i=0;

}}
Don’t forget the resistors coming out of the cathode (-‘ve terminals on the LED) !!. You can see the “ohms” symbol used to donate a resistor. Usually a 330 ohm resistor is used. Sometimes it’s necessary to change the resistance values slightly! on some of the cathodes to get consistency in each colour intensity, or you can just cope with whatever a standard 330 ohm resistor produces – which is usually good enough.

I told you what the passkey is for our bluetooth modules in class.

Advertisements

2 thoughts on “Setting up a HC-05 Bluetooth module with Arduino to control an RGB LED

  1. “Stonez” (Kevin Chen) has a great app for this. I recommend you check it out
    http://stonez56.blogspot.tw/2014/09/arduino-android-led.html
    GreatScott and Stonez…. you guys rock!

    Just in case Stonez’s site goes down, I’ve taken the liberty of pasting a copy of his hide here. Just copy and paste it into your Arduino IDE. Please do not however, Stonez does not use the designated RX and TX pins on his arduino, but tell the program to use Pin 11 and 12 instead. You really should ensure that your TX line from the arduino is about 3.3V and not 5V (as would be the case if you hooked the TX wire from an arduino Uno to the RX of the bluetooth. The bluetooth module will get fried. You have have to use a voltage regulator or a voltage divider circuit to ensure 3.3V. I use a voltage divider in the sketch.

    #include
    #include //Include libraries: SoftwareSerial & Wire
    SoftwareSerial BT(11,12);
    //Define PIN11 & PIN12 as RX and TX pins

    //RGB LED Pins
    int

    PIN_RED = 3;
    int

    PIN_GREEN = 5;
    int

    PIN_BLUE = 6;
    //RED LED at Pin 13
    int

    RED_LED = 13;
    String RGB =
    “”
    ;
    //store RGB code from BT
    String RGB_Previous =
    “255.255.255)”
    ;
    //preserve previous RGB color for LED switch on/off, default White
    String ON =
    “ON”
    ;
    //Check if ON command is received
    String OFF =
    “OFF”
    ;
    //Check if OFF command is received
    boolean RGB_Completed =
    false
    ;

    void

    setup() {

    Serial.begin(9600);
    //Arduino serial port baud rate:9600

    BT.begin(9600);
    //My HC-05 module default baud rate is 9600

    RGB.reserve(30);

    pinMode(RED_LED, OUTPUT);

    //Set pin13 as output for LED,

    // this LED is on Arduino mini pro, not the RGB LED
    }

    void

    loop() {

    // put your main code here, to run repeatedly:

    //Read each character from Serial Port(Bluetooth)

    while
    (BT.available()){

    char

    ReadChar = (
    char
    )BT.read();

    // Right parentheses ) indicates complet of the string

    if
    (ReadChar ==
    ‘)’
    ){

    RGB_Completed =
    true
    ;

    }
    else
    {

    RGB += ReadChar;

    }

    }

    //When a command code is received completely with ‘)’ ending character

    if
    (RGB_Completed){

    //Print out debug info at Serial output window

    Serial.print(
    “RGB:”
    );

    Serial.print(RGB);

    Serial.print(
    ” PreRGB:”
    );

    Serial.println(RGB_Previous);

    if
    (RGB==ON){

    digitalWrite(13,HIGH);

    RGB = RGB_Previous;
    //We only receive ‘ON’, so get previous RGB color back to turn LED on

    Light_RGB_LED();

    }
    else

    if
    (RGB==OFF){

    digitalWrite(13,LOW);

    RGB =
    “0.0.0)”
    ;
    //Send OFF string to turn light off

    Light_RGB_LED();

    }
    else
    {

    //Turn the color according the color code from Bluetooth Serial Port

    Light_RGB_LED();

    RGB_Previous = RGB;

    }

    //Reset RGB String

    RGB =
    “”
    ;

    RGB_Completed =
    false
    ;

    }
    //end if of check if RGB completed

    }
    // end of loop

    void

    Light_RGB_LED(){

    int

    SP1 = RGB.indexOf(
    ‘.’
    );

    int

    SP2 = RGB.indexOf(
    ‘.’
    , SP1+1);

    int

    SP3 = RGB.indexOf(
    ‘.’
    , SP2+1);

    String R = RGB.substring(0, SP1);

    String G = RGB.substring(SP1+1, SP2);

    String B = RGB.substring(SP2+1, SP3);

    //Print out debug info at Serial output window

    Serial.print(
    “R=”
    );

    Serial.println( constrain(R.toInt(),0,255));

    Serial.print(
    “G=”
    );

    Serial.println(constrain(G.toInt(),0,255));

    Serial.print(
    “B=”
    );

    Serial.println( constrain(B.toInt(),0,255));

    //Light up the LED with color code

    //**2014-09-21
    //Because these RGB LED are common anode (Common positive)
    //So we need to take 255 to minus R,G,B value to get correct RGB color code

    analogWrite(PIN_RED, (255-R.toInt()));

    analogWrite(PIN_GREEN, (255-G.toInt()));

    analogWrite(PIN_BLUE, (255-B.toInt()));

    }

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s