söndag 19 juni 2016

Using HC-06 Bluetooth with Adafruit Feather



Well.. To be honest this was not at all what I was working on right now, but after stumbling about for a bit I feel like I should share some little discoveries I made while poking with my Adafruit Feather M0 Adalogger and a recently bought cheap HC-06 bluetooth card.. So here goes..

The sad background

While working on my 433 enabled door-lock, I got sort of annoyed with that the boards 3.3V was not enough to get any range on my 433 receiver, so I went out and bought a Trinket. After a few hours of trying to get that to work with my mac I gave up... The helpful service at Adafruit told me that the bootloader of the Trinket probably was damaged and gave instructions on how to reset it.. But I did not own the Arduino-board necessary for that trick so I just shoved the Trinket into a dark little drawer where it will have to remain until I've forgiven it.
An Adafruit Feather M0 Adalogger


Next I went and bought a ESP8266-01 WIFI-card and spent around two days with it before shoving it into the dark drawer where it will have to spend some time with the Trinket. I  am not sure if the problem was with using a non-standard board (my Feather) or if that card just sucked, but it was a painful experience.
My worst enemy so far, the troublesome ESP8266-01

Enter Bluetooth

I then decided to try my luck with Bluetooth. I went out and bought yet another cheap little card, the HC-06, and this time it worked out fine!
The HC-06 card in all its glory.

Schematics/Pins

Hooking it up to my feather was pretty easy, the only thing I missed originally was that I should of course switch the RXD/TXD ports so that the TXD port on the HC-06 went to the RX port on my Arduino, and the RXD port on the HC-06 went to the TX port on my Arduino.
Please note that in this diagram I used a Adruino Micro-board as I could not find a Adafruit Feather one. Make sure you use the correct ports on your Feather and do not follow the port layout of the diagram above!

So the ports go:
ArduinoHC-06
3.3VVCC
GNDGND
TXRXD
RXTXD 

This is what my setup looked in real life.

Pairing

Okay, I hooked up the board to my OS X using an USB cable. The red LED on my Feather turned on and the red LED on the HC-06 started to flash madly.

I went into the Bluetooth settings of my OS X and checked for devices. Among the found devices was "HC-06". I selected to pair with it, waited out the inevitable fail :) and then retried now that I got to enter the pairing pin ("1234" without the quotes).

Pairing worked out fine and the red LED on the HC-06 stopped flashing and instead produced a steady light to show it was connected.

After a few seconds the HC-06 disconnected and started to flash wildly again. This did not bother me as I intended to hook it up using the serial monitor of the Arduinio IDE instead.

Coding

So then I fired up my Arduino IDE and configured the Board to be the Adafruit Feather M0 and used the normal port with that.

I then took the echo-script I found on an great Instructable by .
This wont work straight out of the box with the Adafruit Feather though, as the TX and RX pins are referred to as Serial1 and not Serial (which is the one the  USB port is using).
With this little change, the echo sketch now looked like this:

String message;

void setup()
{
  Serial1.begin(9600);
}

void loop()
{
  while(Serial1.available())
  {
    message+=char(Serial1.read());
  }
  if(!Serial1.available())
  {
    if(message!="")
    {
      Serial1.println(message);
      message="";
    }
  }
  delay(5000);
}


 With that little adjustment I verified the code and uploaded it to my board.

Test run

With the USB cable still connected to my OS X (for power), I switched the port to be the Bluetooth one (it contains HC-06 as part of its name), and then fired up the Serial Monitor.
After a few seconds the Serial Monitor popped up, and at the same time the madly blinking LED on the HC-06 was replaced by a steady light signaling that the HC-06 was hooked up.

I then sent a text string to the device, and got it echoed back. Success!

Lessons learned

Not much to say.. Make sure to connect TXD to RX and RXD to TX and also, use Serial1 when trying to use TX/RX as serial ports on the Adafruit Feather M0 Adalogger.