How to use HC-06 Bluetooth module to enable communication between Arduino and Android. HC-06 ZS 040 AT Commands, texting and LED examples.

1.Intro

In this tutorial let’s learn how to use HC-06 Bluetooth module, how to control it using AT commands, and turn on some LEDs remotely.

The HC-06 is a very common and affordable Bluetooth module that enables communication between your Android devices and your Arduino. There are a lot of HC-06 modules on the market that have differences in detail so today we’re talking about this ZS-040 version.

Here is the link where I ordered it:

https://www.banggood.com/custlink/vGvKySbeiJ

 

2.Connections

let’s get started from the connections. First connect the 5 volts and ground to the matching ground and VCC on the Bluetooth module, the next step is connect the d2 to TXD which is the transmission. after that it’s a trickiest part for this connection, since the module is running on a logic level of 3.3 volt, so we’ll have to build a voltage divider. Here we’re connecting two resistors in serial to D3 and the ground, here I’m using a 1 kilo ohm and 2 K resistors. By some calculation(Ohm’s Law),  the middle point should generate something close to a 3.3 volt, we can connect that center point to the RXD and that’s all the connections.

3.AT Commands

Next let’s learn about AT commands, a way to communicate with the module and set up some of its parameters. First let’s connect the Arduino to your computer and load the AT command program below:

AT command program

// Basic Bluetooth sketch HC-06_01
// Connect the Hc-06 module and communicate using the serial monitor
//
// The HC-06 defaults to AT mode when first powered on.
// The default baud rate is 9600
// The Hc-06 requires all AT commands to be in uppercase. NL+CR should be added to the command string
//
 
 
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-06 TX to the Arduino RX on pin 2. 
// Connect the HC-06 RX to the Arduino TX on pin 3 through a voltage divider.
// 
 
 
void setup() 
{
    Serial.begin(9600);
    Serial.println("Enter AT commands:");
    
    // HC-06 default serial speed is 9600
    BTserial.begin(9600);  
}
 
void loop()
{
 
    // Keep reading from HC-06 and send to Arduino Serial Monitor
    if (BTserial.available())
    {  
        Serial.write(BTserial.read());
    }
 
    // Keep reading from Arduino Serial Monitor and send to HC-06
    if (Serial.available())
    {
        BTserial.write(Serial.read());
    }
 
}

 

Once the code is uploaded we can open our serial monitor but before we type anything out, make sure you change this little setting and make sure it’s both newline and carriage return. (Notice other versions of HC-06 might have a different setting here so give it a try at different options.)

Also this module’s AT mode only works when not connected to other devices, so hold on to your phone for a minute.

The first thing we should do is to type in AT and the return is OK which means the connection is great and everything’s good. Other things I’ve tried here:

AT  –Returns OK if everything is working.

AT+VERSION –Returns the module firmware version.

AT+NAME  –Returns the name of the bluetooth module when searched by other devices.

AT+NAME = myname  –Returns OK, and the module name will be changed to myname.

AT+UART  –Returns the current Baud rate of the module, default is 9600.

AT+PSWD –Returns the current set password for pairing, default is 1234.

AT+PSWD = “1111”  –Returns OK, the password will be changed to whatever in between the quotation marks.

Here are more settings you can play with but I haven’t tested them all.

 

 

4.Connect to Android devices and start communication

now let’s get connected wirelessly,  we can have Arduino running the same code and go to your Android device, in settings go to Bluetooth and the HC-06 should come up as a device and the default passcode is 1234.

Once it got connected we need some tool to communicate to the module and here I’m using this Bluetooth Terminal app that you can download on Google Play for free.

And once you connect to the HC-06 insecurely you can see the module LED stops blinking and it only flashes twice with a much longer interval.

Now we can send texts back and forth, of course the next thing we can do it is to let the Arduino interpret the code or the command whatever you send and let it do stuff.

5.LED Demo

let’s take an LED for example We’ll need to add a couple extra components and one is a 220 ohm resistor to protect the LED and of course the LED itself.

We are connecting the resistor to Digital Pin 12 and also the LED negative to the ground.

Now you can load this led demo code below and we should be able to type in “O” an “F” for turning the LED on and off respectively.

LED DEMO program

// Basic Bluetooth LED Switch (For Zs-40 Version)
// Connect the Hc-06 module and turn on/off one LED
// On Android device, type "o" to turn LED on, "f" to turn off
//
// The HC-06 defaults to AT mode when first powered on.
// The default baud rate is 9600
// The Hc-06 requires all AT commands to be in uppercase. NL+CR should be added to the command string
//
 
 
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-06 TX to the Arduino RX on pin 2. 
// Connect the HC-06 RX to the Arduino TX on pin 3 through a voltage divider.
// Connect a LED in serial with ~220 Ohm resistor to Pin 12 and ground.

#define LEDPin 12
char recievedChar;
 
void setup() 
{
    Serial.begin(9600);
    Serial.println("Enter AT commands:");
    
    // HC-06 default serial speed is 9600
    BTserial.begin(9600);  

    pinMode(LEDPin,OUTPUT);
}
 
void loop()
{
 
    // Keep reading from HC-06 and send to Arduino Serial Monitor
    if (BTserial.available())
    {   recievedChar = BTserial.read();
        Serial.write(recievedChar);
          if(recievedChar == 'o'){
              digitalWrite(LEDPin, HIGH);    
              BTserial.write("LED is On \r\n");
            }
            else if(recievedChar == 'f'){
              digitalWrite(LEDPin, LOW);
              BTserial.write("LED is Off \r\n");
              
              }
            else{Serial.write("Wrong Command");}  

    }
 
    // Keep reading from Arduino Serial Monitor and send to HC-06
    if (Serial.available())
    {
        BTserial.write(Serial.read());
    }
 
}

so hope this is helpful and please leave a comment if any questions come up. I’ll see you next time!

 

Reference:

http://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/

2 thoughts on “How to use HC-06 Bluetooth module to enable communication between Arduino and Android. HC-06 ZS 040 AT Commands, texting and LED examples.”

  1. Chen, the video shows you using the + – strips on the plug pin board. This is NOT good practice because these are reserved for +VCC and GND. Yes, your board doesn’t have any connections present so you are OK. BUT, if a novice uses this approach and DOES have +VCC or GND connections, too, the could destroy the LED. n Your video at about 3:48 time.

    1. Ah, good catch! I was being lazy and trying to get away with it :D.. Thanks for pointing that out for everyone.

      Hopefully the viewer won’t notice that or have enough knowledge about breadboard at this level.

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar