Introduction: Setup HC-05 and HC-06, for Wireless 'Number Invaders'

As an enhancement of my previous post -- 'Number Invaders' with Virtual 7 Segment Display -- the target for this post is to show how to make this very simple game wireless, with Bluetooth module like HC-05 / HC-06.

Changing the game's code to make use of Bluetooth is very easy; nevertheless, I will show how to setup and configure HC-05 / HC-06 first.

Although HC-05 and HC-06 are functionally similar, setting them up have some subtle differences. I hope this post will make the differents clear, and be helpful for seting up HC-05 / HC-06. What you need is a microcontroler board, like Arduino Nano, some wireings, and of cause a simple sketch to do the job.

Step 1: Connection Diagram

The concentration should simply be on connections between Arduino Nano and HC-05 (or HC-06)

  • Connect HC-05 VCC to Arduino Nano 5V.
  • Connect HC-05 GND to Arduino Nano GND.
  • Connect HC-05 TXD to Arduino Nano D2.
  • Connect HC-05 RXD to Arduino Nano D3.

The rest is just the wireings for the "Number Invaders" game, the same as mentioned in my previous post.

Step 2: The Sketch for Setting Up HC-05 / HC-06

// ***
// * notes for HC-06 / HC-08:
// * . default baud rate is 9600
// * . serial monitor needs no
// * notes for HC-05:
// * . need be in AT mode; need to hold its button while powering on; LED on HC-05 will flash slowly
// * . baud rate is 38400
// * . serial monitor needs NL and CR
// ***
#include <SoftwareSerial.h>
SoftwareSerial hc(2, 3); // 2 => TX of HC; 3 => RX of HC
// for HC-6
// 9600: AT+BAUD4
// 57600: AT+BAUD7
// 115200: AT+BAUD8
// arduino: AT+NAMEarduino
// 1234: AT+PIN1234
// for HC-05
// get name: AT+NAME?
// set name: AT+NAME=arduino
// set password: AT+PSWD=1234
// get baud: AT+UART?
// set baud: AT+UART=115200,0,0
// reset: AT+ORGL
// for HC-08 (set up like HC-06)
// get info: AT+RX
// set name: AT+NAME=xxx
// set baud: AT+BAUD=nnn
// reset: AT+RESET
// reset to default: AT+DEFAULT
const unsigned long baud = 38400; // default for HC-06 / HC-08 is 9600; for HC-05, set to 38400 (assume HC-05 in AT mode)
void setup() {
// initialize Serial
Serial.begin(baud);
Serial.println("Hello from Arduino. Please enter AT command:");
// initialize HC06
hc.begin(baud);
}
void loop() {
// write data from HC06 to Serial
if (hc.available()) {
Serial.write(hc.read());
}
// write data from Serial to HC06
if (Serial.available()) {
hc.write(Serial.read());
}
}

If all the comments are removed, the sketch should be short. Moreover, the sketch should be good for both HC-05 and HC-06.

But do notice the variable baud. Make sure it is set to the correct value.

Step 3: Running of the Sketch for HC-05

  • Set the sketch's baud variable to 38400, which is the baud rate to use when HC-05 is in AT mode. Then, upload the sketch.
  • Put HC-05 in AT mode. To do this, press the button on HC-05 while powering the Arduino Nano / HC-05 up. When in AT mode, the LED on HC-05 should blink very slowly.
  • Use a serial monitor / terminal to connect to Arduino Nano. Make sure 1) the termainl is configured to use the baud rate 38400; and 2) make sure it is set to use CR and NL
  • Make connection.
  • Should see "Hello from Arduino. Please enter AT command". If not, reset Arduino Nano.
  • Enter "AT" to test if the connection is good. It should response with "OK".
  • To set name for HC-05, enter the AT command like "AT+NAME=Nano". It should response with "OK".
  • To check the name set is correct, enter the AT command "AT+NAME?". It should response with "+NAME:Nano".
  • To set HC-05 baud rate to 115200, enter the AT commant "AT+UART=115200,0,0". It should response with "OK".
  • To check the baud rate set, enter the AT command "AT+UART?". It should response with "+UART:115200,0,0".
  • That is it. Now, the attached HC-05 has a name, and is using the baud rate 115200.

Step 4: Running of the Sketch for HC-06

  • Set the sketch's baud variable to 9600 which is the default baud rate of HC-06. Note that after you changed the baud rate, this variable shoud match, if you need to re-run the sketch.
  • Use a serial monitor / terminal to connect to Arduino Nano. Make sure 1) the termainl is configured to use the baud rate 9600; and 2) make sure it is set to append nothing; i.e. no CR; no NL
  • Make connection.
  • Should see "Hello from Arduino. Please enter AT command". If not, reset Arduino Nano.
  • Enter "AT" to test if the connection is good. It should response with "OK".
  • To set name for HC-06, enter the AT command like "AT+NAMENano". It should response with "OKsetname".
  • To set HC-06 baud rate to 115200, enter the AT commant "AT+BAUD8". It should response with "OK115200".
  • That is it. Next time if you need to change it's setting, the baud rate will be 115200. Be reminded!

Step 5: Modify 'Number Invaders' Sketch for HC-05 / HC-06

In order to make use of HC-05 / HC-06 for bluetooth connection to DumbDisplay Android app, the 'Number Invaders' sketch (show in previous post), certainly need be modified, a tiny bit.

Simple replace 2 lines

#include "dumbdisplay.h"
DumbDisplay dumbdisplay(new DDInputOutput(115200));

with

#include "ssdumbdisplay.h"
DumbDisplay dumbdisplay(new DDSoftwareSerialIO(new SoftwareSerial(2, 3), 115200, true, 115200));
  • In order to change connection method, simply need to change IO object passed when create the DumbDisplay object.
  • For HC05 / HC06 attached to Arduino Nano, software serial is used. And here it is assumed the same pin assignments as described previously.
  • Note that the OTG connection mechanism is still enabled; due to how the last 2 arguments passed.


Step 6: Make the HC05 / HC-06 Bluetooth Connection

Before DumbDisplay Android app can make connect to the HC05 / HC06, the Bluetooth device need to paired with your mobile phone first.

Afterward, you should be able to connect to the Bluetooth device (HC-05 / HC-06).

That is it. Enjoy!

Peace be with you. Jesus loves you. May God bless you!