Toggling a LED with Arduino and HC-06

Following the previous post describing the HC-06 module, this one shows how to use it to build a simple program to toggle the debugging LED of an Arduino board, using Bluetooth.

This tutorial assumes the use of the HC-06 integrated in the baseboard described in the previous post.

Connecting HC-06 to the Arduino Board

Using the previously mentioned baseboard with HC-06 makes it very easy to connect it to an Arduino board, as shown in figure 1. For simplicity, only the relevant pins are shown.

It’s important to note that the Tx pin of the Arduino is connected to the Rx pin of the module and the Rx pin of the Arduino is connected to the Tx pin of the module. Assuming that the 2 pins of the same name need to be connected is a very common mistake when using serial communication .

Since there is no standard version of the baseboard for the HC-06, I’ve seen some of them with extra pins accessible, with names such as “key”, “en” or “stat”. Nevertheless, for all the boards that I’ve worked with, they could always remain unconnected.

Important: HC-06 is a 3.3 V device. Although the baseboard allows a supply voltage of 5 V, there is no clear specification if the serial signal is converted to 3.3 V. I’ve used it for quick tests with the connections shown in figure 1 many times without ever verifying any problem, but I assume no responsibility for any material damage it can cause. Nevertheless, for final prototypes, I always use a level shifter to ensure that no problem occurs.

schematics-project (3)

Figure 1 – Schematic for connecting the HC-06 to an Arduino board.

Default configurations

HC-06 comes with some default configurations that we need to take into account when using the module for the first time.

The default baud rate for the module is 9600 [1], meaning that we need to use this value when first connecting it to an Arduino Board. After that, we can change the value using an AT command. Please check the list of supported AT commands here.

When we pair the module for the first time with a device, we need to insert its PIN code. The default PIN is “1234” [1], which can also be changed using an AT command.

Arduino Code

First, we declare two global variables. The first one will hold the LED’s current state. The second one only contains the number of the LED PIN, in order to make the code more easily readable and changeable.

bool state=false;
byte ledPin=13;

In the setup function, we start the serial communications with the module’s default baud rate and we set the pin mode of the digital pin.

void setup() {

Serial.begin(9600);
pinMode(ledPin, OUTPUT);

}

Finally, in the main loop, we just read any data available in the serial port. If it corresponds to the “t” character (from “toggle”), we change the current state of the LED.

void loop() {

if(Serial.available()){

char data = Serial.read();

if(data == ‘t’){

state=!state;

digitalWrite(ledPin, state);

}

}

}

From this code, we can easily conclude that the use of the HC-06 is completely transparent for the Arduino, which sees it as a regular serial connection using an USB cable.

 Sending data from a computer

In order for the HC-06 to receive the serial data to toggle the LED, we first need to pair it with a computer (or any other Bluetooth device).

To make the HC-06 detectable, we need to connect it to the power supply. In this step, we don’t need to have it connected to the Arduino. After that, we just pair it with the computer as we would do for any other Bluetooth device. This will depend on the operating system used, so this tutorial will not cover that step.

After that process, the HC-06 will just appear as any other serial device, making it possible to use with the serial console of the Arduino IDE. Then, just connect to the corresponding COM port and send a “t” character to see it working.

Important: After pairing with the device , it’s possible that 2 distinct serial connections (COMs) are detected for the HC-06.  One of them will not work when we try to connect using the serial console of the IDE, so we need to try both to figure out which one to use. Nevertheless, the working COM will stay the same in following connections, even after disconnecting the HC-06.

Final Notes

  • The method of sending just a character to toggle the LED should only be used for testing purposes. This is not suitable for a final application. Check this post on how to build a robust serial communication protocol.
  • I used the same code of this post to create the application of toggling a lamp, shown in the previous post. The only difference was that the Arduino was connected to a cheap 5 V relay. Please don’t try it if you don’t feel comfortable working with line voltage.

References
[1] http://wiki.pinguino.cc/index.php/SPP_Bluetooth_Modules

2 thoughts on “Toggling a LED with Arduino and HC-06”

  1. This bluetooth-serial enables funny experimental settings. I have a irobot roomba vacuum cleaner which has a “hidden” serial interface. Using the HC-06 its possible to send serial commands via bluetooth and make it kind of remote controlled. An AVR connected to the roomba can loop through the serial commands sended over bluetooth.

    1. That would be a very cool project! You can even take the remote controlling one step further by using an ESP8266 instead of the HC-06 to create a serial-Wifi interface, allowing to control it over the Internet.

  2. This bluetooth-serial enables funny experimental settings. I have a irobot roomba vacuum cleaner which has a “hidden” serial interface. Using the HC-06 its possible to send serial commands via bluetooth and make it kind of remote controlled. An AVR connected to the roomba can loop through the serial commands sended over bluetooth.

    1. That would be a very cool project! You can even take the remote controlling one step further by using an ESP8266 instead of the HC-06 to create a serial-Wifi interface, allowing to control it over the Internet.

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading