HC-05 Serial Configuration Over Bluetooth
by pmcclay in Circuits > Arduino
4862 Views, 10 Favorites, 0 Comments
HC-05 Serial Configuration Over Bluetooth
While using Android devices and HC-05 Bluetooth SPP modules for some Arduino projects, I wanted to check and change HC-05 baud rates and other parameters without connecting to a PC USB port. That turned into this.
HC-05 modules connect serial and Bluetooth devices using the Bluetooth Serial Port Profile (SPP). Many in the Arduino community have described how to configure and use these modules, including several Instructables. Using an HC-05 often requires an initial hardwired serial connection. Most how-tos describe using some sort of USB-to-serial adapter, often an Arduino board, for access to the module's "command" mode from a PC. I'd rather have a widget that I can just stab an HC-05 into and carry on.
This 'ible includes:
- a sketch that will...
- immediately indicate the current serial bit rate (which may be all you needed)
- detect the current rate and begin serial communication
- collect user input over Bluetooth SPP
- signal to press HC-05 command button
- send commands to the module
- receive the HC-05's response, if needed
- signal to release the command button
- respond back to the user over the BT connection
- a ready-to-use appliance
- OPTIONAL for convenience
- very low cost
- very small
- ready to plug in HC-05 module
- powered by microUSB cable
- uses a Digispark-like board with ATtiny85 mcu
I've tested the sketch on an UNO clone and a Digispark clone. It may work on other Arduino-ish boards that implement a "Serial" class with RX on pin 0.
Supplies
- HC-05 module + carrier board (AliExpress)
- Arduino-ish mcu board (UNO clone tested AliExpress)
- hookup parts (like this from here)
- Sketch from here
Optional: parts to build a widget - listed later
Just Try It
Before thinking about what, if anything, to start soldering on: just try out the sketch.
I've been calling it baudat since it deals with baud and AT commands and to credit Mssr. Émile Baudot.
If you're reading this, you probably have something like an HC-05 and something like an "ordinary" Arduino-ish ATmega328 board like an UNO R3 or not too much different. The sketch is tested on an UNO R3 clone. I expect it will work on other boards that implement a Serial class and use pin 0 for serial RX. Or edit serialRxPin.
- Download the sketch, upload it to your board, and open a Serial Monitor window.
- You should be able to read the connected bit rate within the initial noise. Now you know the connection speed, but baudat doesn't.
- Type something at it. It should detect the bit rate and continue. A random smash on the keyboard will probably work. 'U' comes over the wire like x0101010101x which is unmistakable. Any two or more common printing characters (7 bit, not "extended") sent together (line-at-a-time vs character-at-a-time) should work.
- Try connecting the Monitor at different speeds. Common speeds supported by HC-05 modules should work. baudat doesn't recognize very slow, very fast, or "odd" speeds like 14,400 bps.
- Try out the interaction, and see what it would send to your HC-05 if it were connected.
Example: Prompted Basic Configuration
It should look something like this:
[[ example baudat session via Serial Monitor ]]
##noise##noise##noise##
This is 57600 bps. Type something. 'U'is robust.
##noise##noise##noise##
baudat HC-05 config tool
Set BT name, "polar" & serial bit rate? [y/n] Y
Set Bluetooth device name? [y/n] Y
New name: My_New_BT_Widget
Set BT connection status polarity? [y/n] Y
When connected, set STATE pin LOW(0) or HIGH(1)? [0/1] 0
Supported serial baud rates: a: 500000 b: 230400 c: 115200 d: 57600 e: 38400 f: 19200 g: 9600 h: 4800 i: 2400 Select new speed: [a-i] a
==== New parameters ==== BT Name: My_New_BT_Widget Connected STATE signal level: 0 Baud: 115200
Get ready to press HC-05 command mode button ... Press when LED lights; release when LED flashes.
Ready? [any key]
Go... [[ LED lights solid; you would press the command button and see none of the following ]] AT+NAME=My_New_BT_Widget AT+POLAR=1,0 AT+UART=115200,0,0 AT+RESET [[ HC-05 resets into 38.4kbps command mode ]] AT+RESET [[ LED flashes; you would release the command button ]] AT+RESET [[ sent at 38.4kbps; resets HC-05 to comms mode at configured rate ]] [[ repeat from start ]]
This trial doesn't actually do anything because there is no HC-05 in the loop. When connected to baudat through Bluetooth:
- you won't see the commands sent to the module in command mode
- the RESETs may or may not kill the Bluetooth connection, depending on the HC-05 firmware version
- for example, HC-05 firmware version 3.0-20170601 can change the serial bit rate without dropping the BT connection.
OR...
Example: Arbitrary AT Commands
To enter arbitrary AT commands, decline the prompted configuration:
[[ ...startup... ]]
Set BT name, "polar" & serial bit rate? [y/n] N
Enter command: AT+version
Get ready to press HC-05 command mode button ... Press when LED lights; release when LED flashes.
Ready? [any key]
Go...
[[ LED lights solid; select command mode ]] [[ HC-05 interprets following command]] AT+version [[ baudat records response up to timeout or buffer full ]] [[ LED flashes; release command mode ]]
Result: [[ stored result ]]
Enter command: AT
(these examples are in separate steps in an attempt to evade formatting borkage. if they're mangled, see the attached text files.)
Polarity?
Restarting the processor when a Bluetooth device connects with the HC-05 can be very helpful. To upload sketches over Bluetooth, for example. But making that happen takes a little doing.
While there is some variation among "HC-05" carrier boards, many (most?) have a STATE pin that signals HIGH(1) when there is an active BT connection. That's close to useful for resetting the processor, but it signals the wrong way with the wrong timing.
Conveniently, the modules provide a command, AT+POLAR= , to change the polarity of that signal so the STATE pin goes LOW(0) when another Bluetooth device connects. That solves one problem.
If there's no STATE pin, or it doesn't work, and you want this function, take the signal directly from the HC-05 module itself. See the note in the schematic above.
(schematic from here; originally from hiletgo.cn, maybe?)
Try It With the Hardware You Already Have
If you've made it this far, then you should be able to wire up and configure an HC-05.
For the wiring, I like this diagram (above) from this write-up.
The resistor values are less important than their (approximate) ratio. The ratio should be close to (5-3.3)/3.3.
If using a 3.3V mcu board, skip the resistor divider and connect the TX/RX pins directly. You'll probably have to do something different for power to the HC-05.
For Android, I like Serial Bluetooth Terminal.
This should work. You might be done.
OPTIONAL:
The connection via capacitor between STATE and RESET pins solves the timing problem left over from the last step. Together with inverting the polarity of the STATE signal, it allows the HC-05 to restart the processor when a new Bluetooth connection is established.
HERETICAL:
Some have said the resistor divider is unnecessary and may be omitted without destroying your BT module. They are fools and knaves. I would nev*cough*r consider doing such a thing... But a foolish knave who did could get this going with just four jumpers.
Make a Widget
Yay so far. But is this really any easier than any other way to configure an HC-05 without a PC USB port?
It can be if you're willing to dedicate a board that you can program, wire up ready to plug in an HC-05 and keep in your desk drawer.
Arduino board clones have become pretty cheap and some are small, which helps to make this a sensible thing to do. And then there are a few ATtiny85 boards like Digispark. They are smaller than an HC-05 (on a carrier board) and clones are nearly free.
The rest of this 'ible shows how to build the widget pictured above.
Collect Parts
There's obviously a lot of room to do what you want with what you have. This is how I did it.
Parts
- Digispark-like board (AliExpress - Digistump no longer selling )
- with microUSB
- perfboard (AliExpress)
- cut to 0.8" by 0.7"
- 6 pin female header (AliExpress)
- 6 pin & 2 pin male headers (AliExpress)
- 0603 resistor 10 kΩ (AliExpress)
- 0603 resistor 15 kΩ
- 0603 capacitor 1 μF (AliExpress)
- wire (AliExpress)
- (this example also used some cut-off component leads)
Cutting FR4 perfboard
Cutting a little piece of the FR4 perfboard that I've listed can be easy, but explaining how to make it easy would take too much space here so I wrote a separate 'ible for that.
Compiling for Digispark / ATtiny85
Digistump made the Digispark cheap and small by using a simplified microcontroller that the base Arduino "IDE" doesn't know about. So you'll have to add support for that. And I used a different serial library, so there's that too.
Read full instructions for adding Digispark support to Arduino here. Key points include:
- add "Additional Boards Manager URL": http://digistump.com/package_digistump_index.json
- in Board Manager, install package: Digistump AVR Boards
- select board: Digispark (Default - 16.5mhz)
Unpack the SoftSerial-INT0 library into your libraries folder. Remove "-master" from the folder name.
(more about the SoftSerial-INT0 library)
When compiling for Digispark, the sketch uses different pin assignments:
- RX on pin 2 - hard requirement of the SoftSerial-INT0 library
- TX on pin 0 - most Digispark variants have the LED on pin 1
- if your board has the LED on pin 0, edit the sketch to swap LED & TX pins
A real Digispark will have the RESET function disabled. It appears that many (most?,all?) clones leave the RESET function active. If your board has RESET disabled and you want to re-enable it, read here.
You may want to program and test the Digispark before soldering it into anything.
Build It
Pictures. Because easier shown than told.
Please look through all the pictures and notes before starting because sometimes a later detail helps to interpret an earlier step.
Enjoy the Fruit of Your Labor
The last picture is the first picture.