Thursday 28 November 2013

Bluetooth communications for the Arduino

I have never seen such confusing garbage on various web sites as when I searched for Bluetooth use on the Arduino.

I found there are two modules common on the market, the HC-05 and HC-06. There is also a JY-MCU with two versions for "slave" only or "master - slave" applications.

The JY-MCU provides only a "slave" connection if you buy the one that uses the HC-06 module. This is the one described here.

These are the connections for a simple system:

Screen Shot 2013 11 28 at 16 45 54

Take care the Bluetooth BTX goes to the Arduino ARX (0) and BRX to ATX (1).

The Bluetooth name of this module is set by some AT commands you give. The password or PIN for pairing is changed to 0000. The default baud rate is 9600bps.

Two ways to program

If the module is simply connected as above to the Arduino serial RX & TX pins (0 and 1), which are also used by the USB connection, then the Serial.h library can be used. Like this:

#include "Serial.h"

#define LEDPIN 8

void setup() {

  pinMode(LEDPIN, OUTPUT);
  
  Serial.begin(9600); // start serial comms on default pins 0 & 1
}

void loop() {

  char v;

  if(Serial.available() > 0) // if data in the serial buffer, read it
    v = Serial.read();

  if(v == ‘H’) {
    digitalWrite(LEDPIN, HIGH); // turn LED on
  {
  else if(v == ‘L’) {
    digitalWrite(LEDPIN, LOW); // turn LED off
  }

  delay(100);
}


These steps must be followed as the USB must be inactive when the BT is connected as they use the same signals on pins 0 & 1.

1 Connect USB to power BT module, only +5 and GND connected

2 Pair MAC to BT module

3 Run this simple sketch to find the BT device port

void setup() {

    Serial.begin);
    println(Serial.list());
  }


4 Load the BT sketch above via USB and then go to IDE > Tool > Serial Port and click on the device for the BT module

5 Disconnect USB, wire up the BT module RX and TX signals, connect battery supply

6 Reset Arduino to run the BT sketch above

7 Open IDE Serial Monitor which should now be talking to the BT

8 Enter H or L to control LED via Bluetooth

To stop using BT remove the connections to pin 0 RX and pin 1 TX and chose the IDE Serial Port device for USB.

Arduino SoftwareSerial library

The second way is by using the SoftwareSerial.h library, using any other pins, but not RX 0 & TX 1.

You find the BT device port as in 3 above to be able to connect in the IDE.

#include "SoftwareSerial.h"

// BT TX to pin 10 ARX, RX to pin 11 ATX
#define ARX 10
#define ATX 11

SoftwareSerial myBT(ARX, ATX); // create BT object

// can use both USB and BT ports
void setup() {

  Serial.begin(9600); // open comms via USB device port, chose in IDE
  myBT.begin(9600); // open comms via BT device port, chose in IDE 

}

void loop() {

  if(myBT.available()) {

   // do something, e.g. char x = myBT.read();
  }


  if(Serial.available()) {
    // do something e.g. char x = Serial.read();
  }
}


Nothing here has yet been tested as I have not yet received my BT module, but when it arrives I will try it out and update this post.

No comments: