Tuesday, February 13, 2018

HC-06 with Windows 10 - My experience

Many people, including myself, have used HC-06 with many earlier versions of Microsoft Windows, such as Windows Vista, 7 and 8 without any problem. The HC-06 module did it's job happily and the Bluetooth Serial link was very reliable. However with the release of Windows 10, it became hard for me to get connected with their HC-06 Bluetooth Serial port.




However, after an years effort, I realized that the following simple fix could solve this issue without any struggle.

The Issue 

The issue could be summarized as follows.

  • HC-06 gets paired with windows 10.
  • Cannot add a Bluetooth Serial Port for the device
    • HC-06 is not listed in the device list
  • Bluetooth Serial port does not show up in the terminal software

The fix

This is not an uncommon issue. Windows 10 had the same issue with many other Bluetooth devices, other than the HC-06 transceiver module. Therefore, it is most likely for the manufacturer your PC's Bluetooth radio to release a driver update which fixes this issue.

  1. Update the Bluetooth Driver
    • Try to find the update from the manufacturer's website directly.
    • If  the above step is not successful, the driver could be updated using windows update service.
  2. Restart the PC
    • Restarting all the services helps.
  3. Pair the device again, and add a Bluetooth serial port.

Check the drop-down menu for our module

Only the Outgoing port is useful for HC-06

Once the port is added, it will be visible in any Terminal software.

Bluetooth Serial port is available



Hello from Arduino (Over HC-06)


So after just a simple driver update, HC-06 Bluetooth Serial communication on Windows 10 works well, at least for me.

My OS Version is Microsoft Windows 10 Pro. Find version info from the below image.

My Windows version info

I cannot guarantee that this will work for everyone, but it did for me. So it's worth a try.





Saturday, February 10, 2018

Connecting Arduino to Android with HC-06 Bluetooth module

HC-06 Bluetooth module

Many of my colleagues and friends often ask me help on connecting their Arduino boards with Android apps, so that they could control external devices using their smartphones. For this task, I always recommend the HC-06 Bluetooth slave module.

How does it work?

HC-06 works as a UART to Bluetooth radio, which broadcasts whatever the data read from the UART interface, and vice versa. Therefore, no special commands are required to communicate over Bluetooth using HC-06. It will simply transmit the data sent to it by using Arduino's Serial.print() function.

Hooking up HC-06 with an Arduino UNO


Getting Started: The Android app

To get started, I recommend using the Bluetooth Control for Arduino from Play Store. This app provides a good interface to control digital outputs.


Each of the four large buttons on the front screen of this app could be used to turn on and off four digital output pins separately.  When each button is pressed for the first time, they will send out the characters 1, 2, 3 and 4 respectively. When the are pressed again, the characters A, B,C and D will be sent out respectively.
The first set of characters could be used to turn on the outputs, and the second set of characters could be used to turn them off.

Getting Started: The Arduino code


 #include <SoftwareSerial.h>  
 SoftwareSerial mySerial(10,11); //Software Serial, RX=pin 10, TX=pin 11  
   
 const int output1=3;  
 const int output2=4;  
 const int output3=5;  
 const int output4=6;  
 String input;  
   
 void setup() {  
  Serial.begin(9600);  
  mySerial.begin(9600);  
  pinMode(output1,OUTPUT);  
  pinMode(output2,OUTPUT);  
  pinMode(output3,OUTPUT);  
  pinMode(output4,OUTPUT);  
 }  
   
 void loop() {  
  while(mySerial.available()) {   //read the serial input  
   input += (char)mySerial.read();  
   delay(5);  
  }  
  if (input.length()>0) {   //if the input is not empty  
   setOutputs(input);    //call the setOutputs function  
  }  
  input="";  
 }  
   
 /*set Outputs according to the inputs*/  
 void setOutputs(String input){  
  if(input.indexOf("A")>-1)  
   digitalWrite(output1,LOW);  
  else if(input.indexOf("B")>-1)  
   digitalWrite(output2,LOW);  
  else if(input.indexOf("C")>-1)  
   digitalWrite(output3,LOW);  
  else if(input.indexOf("D")>-1)  
   digitalWrite(output4,LOW);  
   
  else if(input.indexOf("1")>-1)  
   digitalWrite(output1,HIGH);  
  else if(input.indexOf("2")>-1)  
   digitalWrite(output2,HIGH);  
  else if(input.indexOf("3")>-1)  
   digitalWrite(output3,HIGH);  
  else if(input.indexOf("4")>-1)  
   digitalWrite(output4,HIGH);  
  }  

This code will turn on and off four LEDs attached to digital pins 3, 4, 5 and 6. They could be replaced with relays to control devices that requires AC mains to operate. 

Monday, February 5, 2018

Reading Arduino Inputs to a Binary Number



In one of my recent projects, I had to read a few digital inputs of an Arduino board into an integer variable as a binary number. My initial thought was to use direct register addressing, but later on, I came up with two other methods which work really well. Here on my blog, I share all three of them, hoping that these would help someone else as well.

Note: Pull down resistors MUST be used for each example.


1. Only using digitalRead()
 void setup() {  
  Serial.begin(9600);  
  pinMode(2,INPUT);  
  pinMode(3,INPUT);  
  pinMode(4,INPUT);  
  pinMode(5,INPUT);  
 }  
 void loop() {  
   int b0 = digitalRead(2);  
   int b1 = digitalRead(3);  
   int b2 = digitalRead(4);  
   int b3 = digitalRead(5);  
   int output=b0+(2*b1)+(4*b2)+(8*b3); //create the binary number  
   Serial.println(output);  
   delay(100);  
 }  


2. Using BitShiftLeft (<<)

 void setup() {  
  Serial.begin(9600);  
  pinMode(2,INPUT);  
  pinMode(3,INPUT);  
  pinMode(4,INPUT);  
  pinMode(5,INPUT);  
 }  
 void loop() {  
   int b0 = digitalRead(2);  
   int b1 = digitalRead(3);  
   int b2 = digitalRead(4);  
   int b3 = digitalRead(5);  
   b1=b1<<1; //Shift the second bit by one binary place  
   b2=b2<<2; //Shift the third bit by two binary places  
   b3=b3<<3; //Shift the fourth bit by three binary places  
   int output=b0+b1+b2+b3; //add them together  
   Serial.println(output);  
   delay(100);  
 }  


3. Reading the registers directly - Simplest method so far

In AVR microcontrollers, there is a register called Chip Register, which stores the input value of a particular group of pins (a port). In This example, Chip register is read and assigned directly to an int variable.

 void setup() {  
 Serial.begin(9600);  
 }  
 void loop() {  
 DDRC=0x00; //Set all the pins of PortC to be inputs (A0 to A5 on Arduino Uno)  
 int a=PINC; //Read the input pins directly into a binary number   
 Serial.println(a);  
 delay(500);  
 }  


This last code is amazingly simple, and lightweight.  It could be way more useful when combined with Arduino Core functions.