A Simple Way to Make Your Project Wireless with Serial Bluetooth Modules
Using serial Bluetooth modules is a straightforward way to make your project wireless, as they can transparently transmit standard serial data to another connected Bluetooth module, without requiring additional code.
Adding Bluetooth to Your Project for Mobile Control
To add Bluetooth to your project for mobile control, simply insert a slave module and connect to it via your phone. However, getting two modules to communicate with each other requires understanding some basic concepts.
How Bluetooth Modules Work
Bluetooth modules work in slave and master modes. Slave modules cannot initiate connections with other Bluetooth devices, but can accept connections. Master devices can be set to master or slave mode, and in master mode, they can initiate connections with other devices. Be sure not to make the mistake of buying two slave devices! You need two master devices or one master and one slave device.
What to Note When Buying Bluetooth Modules
Sometimes, eBay sellers may not provide much information about their modules. To determine whether a module is a slave or master device, be sure to check the following:
Slave | Master | |
---|---|---|
Number of pins | 4 | 6 |
Firmware | Linvor 1.5 or HC06 | HC05 |
The master modules (~£8) are usually a little bit more expensive than the slave modules (~£6).
The modules contain 2 parts, the back plane and the main Bluetooth board. The main Bluetooth part of the module is designed for 3.3V, the back plane board has a 3.3V regulator built in so supplying 5V for power is fine, but there is no level conversion for the serial data, you probably don’t need to level convert the output (TXD) data since most 5V controllers see 3.3V as high, but the serial input (RXD) does need conversion, a voltage divider or resistor and zener diode will do the job.
Divider | Zener |
---|---|
To configure Bluetooth modules, you’ll need a USB to serial converter. You can search for CP2102 on eBay or DealExtreme and find them at a price of less than 2 pounds. I recommend buying 2 converters, so you can connect both the slave device and the master device to your computer at the same time. Make sure at least one converter has 3.3V and 5V output.
Connect the serial converter and Bluetooth modules like so:
Module | Converter |
---|---|
VCC | 5V |
GND | GND |
TXD | RXD |
RXD | TXD |
Configuring
The serial converters usually use 3.3V signaling for serial, so no level conversion needed.
You will need a serial console program, I use RealTerm.
Bluetooth modules can be configured when no other Bluetooth devices are connected to them. By default, the module’s baud rate is 9600, with no parity, 8 data bits, and 1 stop bit.
Open the serial console and type AT, and the module should respond with OK. Note that the timing of sending characters is important. If you input too slowly, the module may not recognize the command and treat it as two separate commands, A and T. Therefore, it’s recommended to copy and paste the characters.
To check the firmware version, copy and paste AT+VERSION, and the module should respond with OKLinvor1.5.
Set PIN: AT+PIN1234
There are only a few available commands for slave modules –
Command | Info |
---|---|
AT+BAUD<setting> | Set baud rate 1 – 1200 2 – 2400 3 – 4800 4 – 9600 5 – 19200 6 – 38400 7 – 57600 8 – 115200 9 – 230400 A – 460800 B – 921600 C – 1382400 |
AT+NAME<name> | Name – Max 20 characters |
AT+PIN<number> | PIN – 4 digit number |
Master (and master as a slave)
Configuring the master is a little different from the slave, there are 2 ways to put the module into AT mode:
Method 1
- Turn off module
- Connect the key pin on the module to 3.3V on the serial converter
- Turn on module
The baud rate will always be 38400 with this method, the module must also be power cycled to get back into normal mode.
Method 2
- Turn on module
- Connect the key pin on the module to 3.3V on the serial converter
The baud rate will be whatever it’s configured to, default 9600, the module can be put in and out of AT mode without being power cycled.
Quick Check
The timing of commands doesn’t matter with the master modules, instead you need to end each command by pressing the enter key (\r\n), sometimes the response will be continually repeated, pressing enter again will stop it.
Entering AT
should return OK
, entering AT+VERSION?
should return something similar to +VERSION:2.0-20100601
Master as a slave
If you’ve got 2 masters you will need to have one operating as a slave which isn’t too hard to do:
Reset to defaults:AT+ORGL
Clear any paired devices:AT+RMAAD
Set PIN:AT+PSWD=1234
Slave mode:AT+ROLE=0
Get the address of the device:AT+ADDR?
Now that you have the address you can skip the AT+INQ
and AT+RNAME?
parts for configuring the master.
Turn off the module, disconnect key from 3.3V and turn back on.
Master as a master
Now, to get the master to connect to the slave module you will need to find the address of the slave. Make sure the slave module is powered up and enter these commands into the master module:
Reset to defaults:AT+ORGL
Clear any paired devices:AT+RMAAD
Master mode:AT+ROLE=1
Need to restart after changing role:AT+RESET
Allow connecting to any address:AT+CMODE=1
Inquire mode – Standard, stop after 5 devices found, (or) stop searching after 5 seconds:AT+INQM=0,5,5
Set PIN, should be the same as the slave:AT+PSWD=1234
Start SPP profile lib (needed to do any Bluetooth transmitting/receiving):AT+INIT
Note: If it says Error:(17)
then that means it’s already loaded, you can carry on.
Start searching:AT+INQ
A short list of devices found should appear, one of them will be the slave module.
The format of the output is as follows:+INQ:address,type,signal
Type can be ignored. The signal will be 7FFF since inquire is in standard mode, in RSSI mode you’ll get a load of the same device but with different signal levels which you don’t want at the moment.
Copy the address part of the devices found, for example 12:3:289431
and change the colons to commas – 12,3,289431
.
To find out which device is the slave module you will need to query each address for its name:AT+RNAME? <address>
Replace <address>
with address of device like 12,3,289431
Response should be +RNAME:linvor
if you’re using a normal slave module or +RNAME:HC05
if you’re using a master module as a slave.
Now you’ve got the address of the slave module you need to pair with it:AT+PAIR=<address>,<timeout>
Note: If you are going to make the master connect to your phone then you need to set the timeout long enough for you to type in the PIN.
Bind address:AT+BIND=<address>
Only allow connecting to the bound address:AT+CMODE=0
And now to actually connect:AT+LINK=<address>
If the configuration is successful, the slave LED should be steadily lit, and the master LED should have a heartbeat flashing mode.
The modules are now fully configured! They should automatically connect to each other as much as possible.
You can find the complete command list for the master module in the download below.
Now, if you want to enter the AT mode on the master module, you need to connect the key to 3.3V before opening the module, then close the module, disconnect the key, and reopen it.
If you connected the key while the module was open, then you only need to disconnect the key.
To test the communication between the modules, you can open 2 serial consoles, one opening the COM port to the slave device, and the other opening the COM port to the master device. If you input in one console, it should appear in the other console. If this works, you can simply plug them into your project!
Extra stuff
These Bluetooth modules’ boards also have SPI, USB, and PCM audio connections, although it’s unclear whether they can be used.
Additionally, it appears that the firmware on the module can be upgraded, allowing the master firmware to be put on the slave module. You can refer to Byron76’s blog post to learn how to upgrade the firmware: http://byron76.blogspot.co.uk/2011/09/i-hope-you-have-good-time-soldering.html
If you want to learn more about various Bluetooth modules, you can check out the relevant page on Pinguino wiki: http://wiki.pinguino.cc/index.php/SPP_Bluetooth_Modules
Downloads
LATEST | btinfo_20140510.zip (564.28 KB) Bunch of pdfs and stuff with Bluetooth commands and info MD5: BA64E38C473F7E2E21EC91A92996CFB5 |
OLD | btinfo_20130106.zip (243.41 KB) Bunch of pdfs and stuff with Bluetooth commands and info MD5: 9E79AD4A022DA1D4A0D694A2F6E35541 |