In my previous post I described the implementation of a two pin, interrupt driven serial interface for the ATtiny - in this post I'll describe how to configure and use a HC-05 Bluetooth breakout board with that interface so you can remotely control any ATtiny based circuit.

The HC-05 Bluetooth Module

HC-05 Breakout

These modules are widely available and usually relatively cheap (about $AU 6 on eBay). There are two common configurations available - one is a module with a pair of IO pins down each side and the other format is a breakout board with some supporting circuitry and a set of 6 pins at one end. Both have the same functionality but using the module gives you easier access to the GPIO pins on the module as well as some additional state - they are a little harder to design into your circuit though.

For this post I'm using the breakout board, the techniques I describe can still be used with the module format although the pin connections will be in a different location.

Module Configuration

The modules I am using (and, from what I can tell, most other variants) default to using the device name HC-05 and communicate at 9600,N,8,1. We certainly want to change the speed and it would be nice to change the name as well. Thankfully - the chipset has an AT command mode you can enter which allows you to reconfigure the device settings. The changes you make using this method are persistent and will be preserved across power cycles.

HC-05 Configuration

To talk to the module for configuration I am using a 3.3V FTDI Friend cable to provide power as well as communications lines. The diagram to the left shows how to connect the devices together. When you wire it up on the breadboard do not connect the 3.3V lines before you plug the FTDI cable into the USB port.

According to the datasheet there are two ways to enter AT command mode:

  1. If the STATE pin is held high when power is applied to the module it will boot into command mode and set the serial port to 38400N81 regardless of the current serial port settings on the device.
  2. While the device is running pulling the STATE pin high will switch to command mode at the current serial port settings.

    For this example we are going to use the first method. First, connect all wires except the 3.3V ones. Plug the FTDI adaptor into your USB port and connect to it with your favourite terminal program at 38400N81. Make sure your serial program is configured to send CR/LF (\r\n) line endings. Connect a wire from the FTDI 3.3V output to the STATE pin and then connect the wire from the FTDI 3.3V output to the 3.3V pin on the breakout board. The red LED on the board should start flashing slowly (about once every two seconds).

    To test the connection, try the following command (in these examples the < and > characters indicate the data transfer direction - don't type them).

 That example simply sends the basic *AT* (attention) command and the device should respond with a simple *OK* to indicate that it got it. If you don't get the *OK* response double check your wiring, the voltage level and your serial port settings.

> **NOTE**: These are relatively cheap devices and are not always of the best quality - it is well worth buying more than one just in case you get a unit that doesn't work. I bought a batch of 5 from the same supplier and one of them simply refuses to enter command mode for some reason even though I can communicate with it over Bluetooth with the default settings.

 Once you have access to command mode there are a wide range of options you can change (see [the datasheet]( for a full description of what is available). In this post I'm just going to concentrate on some of the most common options.

 The first thing we will want to change is the serial configuration - 9600 is way to slow for most purposes (and my [software UART](/a-software-uart-for-the-attiny85/) implementation won't work at such low speeds anyway). The following commands will do the trick ...
> AT+UART?     < OK     < +UART:9600,0,0    > AT+UART=57600,0,0     < OK    > AT+UART?     < OK     < +UART:57600,0,0

In this case we are querying the current settings ('AT+UART?') and the device responds with the currently active settings (9600 N81 in this case). Next, we tell the device to use 57600 baud, no parity and 1 stop bit ('AT+UART=57600,0,0' - the command format and parameters are described in the datasheet). Just to check that everything is saved away safely we query the current settings again to make sure they match what we just set.

The next thing to change is the device name - this is what appears when you browse for available Bluetooth devices. The default name for the module is HC-05 so something that better reflects what you are connecting to would be nice. Use this sequence of commands ...

 Once again we query the device for the current settings, change it to the value we want (in this case '*BLUEBELL*' which is what I've decided to call the device I'm working on) and then query it again to make sure that our changes have gone through.

 Finally, we want to change the pass-code used to pair with the device from the default value (1234) to something that is (presumably) secret.
> AT+PSWD?     < OK     < +PSWD:1234    > AT+PSWD=0000     < OK    > AT+PSWD?     < OK     < +PSWD:0000


Testing is very easy - from the ATtiny side we are simply writing to a serial port, no other setup is required and the only pins you need to connect (apart from 3.3V power and ground) are the Tx and Rx pins. As long as the baud-rate and serial settings match we just read and write data as normal. From the PC side all we need to do is pair with the Bluetooth module and enable it as a serial port.

Serial Connection

I use Xubuntu on my desktop and pairing is done through the Bluetooth Manager application in the Settings Manager. The pairing process is straight forward and the device will automatically be detected as being a serial port.

On my system the pairing process results in a new serial port called /dev/rfcomm0 which I can connect to with my normal serial terminal (in my case I use CuteCom which is perfect for development tasks). Connecting on this port at 57600 now lets me talk to the code running on the ATtiny.

The sample program I'm using simply echoes back all the data it receives and converts lower case letters to upper case just to prove it is not going to some internal loop-back. The image to left shows the results in CuteCom.

The sample program is very simple - this is what the main function looks like:

``` void main() { uartInit(); sei(); while(true) { if(uartAvail()) { uint8_t ch = uartRecv(); if((ch>='a')&&(ch<='z')) ch = (ch - 'a') + 'A'; else if((ch>='A')&&(ch<='Z')) ch = (ch - 'A') + 'a'; uartSend(ch); } } }

Next Steps

Now we have a way to communicate with device without wires we can easily send and retrieve state from it. I'm planning on using an RGB LED for the notification indicator and I'd like to use PWM to get a full range of colours out of it. That is going to be the subject of the next post.