How to use the HC-05 Bluetooth module with Arduino
Welcome! In this tutorial, we’ll learn how to use the HC-05 Bluetooth module with Arduino. By the end, you’ll know how to set up the module and make it work with your Arduino projects for wireless communication.
Specifications
- We use the STATE pin to check whether the Bluetooth module is connected or not.
- RX is the pin to receive the data.
- TX is the pin to transmit the data.
- GND and Vcc are used to power up the module with a voltage between 3.6V and 6V.
- EN allows us to toggle between the AT command mode and data mode.
Changing the name and the password of the module
AT command mode
The HC-05 Bluetooth Module operates in two modes: Data Mode, where it sends and receives serial data, and AT Command Mode, where you can change default settings like the module’s name and password.
To enter AT Command mode, follow these steps: First, upload an empty sketch to your Arduino board. Next, connect the Bluetooth module to the Arduino according to the schematic. Then, plug the Arduino into your computer while holding down the onboard button on the Bluetooth module. You should see an LED on the module blinking every 2 seconds, indicating that you are now in AT Command mode.
Once you’ve entered AT Command Mode, open the Arduino IDE and the serial monitor. Set the baud rate to 38400bps, which is the default speed for the HC-05 Bluetooth Module in AT Command Mode.
- To know the current name of the HC-05 Bluetooth Module, type in the top bar.
AT+NAME? and press enter (you will get the default name of the Bluetooth module followed by an “OK” message). - To change the name type
AT+NAME= and type the name that you like and press enter (you will get an “OK” message confirming that your name has been changed). To check back if the name was changed or not, type agin AT+NAME? and press enter. - To know the current password type
AT+PSWD? - To change the password type
AT+PSWD=”and type your desired password between two quotation marks.”
HC-05 AT Command Set
AT+NAME to know the name of the Bluetooth module.
AT+PSWD to know the password of the Bluetooth module.
AT+UART to know the baud speed of the Bluetooth Module in the Data Mode.
AT+ROLE to know the role of the Bluetooth module whether it is master or slave(master = 1, slave = 0).
AT+CMOD to know how many devices your Bluetooth module can pair with (connected to only one device = 0, connected to many devices = 1).
AT+ADDR to know the address of the Bluetooth module.
AT+RESET to rest the Bluetooth module
AT+ORGL to restore default settings of Bluetooth module
Control A Car Using A Smartphone
To control our robot car using a smartphone, we’ll be utilizing an Android application called Arduino-Bluetooth-RC-Car. This application features a control panel that lets us move the car forward and backward, turn left and right, adjust speed, and toggle the lights, horn, and emergency triangle. Each button press sends a specific character, which the Bluetooth module receives and the Arduino uses to control the car.
Here is the connections. Connect the components as shown in the diagram and use the Arduino sketch provided below.
Schematic
Sketch
int ENA=11;
int IN1=10;
int IN2=9;
int IN3=8;
int IN4=7;
int ENB=6;
int F=5;
int B=4;
int F2=3;
int B2=2;
char x;
int SPEED=0;
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(F, OUTPUT);
pinMode(B, OUTPUT);
pinMode(F2, OUTPUT);
pinMode(B2, OUTPUT);
Serial.begin(9600);
}
void backward()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void forward()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void right()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void left()
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void loop() {
if(Serial.available()>0){
x=Serial.read();
Serial.println(x);
}
if (x=='0')
{
SPEED=0;
} else if (x=='1')
{
SPEED=10;
} else if (x=='2')
{
SPEED=25;
} else if (x=='3')
{
SPEED=50;
} else if (x=='4')
{
SPEED=75;
} else if (x=='5')
{
SPEED=100;
} else if (x=='6')
{
SPEED=125;
} else if (x=='7')
{
SPEED=150;
} else if (x=='8')
{
SPEED=175;
} else if (x=='9')
{
SPEED=200;
} else if (x=='q')
{
SPEED=230;
} else if (x=='D')
{
SPEED=255;
}
if (x=='S')
{
digitalWrite(B, LOW);
digitalWrite(B2, LOW);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
forward();
}
if (x=='F')
{
analogWrite(ENA, SPEED);
analogWrite(ENB, SPEED);
forward();
}
if (x=='B')
{
digitalWrite(B, HIGH);
digitalWrite(B2, HIGH);
analogWrite(ENA, SPEED);
analogWrite(ENB, SPEED);
backward();
}
if (x=='R')
{
analogWrite(ENA, SPEED);
analogWrite(ENB, SPEED);
right();
}
if (x=='L')
{
analogWrite(ENA, SPEED);
analogWrite(ENB, SPEED);
left();
}
if (x=='W')
{
digitalWrite(F, HIGH);
digitalWrite(F2, HIGH);
}
if (x=='w')
{
digitalWrite(F, LOW);
digitalWrite(F2, LOW);
}
if (x=='U')
{
digitalWrite(B, HIGH);
digitalWrite(B2, HIGH);
}
if (x=='u')
{
digitalWrite(B, LOW);
digitalWrite(B2, LOW);
}
}
FAQ
You can connect the EN pin of the HC-05 Bluetooth Module with the 3.3v of the Arduino to enter the AT Command mode OR you can just click and hold the Button on board of the Module while plugging in the Arduino to your computer.
You will see a diode on the module blinking every 2 seconds which means you are successfully entered the AT Command Mode.
- Check out the baud rate, it should be at 38400 bps.
- If you could not enter the AT Command Mode then you should connect the RX pin of the Bluetooth Module to the RX pin of the Arduino through a voltage divider like it shown in the schematic down below.