My early Blogs about moving data between Arduino and Android apps developed using MIT AppInventor were posted back in 2014. Since then I've learned a little more about some of the features of AppInventor and also how to use Arduino SoftwareSerial.
Connecting the Arduino UNO to the HC06 Bluetooth Adapter
My 2014 Blogs had the HC06 TXD and RXD connections "piggy-backed" onto the hardware UART of the UNO - pins RXD/D0 and TXD/D1.
Note that the signal to the RXD input of the HC06 must be restricted to around 3.3v; the 5v TXD/D1 line from the UNO is connected via a simple potential divider to the RXD input of the HC06.
At the time I first worked on this, there was some advice around to suggest that piggy-backing would not work. However, it worked OK for me and also for other people!
Or you can use a breadboard:
(Author's note - I've also learned a bit of fritzing since 2014/15!)
(Thanks to http://forum.fritzing.org/t/bluetooth-hc-06/2487 for the HC06 fritzing image - I had to import it)
When I returned to using a HC06 recently, I found that I couldn't upload sketches whilst the HC06 was piggy-backed onto the UNO UART. I put this down to the enormous changes that have taken place in the Arduino IDE since 2014 - I am just speculating.
Since I don't (yet) own an Arduino Mega, with its luxurious 4 hardware UARTs, I had no option but to move on to using Software Serial on the UNO. I've chosen to use pin D3/RXD and pin D4/TXD:
Of course, the above schematic is somewhat idealised. In reality I am using a mini-breadboard stuck to a proto-shield, which then plugs into the Uno, something like this:
The HC06 is actually plugged vertically into the breadboard, rather than lying on its side as this schematic suggests.
Unfortunately, I can't find a fritzing image which accurately depicts my proto-shield, so this schematic looks as thought the mini-breadboard is stuck to the top of the Uno.
And the final reveal:
As can be seen, the proto-shield (a pre-R3 arduino shield) also carries a reset button and 2 LEDs.
Developing the Software - Arduino
Here is a very simple sketch to send data from the keyboard to the HC06. The data items are echoed to the serial monitor and also sent to the HC06 by SoftwareSerial. Note that an HC06 is delivered set to 9600 baud, although you can configure it for higher or lower baud rates
#include #define rxPin 3 // define SoftwareSerial rx data pin #define txPin 4 // define SoftwareSerial tx data pin SoftwareSerial blueTooth(rxPin, txPin); // create instance of SoftwareSerial void setup() { Serial.begin(9600); // Start hardware Serial blueTooth.begin(9600); // Start SoftwareSerial } void loop() { char c; if (Serial.available()) { c = Serial.read(); Serial.print(c); // Write the character to the Serial Monitor blueTooth.write (c); // Write the character to Bluetooth } }
I always like to check my work as I go along - go too far down the track and debugging can become tortuous! The objective here is for the Arduino to transmit data, via the HC06, to an App running on an Android device - the App to be developed in AppInventor. Before we get into developing the App, let's establish that the Arduino and the HC06 are happily working together and the sketch is doing what we want.
You can do this quite easily with a terminal emulator, like Tera Term, provided your PC has Bluetooth. I have done this on Windows 7 and Windows 8; unfortunately my Windows 10 PC doesn't have Bluetooth.
However, since the target is an Android device, 'phone or tablet, it's very easy to use an App called Blue Term, which is free to download from Google Play.
The setup process is as follows:
- Make sure the Arduino and HC06 are powered up - LED on HC06 should be flashing.
- Pair the HC06 to your phone - the default pairing code is 1234.
- Start the Blue Term App.
- In the top right hand corner it shows not connected.
- In the bottom right hand corner, press the 3 dots symbol and click Connect Device.
- Blue Term will scan and give you a list of all the paired devices - there may only be one!
- Click on HC-06
- The top right hand corner will show connecting and then connected-HC-06. The HC06 LED will stop flashing.
- In the Arduino IDE, open the Serial Monitor, setting the terminator to both NL and CR.
- Type a test string on the PC keyboard and then press Return.
- The string should appear in the Blue Term window on the Android device. (if the text is difficult to read, press the 3 dot symbol again, select preferences and then Font Size - the largest seems to 20pt, which is still quite small!
- If it doesn't work then start checking connections TXD and RXD, the potential divider, the resistor values, the Arduino Sketch etc etc.
It is pointless to move on to developing the Android App until the Arduino/HC06/Arduino Sketch is working!
_________________________________________________________________________
EDITED ON 23 May 2018 - I've just noticed that line 1 of the sketch has been corrupted! It should read:
#include <SoftwareSerial.h>
Apologies for that
Top Comments