Arduino Projects: JY-MCU / HC-06

IMG_20160320_222055

I installed the Bluetooth module on a chassis with a Arduino and a motor shield in anticipation of having a Bluetooth controlled robot.

 

Lately I’ve found myself with some extra time and wanting to do some Ardunio projects.  I’m working on controlling a small robot with some DC motors and perhaps putting a buzzer on it, a range sensor for collision avoidance and maybe a mechanical arm to pick things up.  I purchased the module from Amazon for $10.

I’ve been working trying to get the bluetooth module JY-MCU/HC-06 to work, but unfortunately it took a bit of a learning curve.  Here is what I found from reading:

  • Although the RX line claims it needs 3.3V, 5V is fine.
  • The JY-MCU doesn’t need RX/TX to be discovered; all it needs is power and the blinking red light to be discovered
  • My Nexus 5 Android version 6 doesn’t detect
  • I tried a Samsung Galaxy S2.  The HC-06 was discoverable but would drop the connection after about 20s.
  • Commands can be issued through the serial command window in the Ardiuno IDE.  Here is a link to the list of the commands
  • Here is the HC06 datasheet
  • Baud Rate is 9600 by default
  • I used Sena Blueterm, free on the Android Play store, to send commands from my Nexus 7 to the device.  Here is a tutorial.
  • I’m using Bluetooth Terminal from the Android play store.

Code used to confirm that the Arduino Uno has received information from Computer –> Ardiuno –> Computer

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print(“I received: “);
Serial.println(incomingByte, DEC);
}
}

  • Nexus 7 tablet can discover the HC-06, but gives a “failed to connect device” error.  Went to sleep, then tried it again next morning with passcode ‘1234’ and it paired.  Weird.
  • Turning the BT receiver/transmitter in Android (pulldown dashboard box) seems to help.

Lighting an LED by sending commands over  Serial via the Arduino IDE

char blueToothVal; //value sent over via bluetooth
char lastValue; //stores last state of device (on/off)

void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}

void loop()
{
if(Serial.available())
{//if there is data being recieved
blueToothVal=Serial.read(); //read it
}
if (blueToothVal==’n’)
{//if value from bluetooth serial is n
digitalWrite(13,HIGH); //switch on LED
if (lastValue!=’n’)
Serial.println(F(“LED is on”)); //print LED is on
lastValue=blueToothVal;
}
else if (blueToothVal==’f’)
{//if value from bluetooth serial is n
digitalWrite(13,LOW); //turn off LED
if (lastValue!=’f’)
Serial.println(F(“LED is off”)); //print LED is on
lastValue=blueToothVal;
}
delay(1000);
}

Lighting the LED via Bluetooth

#include
SoftwareSerial BT(10, 11);
// creates a “virtual” serial port/UART
// connect BT module TX to D10
// connect BT module RX to D11
// connect BT Vcc to 5V, GND to GND
void setup()
{
// set digital pin to control as an output
pinMode(13, OUTPUT);
// set the data rate for the SoftwareSerial port
BT.begin(9600);
// Send test message to other device
BT.println(“Hello from Arduino”);
}
char a; // stores incoming character from other device
void loop()
{
if (BT.available())
// if text arrived in from BT serial…
{
a=(BT.read());
if (a==’1′)
{
digitalWrite(13, HIGH);
BT.println(“LED on”);
}
if (a==’2′)
{
digitalWrite(13, LOW);
BT.println(“LED off”);
}
if (a==’?’)
{
BT.println(“Send ‘1’ to turn LED on”);
BT.println(“Send ‘2’ to turn LED on”);
}
// you can add more “if” statements with other characters to add more commands
}
}

  • Arduino is not responding to command sent over bluetooth
    • Checked each jumper cable by lighting up and LED.  Cables are fine.
    • Unplugged the module and plugged it back in.  It works!

Next step, writing the code to control the motors.