I always ask myself, can I create something with the HC_05 Bluetooth module? So I did a little research and guess what--it wasn't that hard do it. So I have also taken it upon myself to show you guys how you can also have fun with this interesting module.
This tutorial is to help you with the basic connections of the HC-05 Bluetooth module. The tutorial is in two parts. The first Section is to help you setup the basic parameters of your Bluetooth module, such as the password and MAC-Address.
The second section is to help you use the Ardudroid App to connect to the Bluetooth module, as well as use it to control LEDs and a buzzer.
Enjoy!!!!!!!
Components Needed for This ProjectHardware Components
- Arduino Uno : https://www.sparkfun.com/products/11021
- Ultrasonic Sensor (4 pinout) : http://letsmakerobots.com/node/30209
- Jumper wires : https://www.sparkfun.com/products/11026
- Breadboard : https://www.sparkfun.com/products/12615
- HC-05 Bluetooth module : http://www.emartee.com/product/41656/
- Piezoelectric Buzzer; https://www.adafruit.com/product/160
- Resistors. (Kindly note that the Resistor are connected in series and also their values are 2Kohm and a 1kohm)
Software
- ArduDroid App by TechBitar : https://play.google.com/store/apps/details?id=com....
Attached are the Breadboard view and the Circuit diagram of the Project.
Arduino CodeKindly note that, this is the original code of the ArduDroid App for the Arduino version, but I did a little edit to suite this tutorial. Also you will note that with most of the code I have commented out, it is because I do not need it. You can also go ahead and make any changes you need. I hope this helps.
/*
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
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
#define BUZZER_PIN 9
#define red_LED 4
#define green_LED 5
#define blue_LED 6
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
delay(500); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping:");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if (uS / US_ROUNDTRIP_CM <= 10)
{
Serial.print("OBSTRUCTION");
return; // Done. return to loop();
}
if (uS / US_ROUNDTRIP_CM > 10)
{
Serial.print("NO OBSTRUCTION");
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
}
}
Video of the ProjectEnjoy the video, it is more in-depth.
Pictures of the Project
Comments
Please log in or sign up to comment.