arduino bluetooth wireless

Arduino Bluetooth Tutorial – Sending Wireless Data to control RGB LED

Arduino Bluetooth Wireless Control

Wireless technology is quickly growing and expanding in all applications. In the recent years we saw the appearance of wireless coffee mugs, locks and other gadgets. Getting connected to your Arduino is a great way to learn what it really takes to get into the wireless market and expand your capabilities. Although there are various ways through which you can achieve wireless connectivity, Bluetooth is one of the cheapest, most reliable and simplest ones. With an inexpensive module and a phone application, you can get up and running within minutes. In this tutorial we will take a look at what it takes to get connected the Bluetooth module in order to change the brightness of an RGB LED and toggle the Output pin 13.

Required Hardware

For this tutorial, you will require the following:

  • Arduino
  • Breadboard
  • Push Buttons
  • bluetooth Module – HC-06 or HC-05
  • RGB LED
  • Jumper Wires

Hardware Setup

The Bluetooth Module communicates with the serial port of the Arduino. This means that it will need to be placed at the TX and RX pins. Make sure to connect the TX of the Arduino to the RX of the Bluetooth Module. Connect the RX of the Arduino to the TX of the Bluetooth. This is done in order to make sure that the transmitter one one side is sending data to the receiver on the other.
Arduino Bluetooth Tutorial

The RGB LED can be connected to any Analog pins. This tutorial is meant to be a basis for Bluetooth and the applications for which you use the pins are not extremely relevant.

Arduino Bluetooth Implementation

You will need to implement the code from the TechBitar website. You can find the full descriptions by clicking the link below. Otherwise, copy the code provided here.
TechBitar Code: Click Here


/*
PROJECT: ArduDroid
PROGRAMMER: Hazim Bitar (techbitar at gmail dot com)
DATE: Oct 31, 2013
FILE: ardudroid.ino
LICENSE: Public domain
*/
#define START_CMD_CHAR '*'
#define END_CMD_CHAR '#'
#define DIV_CMD_CHAR '|'
#define CMD_DIGITALWRITE 10
#define CMD_ANALOGWRITE 11
#define CMD_TEXT 12
#define CMD_READ_ARDUDROID 13
#define MAX_COMMAND 20 // max command number code. used for error checking.
#define MIN_COMMAND 10 // minimum command number code. used for error checking.
#define IN_STRING_LENGHT 40
#define MAX_ANALOGWRITE 255
#define PIN_HIGH 3
#define PIN_LOW 2
String inText;
void setup() {
Serial.begin(9600);
Serial.println("ArduDroid 0.12 Alpha by TechBitar (2013)");
Serial.flush();
}
void loop()
{
Serial.flush();
int ard_command = 0;
int pin_num = 0;
int pin_value = 0;
char get_char = ' '; //read serial
// wait for incoming data
if (Serial.available() < 1) return; // if serial empty, return to loop(). // parse incoming command start flag get_char = Serial.read(); if (get_char != START_CMD_CHAR) return; // if no command start flag, return to loop(). // parse incoming command type ard_command = Serial.parseInt(); // read the command // parse incoming pin# and value pin_num = Serial.parseInt(); // read the pin pin_value = Serial.parseInt(); // read the value // 1) GET TEXT COMMAND FROM ARDUDROID if (ard_command == CMD_TEXT){ inText =""; //clears variable for new input while (Serial.available()) { char c = Serial.read(); //gets one byte from serial buffer delay(5); if (c == END_CMD_CHAR) { // if we the complete string has been read // add your code here break; } else { if (c != DIV_CMD_CHAR) { inText += c; delay(5); } } } } // 2) GET digitalWrite DATA FROM ARDUDROID if (ard_command == CMD_DIGITALWRITE){ if (pin_value == PIN_LOW) pin_value = LOW; else if (pin_value == PIN_HIGH) pin_value = HIGH; else return; // error in pin value. return. set_digitalwrite( pin_num, pin_value); // Uncomment this function if you wish to use return; // return from start of loop() } // 3) GET analogWrite DATA FROM ARDUDROID if (ard_command == CMD_ANALOGWRITE) { analogWrite( pin_num, pin_value ); // add your code here return; // Done. return to loop(); } // 4) SEND DATA TO ARDUDROID if (ard_command == CMD_READ_ARDUDROID) { // char send_to_android[] = "Place your text here." ; // Serial.println(send_to_android); // Example: Sending text Serial.print(" Analog 0 = "); Serial.println(analogRead(A0)); // Example: Read and send Analog pin value to Arduino return; // Done. return to loop(); } } // 2a) select the requested pin# for DigitalWrite action void set_digitalwrite(int pin_num, int pin_value) { switch (pin_num) { case 13: pinMode(13, OUTPUT); digitalWrite(13, pin_value); // add your code here break; case 12: pinMode(12, OUTPUT); digitalWrite(12, pin_value); // add your code here break; case 11: pinMode(11, OUTPUT); digitalWrite(11, pin_value); // add your code here break; case 10: pinMode(10, OUTPUT); digitalWrite(10, pin_value); // add your code here break; case 9: pinMode(9, OUTPUT); digitalWrite(9, pin_value); // add your code here break; case 8: pinMode(8, OUTPUT); digitalWrite(8, pin_value); // add your code here break; case 7: pinMode(7, OUTPUT); digitalWrite(7, pin_value); // add your code here break; case 6: pinMode(6, OUTPUT); digitalWrite(6, pin_value); // add your code here break; case 5: pinMode(5, OUTPUT); digitalWrite(5, pin_value); // add your code here break; case 4: pinMode(4, OUTPUT); digitalWrite(4, pin_value); // add your code here break; case 3: pinMode(3, OUTPUT); digitalWrite(3, pin_value); // add your code here break; case 2: pinMode(2, OUTPUT); digitalWrite(2, pin_value); // add your code here break; // default: // if nothing else matches, do the default // default is optional } }

Installing & Running the Application

TechBitar has developed an application for an Android phone. This app allows the user to connect to the Arduino Bluetooth capabilities and play with the pins. Click the link below in order to download the app from the Google Store. Once you have installed the app, launched the main screen, you will be able to connect to the module. Pay attention to the password which will come up the first time you connect; it will be 1234 OR 0000 depending on your module.
Android App: Click Here

Thank you all for reading/watching. If you have any additional questions, please write them down on the forums: EEEnthusiast Forums
- EEEnthusiast

Leave a Reply

Your email address will not be published.

CommentLuv badge