Monday, August 8, 2011

Adding Bluetooth to your MSP430 Project

Most projects which I have seen input and output some sort of data: be it a multimedia stream, sensor data, or user inputs. In this post I will discuss how to easily make your project Bluetooth enabled. For hobbyists and professionals alike one of the most important aspects of any design is cost; in my own project I had three requirements for a Bluetooth Module, low cost, low size, and very easy to use.

The RN-42 module, which is available at SparkFun electronics and Mouser, is a very cheap and simple way to use Bluetooth in your project. The best part about the RN-42 module is that it has an integrated antennae and contains everything needed for the Bluetooth protocol. You would be surprised how many modules require a special external microcomputer that contains a Bluetooth stack to operate.

For how great the module may seen, there are a multitude of downsides. The first is not such a big deal and might not even be a downside for you. The device is in a surface mount package. For those of you who are not making your own PCBs, you can buy one at my online store and be done with it. For those of you on a budget, it is possible to solder wires onto each pad of the RN-42 even though I would not recommend it. The last, and my favorite solution, is to just make a custom PCB for your project and solder on the module yourself. The picture below is of a custom breakout board I made to evaluate the module. The schematic is shown later in this post.

My custom breakout board for the RN-42.
The other negative aspects of this module are its size and maximum baud rate. The module is a little bigger than I would like, but still reasonably small. The device also draws a bit more power than I would like, but all things considered it is not too bad for being a Bluetooth module. What bothers me the most about this device is that the maximum baud rate is very very low. I will discuss this in more detail after I show you how to integrate this device into your project.

Link Summary

RN-42 at SparkFun
RN-42 Breakout at SparkFun
RN-42 at Mouser
RN-42 Datasheet
Roving Networks User Manual
NJC's Software UART
RealTerm

Adding Bluetooth to your Project

The easiest way to send and receive data using this Bluetooth module is to use a UART connection. The first schematic below, which is based off of the datasheet and example schematic from SparkFun, is how I connected the module to my project.

A complicated example for hooking up the RN-42 to the MSP430F5510.
This shows how to connect the RN-42 to an MSP430. Please note that this is just an example and that it is much more complicated than the bare minimum; see the next schematic for the bare minimum needed. The first and most important thing I want to mention is that the device requires a supply voltage between 3V and 3.6V. Luckily I use 3.3V for most of my projects which is perfect for this module.

My custom breakout board for the RN-42
The figure above shows the schematic for the breakout board which I used for evaluating the device and shows what kind of connections the module needs in order to function properly. After testing the device thoroughly it was obvious to me that the device is so simple to use that I could have put it in my project right away. The range is quite good and the device is very reliable when using a baud rate of 115200.

Connecting

Hopefully by now it is clear that from the hardware side of things only a UART connection is needed between the Bluetooth module and your MSP430. What about from the other side of the data stream? The computer. Linking the module to the computer was surprisingly easy. On Windows 7 (I can’t speak for any other OS or version of Windows), it was plug and play. Within a few seconds the drivers were automatically installed and I could see which COM port the module was connected to in the device manager.

The device manager showing the RN-42 connected as COM6.
As I mentioned before, my terminal program of choice is Realterm. The first test I recommend completing when setting up the module is a simple echo test. Connect the RX and TX pins on the module together, then set the COM port to the correct port and the baud rate to 115200 baud in your terminal. Once everything is set up and the power is on, wait for the device to connect. If you have the status LEDs connected to the module (which are optional if you are worried about power consumption), the LED connected to PIO2 should turn on when the module is paired with the computer. The other status LED will blink if the device is not connected. Once connected, type a few characters in the terminal and you should receive back every character you typed into the terminal.

Note: If you are running off of a battery, which you probably are given that this is a post about Bluetooth, you will run into connection issues when the battery is getting low. The module will still turn on and the LEDs will still blink, but you will not be able to reliably connect. If you are having connection issues, check your battery.

That should be all that you need to know in order to add Bluetooth to your project. If you think there is something I have missed, let me know and I will add a section to this post.

Commands?

Yes, this module can accept commands which allow the user to change some important settings. I will not be discussing these commands in this post. You can find all the information you need on this topic in the Roving Networks User Manual.

Baud Rate Trouble

