Saturday, April 23, 2011

Bluetooth with Arduino and Processing program on a XP laptop

This is to document the stuff I went through to make bluetooth work.

It took some work to get a XP PC running a processing program to talk to an Arduino over bluetooth.

EDIT:
Let me add a couple disclaimers before you get too far.
With my current setup, I have to make the connection with the laptop right next to my Arduino.  Once I have the connection, I can move away.
Distance is limited.  I lost the connection after about 30 feet.  Signal had to go through a metal chassis, a window and a wall to get to me though.
If I had to guess, I think it is the cheap dongle I bought.  I ordered a toshiba bt dongle, will see if that works better.

On one side, I have an arduino duemilanove.  My specific application is a coffee roasting PID program.

I bought this bluetooth module, the price was right
mdfly bluetooth module

First off, I had trouble talking to the module with my duemilanove, but luckily I had a mega that I bought for another project, so I connected the bt module to my mega.

I connected it to com 3, and used this code in the mega

//start program
void setup()
{
Serial.begin(9600); //pc
Serial3.begin(9600); //BT
}
void loop()
{
char ser_char;

if (Serial.available() >0){
Serial3.write(Serial.read());}
if (Serial3.available() >0){
ser_char = Serial3.read();

Serial.write(ser_char);
Serial3.write(ser_char); //ECHO
}
}  //end program


EDIT
If you have an arduino with just one port, you can run this code instead

void setup()
{
//Serial.begin(57600); //pc
Serial.begin(9600); //pc
}
void loop()
{
char ser_char;
 
if (Serial.available() >0){
  ser_char = Serial.read();
 
  Serial.write(ser_char);
  }
}
This code will let you open the bluetooth serial port, and echo what you type in the serial port window.
end edit

Next I connected the bluetooth module to my arduino mega.  I added the two resistors to the bluetooth rx line because the actual bluetooth module is running on 3.3 v.  I found someplace that the module inputs are not rated for a signal over 3.3V, and they recommended adding a resistor divider to bring the voltage down. 
Two 10k resistors should work fine, I used some values I had around.
If the signal level is 4.5V, my resistors give a 2.46v signal.
Two 10k's will give you a 2.25v signal, and either is high enough.




After setting up the arduino environment for a mega, I downloaded the code above.  I ran "serial monitor" from the tools menu of arduino.  I am pretty sure I had to open another serial port, although I'm not sure why.  I opened the port with putty.  If you don't have putty, I recommend getting it to use for this, it's easier to use then the windows terminal program.

Serial monitor should be set up for 9600 baud.

When it is working correctly, you can type "AT" on the arduino serial monitor, and it should respond "OK".

To change the baud rate, you type "AT+BAUD7"  to change to 57600 baud.  Should respond  OK57600, but then it will stop responding until you change all the baud rates.  Remember you need to change the baud rates for the PC programs (arduino and terminal program), and then change the baud rate for the arduino program
These two lines in the code above, change 9600 to 57600
is
     Serial.begin(57600); //pc
     Serial3.begin(57600); //BT

was
     Serial.begin(9600); //pc
     Serial3.begin(9600); //BT


Now for getting bluetooth working.

I bought these PC bluetooth adapters
ebay bluetooth adapter
BTW, I bought two of them, and I'm glad I did.  Turns out one doesn't work very well, and only connects intermittantly.  The other one works fine, I guess you can't expect much for a couple of bucks.
With the mega running the code above, I was able to connect to the bluetooth module, after some fiddling, with no problems, if I used putty to talk to the bluetooth serial port.  This was using the standard XP drivers.  But, no matter what, I could not get a processing program to talk over bluetooth.  I could not even see the bluetooth serial port, when I listed the serial ports in the processing.

I tried some things, then started trying different bluetooth drivers.  First driver didn't recognize my dongle.  I tried bluesoleil, and that worked better. With this, at least I could now see the bluetooth com port in processing, but it still wouldn't connect.

Finally, I found the Toshiba blue tooth stack.

This thread at this forum has a link to download the stack.
Toshiba bluetooth stack thread

The problem is that it is a 30 day version, so if you don't have a toshiba laptop, or a toshiba bluetooth dongle, it only has a 30 day license.

Download and install the Toshiba bluetooth stack software.

At some point, it will ask you to add your bluetooth device, so plug in the dongle, and let it continue.
After it installs, let it reset you PC.

After the reset, double click on the little bluetooth symbol in the task tray, and a "Add New Connection Wizard" box will pop up.  Turn on the Mega, push next in the wizard window.  It will search for bluetooth devices, and then this "Select a device" window will pop up.



Push next, and this window will pop up



Select Next, and then this will come up


Double click on Linvar, and then it will ask for a PIN.  Type "1234" for the PIN, and OK



In the Bluetooth Settings window, click on Detail, and this window will pop up.  It shows you what com port the bluetooth port is on.


On my PC, it is COM40.

Now, you should be able to go use putty, and open COM40, and when you type things in, they will echo back if it is working right.  You may need to open another port (again, don't know why)

When you are connected on the bluetooth port, the LED on the bluetooth module will stop blinking, and just go on solid.

After this, I removed the bluetooth module from the mega, and moved it to my other Arduino setup.
Connected as follows:
EDIT updated to add switch





I added one more thing, a switch on the Arduino Pin 0 (Arduino serial RX).  The switch either connects the bluetooth module, or opens the connection.  When the connection is open, you can hook up the arduino with USB, and download a new program.

After connecting, and downloading the actual program, I was able to run my processing program, set to COM40, and it connects and works fine :)

Hope this helps someone get bluetooth working.