logologologologo
  • home
  • learn to code
    • Space Invaders – Beginner’s Course
    • Asteroids – Beginner’s Level 2
    • Snake – a Beginner’s Challenge
  • learn to make
    • Arduino Tutorials
  • blog
    • Computer Science
    • Coding
      • Collision Detection
    • Retro Gaming
    • Computer Games for Vision Therapy
  • contact me
Add Bluetooth to your Arduino project
Adding Bluetooth to Your Arduino Projects
30th January 2021
Setup VICE Commodore 64 emulator
Turn Your PC into a Commodore 64 and Play Every Game For Free
14th February 2021
Published by Bob at 4th February 2021
Categories
  • Arduino
  • Learn to Make
Tags
  • arduino
  • lcd touchscreen
AT commands to set up your bluetooth module

Adding Bluetooth to Your Arduino Projects

Bluetooth modules such as the HC-05 and HC-06 provide an easy way to add Bluetooth connectivity to your Arduino project. These modules arrive set up and ready to go with default settings for their connection speed to your Arduino, Bluetooth broadcast name, and Bluetooth pin number. You can use them as they are but is much better to customise the module’s settings to customise and secure your project.

I’ve covered how to connect the module to your Arduino in a previous video so please have a look here to get started.

Arduino connected to HC06

Once you’ve got the Bluetooth module connected to your Arduino we can use the Arduino as a serial terminal to talk directly to the Bluetooth module.

Using Your Arduino As a Serial Terminal

To use your Arduino as a serial terminal only requires a very simple sketch to be uploaded into it.

Code for Bluetooth Terminal Test

Download Sketch

Lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Arduino.h"
#include <SoftwareSerial.h>
 
const byte rxPin = 9;
const byte txPin = 8;
SoftwareSerial BTSerial(rxPin, txPin); // RX TX
 
void setup() {
// define pin modes for tx, rx:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
BTSerial.begin(9600);
Serial.begin(9600);
}
 
String messageBuffer = "";
String message = "";
char dataByte;
 
void loop() {
 
while(BTSerial.available()) {
dataByte = BTSerial.read();
Serial.write(dataByte);
}
 
while(Serial.available()){
dataByte = Serial.read();
BTSerial.write(dataByte);
}
 
}

Where to Get Your Bluetooth Modules

Amazon:

HC-05 Bluetooth module : https://amzn.to/3oyCqd5
HC-06 (slave only) : https://amzn.to/3oyCqd5

eBay:

HC-05 Bluetooth module : http://ebay.us/qODpaV
HC-06 (slave only) : http://ebay.us/Opu3Zs

This sketch takes any data you send to it from the serial monitor inside the Arduino IDE and passes it straight out to the Bluetooth module. Any data coming back from the Bluetooth module is simply relayed back to the serial monitor. In this way, we can type commands into our serial monitor, have those executed on the Bluetooth module, and then see the results back on screen.

AT Commands for the Bluetooth Modules

These Bluetooth modules can be controlled through a series of AT commands. This technique is common to a large number of serial devices. Before the module will respond these commands it needs to be put in to AT mode. The HC-06 module is always in AT mode but the HC-05 modules need to be powered on with their small pushbutton held in.

Once the module is in AT mode we can start to send commands.

If you type AT into your serial terminal and press send you should see your Bluetooth module respond with an OK. If not make sure your serial terminal is connected to the same COM port as your Arduino and that the baud settings for both the Arduino to Bluetooth module and Arduino to PC connections are correct. If you haven’t yet changed any settings on the Bluetooth module it will be working at 9600 baud.

If you then type AT+VERSION and send that the Bluetooth module it should respond with the current version of the software it’s running.

What Settings Should You Change on Your Bluetooth Module?

The default serial speed is that the 9600 Baud. This is quite slow so it’s worth increasing that. The maximum you can set it to on an Arduino Uno is 115200. Don’t forget that once you change the baud rate you need to adjust the Arduino sketch and set the Arduino to Bluetooth module data rate to the correct value.

The Bluetooth modules broadcast name should be set the something sensible for your project. This is entirely up to you but it will help you identify your device when you try to pair with it.

Bluetooth devices use a four-digit pin as security when pairing. The modules use a default value of 1234 so you need to change this to make sure other people cannot pair with your Bluetooth module.

The AT commands we used to change the settings are different for each device so you’ll need to use the table below for your module.

HC-05 AT Commands

AT
Check connection

AT+VERSION?
Get software version

AT+RESET
Reset the Bluetooth module back to data mode. This does not reset the settings.

AT+UART=SPEED,STOP,PARITY
Set the baud rate and serial parameters between the Arduino and Bluetooth module. Replace SPEED with a value from below.

4800 = 4800 bps
9600 = 9600 bps
19200= 19200 bps
38400 = 38400 bps
57600 = 57600 bps
115200 = 115200 bps

STOP tells the module how many stop bits to send.

0 = 1 bit (Default)
1 = 2 bits

PARITY tells the module what parity to use.

0 = None (Default)
1 = Odd
2 = Even

So to set the baud rate to 115200 baud you would send the command,

AT+UART=115200,0,0

AT+NAME=XXXXXX
Set the Bluetooth broadcast name for the module. Replace the Xs with the name you want to use. Make sure you don’t put any spaces between the AT+NAME, =,  and your desired broadcast name. For example, to set the broadcast name to My Project you would send the command,

AT+NAME=My Project

AT+PSWD=XXXX
Set the Bluetooth pairing password to a four-digit PIN. Replace the Xs with the four-digit pin you want to use. For example, AT+PSWD=4321

HC-06 AT Commands

AT
Check connection

AT+VERSION
Get software version

AT+BAUDX
Set the baud rate between the Arduino and Bluetooth module. Replace the X with a value from below.

1 = 1200 bps
2 = 2400 bps
3 = 4800 bps
4 = 9600 bps
5 = 19200 bps
6 = 38400 bps
7 = 57600 bps
8 = 115200 bps

So to set the baud rate to 115200 baud you would send the command,

AT+BAUD8

AT+NAMEXXXXXX
Set the Bluetooth broadcast name for the module. Replace the Xs with the name you want to use. Make sure you don’t put a space between the AT+NAME and your desired broadcast name. For example, to set the broadcast name to My Project you would send the command,

AT+NAMEMy Project

AT+PINXXXX
Set the Bluetooth pairing password to a four-digit PIN. Replace the Xs with the four-digit pin you want to use. For example, AT+PIN4321

Links

Amazon:

HC-05 Bluetooth module : https://amzn.to/3oyCqd5
HC-06 (slave only) : https://amzn.to/3oyCqd5

eBay:

HC-05 Bluetooth module : http://ebay.us/qODpaV
HC-06 (slave only) : http://ebay.us/Opu3Zs

Share
2
Bob
Bob

Related posts

Fix Your SD Card After Using It As a Raspberry Pi or Linux Boot Disk
25th February 2021

Fix Your SD Card After Using It As a Raspberry Pi or Linux Boot Disk


Read more
Add Bluetooth to your Arduino project
30th January 2021

Adding Bluetooth to Your Arduino Projects


Read more
Games coding on ILI9341 LCD touchscreen
3rd January 2021

Coding Games on an ILI9341 SPI LCD Touchscreen with Arduino


Read more

Comments are closed.

  • Privacy Policy
  • Contact Me
© 2020 Bytes N Bits
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok