Introduction: Connecting HC-05 Bluetooth Module to Arduino
The HC-05 Bluetooth module is an excellent interface for communicating with your mobile. It has a great data transfer rate and very easy to implement.
In order to try this tutorial out you will need the following:
- Any Arduino board you like and if you don’t already have one check this out
- Grab a breadboard similar to any one of these
- Get yourself one of the HC-05 Bluetooth Modules, if you don’t already have one then grab one from here
- 2 Resistors, 2K Ohm and 4.6K Ohm
- Finally grab some Dupont jumper wires or use normal wires if you are comfortable with that
It is worth noting that the HC-05 power in (Vcc) uses 5V, while the transmit and receive (TXD and RXD) logic signal uses 3.3V. Accordingly sending signals from the HC-05 module to Arduino is ok as the Arduino I/O pins can safely receive up to 5V but the issue is when Arduino tries to send the data to the HC-05 with signal level 5V, in this case it is required to use a voltage divider as we will see during the tutorial.
Also, it worth noting that the above links are for those of you guys living in Egypt, still, for those of you living abroad and want to get the components you can either drop us an e-mail to see if we can ship the components out to you or try Amazon.com:
- For Arduino OSOYOO Mini USB Nano V3.0 ATMEGA328P Module CH340G 5V 16M Micro-controller board for Arduino
- For Breadboard Solderless Plug-in BreadBoard, 830 tie-points, 2 Power lanes, 200PTS, 16.5 x 5.4 x 0.85 cm
- For HC-05 JY-MCU Arduino Bluetooth Wireless Serial Port Module
- For Dupont Gikfun 3 x 40P 20cm Dupont Wire Jumper Cable 2.54 1P-1P Male-Male/Female-Female/Female-Male EK8403
Step 1: Check Breadboard Power Rail Consistent Connectivity
Get 2 dupont jumper cables, connect 1 at the rail start and one at the rail end. Now grab your voltmeter and set it on Ohm or connectivity measuring mode then connect the tips to the end of the jumper cables. Make sure that the rail is consistent.
Step 2: If Your Breadboard's Power Rail Connectivity Is Continuous You Can Skip This Step
If the power rail connectivity is not consistent then consider using jumper wires to fix that.
Step 3: Arduino Nano Connection
Connect the Arduino Nano and connect its 5V out and GND to power rails as depicted in the image above.
Step 4: Connect the HC-05 Bluetooth Module
Now let’s connect the HC-05 and its power sockets. Vcc to the Arduino 5V and GND to Arduino GND.
Step 5: Connect the Bluetooth Module TxD
Next we will connect the HC-05 TXD (this is the pin where all data received from the mobile will be transmitted via it to Arduino) to Arduino pin D3.
Step 6: Connect the Bluetooth RxD Module Using the Voltage Divider
Now we will have to connect the HC-05 RXD. First we need to establish a voltage divider using the 2K and the 4.6K ohm resistors. First connect one end of the 2K resistor to Arduino D2 pin, the connect the other end to any unused line in the breadboard, then into this line connect the 4.6K resistor and the other end to the GND. See the image above.
Connect a dupont jumper originating from the centre point between the 2 resistor and connect it to the HC-05 RXD.
Step 7: Create the Arduino Sketch
You are all done now with the hardware work. Let’s move to programming. Grab you USB cable and connect the Arduino to your laptop and use the code below:
#include <SoftwareSerial.h>
// Define the data transmit/receive pins in Arduino
#define TxD 2
#define RxD 3
SoftwareSerial mySerial(RxD, TxD); // RX, TX for Bluetooth
void setup() {
mySerial.begin(9600); // For Bluetooth
Serial.begin(9600); // For the IDE monitor Tools -> Serial Monitor
// Any code that you want to run once....
}
void loop() {
// put your main code here, to run repeatedly:
boolean isValidInput; do { byte c; // get the next character from the bluetooth serial port
while ( !mySerial.available() ) ; // LOOP...
c = mySerial.read(); // Execute the option based on the character recieved
Serial.print(c); // Print the character received to the IDE serial monitor
switch ( c ) {
case 'a': // You've entered a
// Do the code you need when 'a' is received.....
mySerial.println( "You've entered an 'a'" );
isValidInput = true;
break;
case 'b': // You've entered b
// Do the code you need when 'a' is received.....
mySerial.println( "You've entered an 'b'" );
isValidInput = true;
break;
default:
// Do the code you need when any other character is received.....
mySerial.println( "Please enter 'a' or 'b'" );
isValidInput = false;
break;
}
} while ( isValidInput == true ); // Repeat the loop
}
Step 8: Congrats, You Are All Done :)
Oh, forgot to tell you, download and use BT-Term (S2 Terminal for Bluetooth) downloadable from the Google PlayStore to send characters to your module
Hope this tutorial helps!
9 Comments
Question 3 years ago on Step 3
how can i add hc 06 file in fritzing
Question 5 years ago
thank you for your help,
but i have a doubt, cant we use the RX and TX of the nano to communicate with bluetooth module without the voltage divider.
6 years ago
what if i used a different resisters insted of 2k and 4.6
6 years ago
How to connect the Bluetooth module RX terminal with aurduino nano without voltage divider
6 years ago
I bought an SPP-C (HC-05 compatible) with no idea how to use it, but you came through for me, man. Thanks!
Let
me mention, though, that #define's don't need semicolons - you have
them in your listing, and that may mess with some newbies out there.
Thanks, again!
Reply 6 years ago
Thank you very much for your nice and pleasant words, as well as thank you for highlighting the ";" thing :), I've amended it and also for some strange reason the #include <SoftwareSerial.h> was missing the "<....>" part. Fixed too.
Thanks pal and feel free to shout on me if I can be of any further help.
7 years ago
Hello to everyone.I have make a bleutooth robot which i can control it via android(tablet) and i have a problem with the code.I want from the robot to stop when connection lost or bluetooth disconnected.Now when the connection lost the robot go ahead and falls on the objects.How to add this line on the code?Can you help me please?The STATE pin of HC-05 when is HIGH is connected and when is LOW is disconnected.I put this on my code but it doesn't stop.Thank you for your time
The bluetooth that i use is HC-05
My code:
#include <Servo.h>
Servo SERVO_1; // Initialize Servo1
// Motor Control Variables
int PWM1 = 9;
int ENABLE1 = 8;
int PWM2 = 5;
int ENABLE2 = 7;
int PWM3 = 3;
int ENABLE3 = 4;
int PWM4 = 6;
int ENABLE4 = 12;
int STATE=2;
void setup() {
SERVO_1.attach(10);
Serial.begin(9600);
pinMode(ENABLE1, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
pinMode(ENABLE2, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
pinMode(ENABLE3, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
pinMode(ENABLE4, OUTPUT); //Δήλωση όλων των μεταβλητών ως έξοδος
pinMode(STATE, INPUT);
}
void loop() {
if(digitalRead(STATE)== HIGH)
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
int incomingByte = Serial.read();
// action depending on the instruction
// as well as sending a confirmation back to the app
switch (incomingByte) {
case 'F':
moveForward();
Serial.println("Going forward");
break;
case 'L' : // Case 'L' is received,
SERVO_1.write (180); // Στρίψε Αριστερά.
SERVO_1.attach(10);
break;
case 'N':
turnright();
Serial.println("Turning right");
break;
case 'M':
turnleft();
Serial.println("Turning left");
break;
case 'O' : // Case 'L' is received,
SERVO_1.write (0); // Στρίψε Αριστερά.
SERVO_1.attach(10);
break;
case 'B':
moveBackward();
Serial.println("Going forward");
break;
case 'P':
SERVO_1.write(90); // Στρίψε Αριστερά.
SERVO_1.attach(10);
break;
case 'S':
moveNone();
Serial.println("Stopping");
break;
default:
// if nothing matches, do nothing
break;
}
}
}
void moveForward() {
// turn the driving motor on to go forwards at set speed
digitalWrite(ENABLE1, HIGH);
digitalWrite(ENABLE2, HIGH);
digitalWrite(ENABLE3, HIGH);
digitalWrite(ENABLE4, HIGH);
analogWrite(PWM1, 255);
analogWrite(PWM2, 255);
analogWrite(PWM3, 255);
analogWrite(PWM4, 255);
}
void moveBackward() {
// turn the driving motor on to go backwards at set speed
digitalWrite(ENABLE1, LOW);
digitalWrite(ENABLE2, LOW);
digitalWrite(ENABLE3, LOW);
digitalWrite(ENABLE4, LOW);
analogWrite(PWM1, 255);
analogWrite(PWM2, 255);
analogWrite(PWM3, 255);
analogWrite(PWM4, 255);
}
void turnright() {
digitalWrite(ENABLE1, HIGH);
digitalWrite(ENABLE2, HIGH);
digitalWrite(ENABLE3, LOW);
digitalWrite(ENABLE4, LOW);
analogWrite(PWM1, 255);
analogWrite(PWM2, 255);
analogWrite(PWM3, 255);
analogWrite(PWM4, 255);
}
void turnleft() {
digitalWrite(ENABLE1, LOW);
digitalWrite(ENABLE2, LOW);
digitalWrite(ENABLE3, HIGH);
digitalWrite(ENABLE4, HIGH);
analogWrite(PWM1, 255);
analogWrite(PWM2, 255);
analogWrite(PWM3, 255);
analogWrite(PWM4, 255);
}
void moveNone() {
// turn the driving motor off
digitalWrite(ENABLE1, 0);
digitalWrite(ENABLE2, 0);
digitalWrite(ENABLE3, 0);
digitalWrite(ENABLE4, 0);
analogWrite(PWM1, 0);
analogWrite(PWM2, 0);
analogWrite(PWM3, 0);
analogWrite(PWM4, 0);
SERVO_1.detach();
}
7 years ago
Cool project!
Reply 7 years ago
Thanks bro!