As I mentioned earlier I want to discuss the problems I have with the data rate of this device. My frustrations really arise from false claims on the specifications of the device from both the manufacturer and vendors. 
  • "Over air data rate of 721kbps to 2.0Mbps" - SparkFun product page
  • "Data Rate: 1200 bps to 921 Kbps" - Mouser
  • "Sustained SPP data rates - 240Kbps (slave), 300Kbps (master)" and "HCI data rates - 1.5Mbps sustained, 3.0Mbps burst in HCI mode" - Roving Networks Datasheet for the RN-42
The quotes above show where my frustrations originate. To be honest, I am not really sure where SparkFun got their numbers; if I am missing something, please point it out to me. To make things clear, this device really only functions in SPP mode, thus you have a max data rate of 240Kbps if the device is in slave mode. I'm not even sure if it's possible to put the device in HCI mode, apparently it must be done at the factory.

So the datasheet is clear about the max speeds in each mode, but I am frustrated that they do not make it abundantly clear that the device cannot work in HCI mode. I emailed Roving Networks inquiring about this dilemma I was having (I wanted to use 921600 baud sustained over the air) and though they were friendly, prompt, and reasonably helpful, I was/am not happy with how misleading their datasheets are.

“The HCI mode is a special build of firmware the needs to be programmed at the factory.” “Please understand that in HCI mode, the bluetooth stack is running on the external host processor and the module is acting as a radio.” All in all, I do commend them on their customer service, just keep in mind that realistically speaking, you will only be able to use SPP mode.

Conclusions

Despite my frustrations, this is a great solution for making your project wireless. I have found very few chips or modules that can be integrated into a project with such ease. I hope this post helps a few people successfully add Bluetooth to their projects. There are so many cool possibilities for projects when using Bluetooth; the possibilities are endless! Who wouldn't want to hook up a project to an Android phone?

Comment away! Let me know what projects you have added Bluetooth to!

