You are on page 1of 6

TUTORIAL# 23

Bluetooth Controlled RGB LED with Arduino

What will you make/learn?

You will learn how to program Arduino and blink a LED with it.

Difficulty level: Beginners

What you need for this? (Components + skill level)

Components: Arduino UNO, Arduino Cable and a RGB LED and a Bluetooth Module (HC-05).

How will you do it with these components?

LEDs are small-sized and highly used components in electronics. You may find them in many
things from your TVs to cars. In this tutorial we are going to blink a LED using Arduino UNO.

What is schematic?
Here LEDs long leg goes to ground and other pins are connected as shown in Breadboard circuit
diagram above.

What is the code?


Arduino code consists of two main functions one is setup and other is loop. Code inside setup function
executes once when Arduino runs and the code inside loop function keeps on repeating line by line.

Open Arduino IDE


Paste the following code in it.

#include <SoftwareSerial.h>

SoftwareSerial bluetooth(2, 3); // RX, TX

char command;

String inputCommand;

// the setup function runs once when you press reset or power the board

void setup() {

bluetooth.begin(9600);

Serial.begin(9600);

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

// the loop function runs over and over again forever

void loop() {

connectToBluetooth();

void connectToBluetooth(){

if (bluetooth.available() > 0)
{inputCommand = "";}

while(bluetooth.available() > 0)

command = ((byte)bluetooth.read());

inputCommand += command;

Serial.println(inputCommand);

if(inputCommand == "r")

ledTurnOn(11);

ledTurnOff(10);

ledTurnOff(9);

if(inputCommand =="g")

ledTurnOff(11);

ledTurnOn(10);

ledTurnOff(9);

if(inputCommand == "b"){

ledTurnOff(11);

ledTurnOff(10);

ledTurnOn(9);

}
if(inputCommand == "0")

ledTurnOff(11);

ledTurnOff(10);

ledTurnOff(9);

void ledTurnOn(int pin)

//analogWrite(pin, 255);

digitalWrite(pin,HIGH);

delay(100);

inputCommand = "";

void ledTurnOff(int pin)

//analogWrite(pin, 0);

digitalWrite(pin,LOW);

delay(100);

inputCommand = "";

Connect Arduino to computer via cable


Press the upload button and wait until Arduino shows done uploading message.

You might also like