Wednesday, May 9, 2012

Android to Microcontroller via Bluetooth

1. Choose a bluetooth device for your microcontroller.
The first thing you need to do is decide which bluetooth device you want to use for your microcontroller. You will want a device that you can communicate with via the serial protocol.  I used the BlueSmirf Gold modem from Sparkfun.

You will probably have to solder some headers to the chip, I used male headers to make it simple to plug into my breadboard.
2.  Connect the bluetooth device to the microcontroller
To connect the bluetooth device, you will need to find the Rx and Tx pins on your microcontroller.  Make sure that you cross the wires by connecting the Rx pin from your bluetooth device to the Tx pin on the microcontroller, and the Tx pin from your bluetooth device to the Rx pin on the microcontroller.  You will want to look in your datasheet to make sure you are using the right pins.  If you are using an Arduino, the diagram below may be helpful.
3.  Configuring the Bluetooth Device
To configure the bluetooth device from a computer, it is helpful to have a USB to serial breakout board.  I use the following from Sparkfun:
The simplest way to hook this up to the bluetooth modem is by using the 4 pins located along the front. Make sure you cross the Tx and Rx wires.  The board above has a 3.3v pin that is powered via the USB when plugged into the computer.  The BlueSmirf above can also operate on 3.3v, so powering the BlueSmirf is as easy as running a wire from the 3.3v output of the USB device to the BlueSmirf.  Also, make sure you run a wire from the ground pin of the USB device to the BlueSmirf to give them a common ground.
Now that everything is connected you can use a program such as Hyperterminal or Tera Term to communicate with the bluetooth modem.  HyperTerminal may already be installed on your computer if you are using Win XP.  Whichever application you are using, create a new connection.  Make sure to select Serial Port and the desired COM port.  If you do not know which COM port to use, you should be able to find it by looking in the device manager.  Mine says USB to Serial Port (COM3).  You also need to configure the serial port connection to use the corect baud rate.  Remember, the default baud rate of the BlueSmirf is 115200.  If you are using a different bluetooth device you will want to check your data sheet to determine what the default baud rate is.
The following instructions are specific to the BlueSmirf.  If you have a different bluetooth device you will want to refer to your datasheet.
Now that everything is configured, type $$$ in the prompt.   If everything is wired up correctly, you will received a response of CMD.  If you are using the BlueSmirf, the status LED will start blinking rapidly.  Now type D and press enter.  This will display the current configuration settings.
If you want to change the baud rate (to 9600 for example) type SN,96 and press enter.  You should receive a response of AOK which signifies that the change was successful.  Again, type D to see the settings and verify that the baud rate is now 9600.
Once you have completed the change, type — to exit command mode and terminate the connection.  If you desire to make additional changes to the configuration that I have not covered here, you should be able to find detailed commands for the bluetooth device that you are using in the documentation.  For the BlueSmirf you can look here.
4.  Writing code for the microcontroller
Depending on the microcontoller you are using, this may or may not be a daunting task.  You will want to refer to your data sheet for specifics on how to set this up for your microcontoller.  One thing to remember is that you must use the baud rate that your device is configured to use.
Use the settings you have configured above to initialize the serial driver on your microcontroller.  If you are using an Arduino, this is as simple as writing Serial.begin(baudRate); in your setup function.
That’s it for Part 1.  At this point you should have a bluetooth device connected to your microcontroller and be able to read/write to it using a USB to serial breakout board such as the one I discussed above.  In Part 2 I will discuss how to interface this bluetooth device with an Android phone.  Hopefully the information above has been helpful, let me know if you have any questions.

By ryandebenham.com


Post a Comment