19 comments:

  1. These are even cheaper for what it is worth:
    http://www.mdfly.com/index.php?main_page=product_info&cPath=8_47&products_id=63

    And they can be had for even less on eBay. RN ones do have a better AT command set though, and easier to read English documentation.

    ReplyDelete
  2. I read the datasheet straight from Roving Networks, and I did not have any trouble understanding the various capabilities of the various versions of this module. I am not trying to be insulting, but I think perhaps you simply misunderstood the datasheet, and that is not their fault. Sparkfun could have done a better job since they ARE targeting hobbyists who are not necessarily as skilled at deciphering a datasheet as someone like myself who has designed commercial Bluetooth-equipped products for a living. So, I'm not sure it was fair to blame your confusion on RN as they cater to a different crowd than Sparkfun.

    BTW, my name is Don Stratton. I am only posting this "anonymously" because your comment system asks for a name and a URL. Did you mean email address? I would have given one, but only if it were not made public as I have quite enough spam as it is.

    ReplyDelete
  3. @Brian - What kind of data throughput have you been able to get with that device? Very interesting!

    @Don Stratton - As far as the comment system goes, the URL is an optional field and it is for a website if you have one. This allows other users to click on your name and see your website.

    As far as my frustrations go with RN, I am not claiming that there was misinformation on the fact that you need to use the device in HCI mode for higher speeds, but rather that you need an external device for the stack. This is not said anywhere in the datasheet. But regardless, even though I am a professional I am no Bluetooth expert and it is not obvious to me that an HCI stack would need to be run on an external chip.

    It would be nice if companies would write datasheets as if they were writing to an experienced hobbyist. But till that happens, its up to people like you and I to help interpret.

    ReplyDelete
  4. Could u provide a ready to use library for software UART (for all newbies)?

    Something like:
    magic_function("this string will be transmitted over uart");

    I know that u wrote such function, but as an main program - can u pack it into one portable function so all launchpad newbie sould just use it by copying single function and then using single line in main() ?

    ReplyDelete
  5. The range and the baud rate of its bluetooth is quite good and favorable for avid users.

    Distance Sensor

    ReplyDelete
  6. Wow, thank you so much for your contribution! The information you've provided are a brilliant thing! Thanks a million one more time! Everything works like a charm!

    ReplyDelete
  7. Its a good project and will definitely try this. Sometimes the above mentioned hardware is not available so it becomes a tough task to implement this.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Greetings, I have the next question. There is support when I connect rx tx launchpad cable to the computer or a bluetooth module? Thanks in advance. Leonardo Cruz.

    ReplyDelete
  10. Leonardo: This module can be connected to the computer. All you need to do is connect the RX and TX pins correctly to the Launchpad, and use a terminal program on the computer. I recommend Realterm. I personally do not provide support from a software standpoint on the computer, but this device works great with a computer. I hope that answered your question.

    ReplyDelete
  11. Hi Nicolas,

    I have a question to you, I am using MSP430G2231 controller to simulate UART with baud rate 9600 to interface to RN-42, I keep sending a pattern of 0xA5 on the Tx pin and I have checked with my scope the timing and it looks fine, but when I tried to connect it to RN-42 RX pin, it looks like it is not recognized by it, and the LED indicating there something transmitted over Bluetooth(RED LED) is not flashing.

    I am not sure what is the problem here, and I am not sure is the baud rate is the problem.

    Appreciating your help, thanks in advance.

    Best Regards,
    Ahmed

    ReplyDelete
  12. Ahmed: There are two things you should try out. The first is to short the RX and TX pins on the Bluetooth module and make sure that you are receiving what you transmit from your computer. Next, you might have to change the baud rate of the RN-42 module. This is done by entering the RN-42's settings menu through your computer's terminal. Take a look at the datasheet, it has all the information you need.

    ReplyDelete
  13. A very wonderful site about the Texas MSP430 Launchpad, beginners shifting from basic MCU's to Texaslaunchpad like me will get benefit from this blog.
    http://www.npeducations.com

    ReplyDelete
  14. Can i connect this MSP430 interfaced with RN-42 bluetooth to an Android device and is it possible to communicate between android device and MSP430 over bluetooth to control MSP430 for other applications(like DC motor).

    ReplyDelete
  15. Prashant: I believe it is possible, although I have never tried to do so. Please see the following link. http://www.circuitsathome.com/diy-2/testing-rn-42-bluetooth-module-using-android-phone. Additionally, I"m sure there is a vast amount of information on using the RN-42 module and an Arduino with an Android phone. Use these as references. Hope that answers your question!

    ReplyDelete
  16. Hi NJC, Thanks for writing this up. I can transmit data but the board doesn't seem to be able to receive. This is function for receiving http://pastebin.com/p6RgiJeY

    ((IFG2&0x01)==0) never goes to '1', on CCS I watch the code just get there and wait. This is my full code BTW: http://pastebin.com/Mkq6J4Lu

    Also I can transmit if I comment out the part that receives and just do OUTA_UART('C'); or something.

    Is there something that I am doing wrong? The Bluetooth is set for 9600,N,1 like the board.

    Thanks.

    ReplyDelete
    Replies
    1. Hi Mike. My first suggestion would be to make sure your code works without the Bluetooth board. Connecting to the computer via USB will make debugging easier and help you ensure your code works before going to Bluetooth.

      To help you further, I have a question about your code. What are you trying to accomplish with the USCI0RX_ISR(void) interrupt? It seems as if you are trying to send '!' to the computer every time you receive a character, is this correct?

      If you have trouble getting your code to work, I would recommend posting specific coding questions on http://forum.43oh.com/. Additionally, the example code that TI provides is VERY helpful.

      I hope that helps!

      Delete
    2. Thanks for the reply.

      the USCI0RX_ISR(void) interrupt is me trying my hand in interrupts because eventually I would like to use them. I originally had that commented out. I got that from example code I found on E2E. The full code is http://pastebin.com/TH1avecG. That code works when connected to the Launchpad through TeraTerm but not when connected to Bluetooth through TeraTerm.

      I definitely need to focus on one thing at a time and I will use those forums. Thank you!

      Delete
    3. Glad to help! If the code works fine when not using the Bluetooth, everything should be ok. The default baud rate for the Bluetooth module is not 9600, that might be what is causing you some problems. If I remember correctly, the default baud rate is 115200. If you prefer to use 9600, you can change the baud rate of the device. For instructions on how to do this see the device's documentation.

      Hope that works out for you.

      Delete