I'm working on a project where I need to have Bluetooth capability on my Arduino Nano to send data from a sensor over to the computer. Unfortunately, I'm having a difficult time figuring out how to establish a connection from the HC-06 to the computer.
I've hooked up the HC-06 to my Arduino in a fashion similar to the picture shown below.
I've also been testing it with various code that I've found online that deal with tutorials on the HC-06. The current code I'm working with is below.
#include <SoftwareSerial.h>
SoftwareSerial BT(1, 0);
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
}
}
When I search for a connection on my computer, it finds the HC-06 and I can then attempt to establish a connection. I enter the pairing code of 1234. It then says that a connection has been established so I go into the Bluetooth Services tab where it tells me what COM port to use (either COM7 or COM4 depending on which computer I try it on. I've tried this on two separate computers.)
So, I open a PuTTY session and attempt to connect to the indicated COM port. About 50% of the time, it says that a connection could not be established to the indicated COM port. The other 50% of the time, I am able to open a PuTTY session but then nothing happens. The green LED on the HC-06 continues to blink away no matter what I try to do.
Any help would be VERY appreciated as I feel like I've tried everything I can think of.
Thank you!
a=(BT.read());
has unneeded brackets. Remove the outside ones