Play Bluetooth With Arduino

We have provide an inexpensive serial port Bluetooth module , it can work as a Bluetooth slave and communicate with a Bluetooth master like PC and mobile phone.

Arduino is a  very popular development platform of AVR, so we choose it to work with this Bluetooth module and this combination can generate a lot of applications.

First , put the BT shield stand on Arduino board – the BT shield has a serial port Bluetooth module (Slave) on it. The BT shield have 3 pins connect with Arduino board : State , RX and TX. With the State LED and State pin, we can get the connection status of the BT shield , when the Bluetooth serial port channel is not build , the State LED will alternating light and off , and if the serial port is opened , the State LED will be on. The State pin will be the same mode as the LED .

You can get more information about the BT Shield in the product page , or download the datasheet here.

Here we make a example how to use the Arduino with the Bluetooth module to send data to PC.

In this code , we use the interrupt to check the State pin, if the state pin level from high to low , then we consider that the module still in the standby mode, so wait for connecting. If the State pin maintain more than one second of high level, we consider that the Bluetooth channel has build and we can sent the data to serial port.

Download the code below into the Arduino:

unsigned int timeout=0;
unsigned char state=0;
 
ISR(TIMER2_OVF_vect)          //Timer2  Service 
{ 
  TCNT2 = 0;
  timeout++;
  if (timeout>61)
  {
    state=1;
    timeout=0;
  }
 
}
 
void init_timer2(void)               
{
  TCCR2A |= (1 << WGM21) | (1 << WGM20);   
  TCCR2B |= 0x07;   // by clk/1024
  ASSR |= (0&lt;<as2); -="" 0:="" 1:="" arduino="" case="" clock="" enable="" external="" hellow="" in="" internal="" interrupt="" not="" overflow="" pre="" state="0;" tcnt2="0;" timeout="0;" timer2="" timsk2="" use="" used="" void=""><p>
	Reset the Arduino , we can see the PWR LED of the BT Shield is on , and the State LED is light and off , the D13 LED of Arduino board is off , that means the Bluetooth connection is not complete. Open the hardware manager , find the Bluetooth mode and connect it , the Pincode is 1234 as default . After install the driver , we can find a Bluetooth serial port in the PC , and use a serial monitor software to open it , the State LED on the BT Shield is continued on , and the D13 LED of the Arduino board will light ,too.It means the connection is build and we can communicate with PC now.</p>
<p>
	On the serial monitor on PC , we can see the data that send from Arduino.</p>
<p>
	<img alt="" src="http://blog.iteadstudio.com/wp-content/uploads/image/2010_06/play-bluetooth-with-arduino_3.jpg" style="width: 400px; height: 299px; " /></p>
<p>
	This just a opening , with this demo , we can use these module in many places . How about sending the sensor data to a software on PC or Phone.</p>
<p>
	The next step , we are going to make a Wireless Energy Monitor that base on <a href="http://iteadstudio.com/store/index.php?main_page=product_info&amp;cPath=4&amp;products_id=56" target="_blank">Non-invasive AC current sensor</a>. There is a open source <a href="http://openenergymonitor.org/emon/node/28" target="_blank">project</a> but it&rsquo;s use the USB serial port and we will use Bluetooth , and we will make a sample software to calculate the Energy.</p>
</as2);>

Responses to “Play Bluetooth With Arduino

  1. Could you explain this code and give example how and when read data from bluetooth?
    Is it possible to switch between USB and bluetooth on Andruino UNO?
    Hardware is great, but please provide more documentation..

  2. Hello,
    I’ve recently bought a Bluetooth shield 2.1 from your store. Following the code that you provide I could send data from the arduino to a PC flawlessly.
    However now I would like to send data from a PC to an arduino. The code I use is the same for reading from a serial input:
    if (Serial.available() > 0) {
    // get incoming byte:
    echo = Serial.read();
    Serial.print(echo);
    Serial.print(“\n”);
    delay(100);
    }
    With this code I’m not able to receive anything. Is there something I’m missing?
    Thank you very much.

      • As I told you in a mail, after a long research I noticed that it is needed a pull down resistor on pin 0 to make it work.
        Now it works flawlessly without any modification in the code.
        Without this pull down resistor at pin 0 the arduino can not read the data received through the BT shield.

  3. Hi,
    I’ve recently bought a Bluetooth shield 2.1, and I’m also having problem to send and receive data from it.
    Just to start I’m using the default example code. However, I’m not able to send or receive data when connected thru the bluetooth serial.
    I’m using linux and connecting with the commands:
    hcitool scan
    sdptool browse XX:XX:XX:XX:XX:XX
    rfcomm connect 0 XX:XX:XX:XX:XX:XX
    And it is working, that part i’m sure. However, the arduino is not receiving.
    Could some one provide a better example with the sending and receiving data.
    PS: Thanks for the pulldown-pin-0-resistor tip Sergio, but it didn’t lead me anywhere

  4. @Andrew
    I did all the experiments in Windows 7 but:
    First of all, connect the Arduino with the BT shield to an USB and with, the switch in FT position, try to send some commands through the serial console in Arduino IDE to the Arduino COM port. This should be doable and you should be able to configure the baud rate, pin code, etc.
    for testing I used the following code:
    if (Serial.available() > 0) {
    // get incoming byte:
    echo = Serial.read();
    Serial.print(echo);
    Serial.print(“\n”);
    delay(100);
    }
    I used a 2$ bluetooth usb (from dealextreme) and installed the driver to make it a virtual COM (COM4 in my case). Then, I paired the devices using the default Windows configuration form. Then, in the Arduino IDE, changed the communication port to the virtual COM and used the serial console to send data. You will notice that the LED from the BT shield stop blinking whenever the BT shield gets paired.
    Remember the pulldown-pin-0-resistor thing.

  5. Hello,
    I asume that Open the hardware manager , find the Bluetooth mode and connect it , you mean the windows system/hardware/manager? But still I can’t find that. You write on your site After install the driver where cane I find that driver?
    I work with win7 and I can’t find signals from the bleutooth on my Arduino. I use your sketch on serialport com4. No response on command AT in the serialport command venster.
    I read something about a pulldown resistor. Is that a bug?

  6. @P.Hogenstijn: Don’t know if the pull down resistor think is just my board (itead haven’t said anything) or it is an error in the design. But yes, I had to pull down the pin 0 (RX) in order to be able to receive anything.

  7. Hi Sergio!!
    I have read your code and I think that with this code you will send data to the serial port of Arduino. With you code I managed to receive the commands from the PC and send them to the BT COM port.
    I guess that you want to do it the other around. Send data from the BT and process it at the Arduino. Am I right?
    Maybe we should that by email…

  8. Hi Sergio, The program works fine with the FT232 but not with the bluetooth. How exactly did you wire the pull down resistor and which value of a resistor did you use? I have the same problem as you described and I quickly wired the pin 0 from the arduino to 5V and it was finally reading data from bluetooth. Definitely the guys from Itead forgot about that little detail.

  9. Hi, I bought 2 of these modules. Followed the instructions on this page, but the led keeps blinking (“like is not connected to the PC”) and no data is sent or received. I am using Windows 7, I am able to see the bluetooth module, paired it , but no connection.
    I tried different baud rates by configuring the ports and still no joy. Please help.

  10. Hello,
    I own a BT shield v2.1 and I try to make it work with an Arduino Uno.
    The jumpers are set “To board” and “5V”.
    I use a bluetooth dongle to establish the communication between my PC and the BT shield.
    The devices are paired correctly as the D1 LED is fixed and the demo code works fine: I can send strings from the BT shield to my PC with my terminal software.
    However, it is impossible in the other way: whatever I type in the terminal software, nothing arrive to the arduino UART.
    Is there something to do with the pull-down resistor (which value?). I tried with a 68k but it doesn’t solve the problem.
    Thank you for your help.

  11. @Manu:
    I answer myself: BT module receives data if I put the jumper on “3.3V”.
    However, it means that the BT module has 5V on its pins which is higher than recommended in the datasheet.
    Is there a risk to damage the module with 5V on the Rx input pin or will I need to change the resistors value of the voltage divider (R3 and R4)?
    Thank you for your reply,

  12. Hello! i bought the BT shield V2.2. I was trying to send some AT commands just to see the response, but it doesnt work. The status LED blinks 1time/2s when its supposed to blink 2times/1s when connected to the serial port. Is there something I am not doing??

    • @Pablo Hi, have you read the datasheet of this shield and make sure the shield work in the command mode but in data mode, as your description you just in data mode so of course you can not use the AT command, would you like to read the datasheet more carefully and if there still any problem, you can contact support@iteadstudio.com for more help.

  13. Hello, I have bought Iteaduino BT v1.0 (with ATMEGA328P), I have several examples and Try walking,
    I have a problem of non nivreau bleutooth in fel i can has detected the bleutooth my card BT v1. 0 (with ATMEGA328P) BUT IN laptops in My Computer and the level of COM 4 (c serial port is used) I Get No Bluetooth addresses of mobile phones, but so common and Display COM 4 on the address bluetooth??? That one day was not well configure the card Iteaduino BT v1.0 (with ATMEGA328P)?? Well I do ou DOI non java code android e Who Allows Display the Address bleutooth
    thank you

  14. Good luck getting support from these people. I’ve tried through two different means.
    Thanks @sergio for the pulldown resistor idea. That’s what it ended up taking to get this board working for me. For anyone else running into problems reliably connecting to the board, two things.
    1. You can’t have the board connected to the Arduino while trying to upload. Since it uses pins 0 and 1, it interferes with your computer connecting to the Arduino through the USB.
    2. A 10k resistor connected from pin 0 to ground helps pull the signal low when the board isn’t driving it high. This made the difference between banging my head on the desk and having a working connection.

    • Thanks Sandy. This product review on the store page was helpful to me:
      You definitely need a tutorial on how to set this up.
      To get into AT command mode, set the switches to “FT323″ and “5V”
      Plug you shield into an Arduino
      Plug the Arduino into your computer via USB
      Upload a blank sketch to your Arudino (to make sure it’s not transmitting serial message)
      Open a serial terminal program (My favorite is CoolTerm for OSX)
      Make sure you have the options set to 9600 Baud, data bits 8, parity none, stop bits 1
      Open a port to the Arduino (so it would be your USB connection, NOT bluetooth)
      You should be able to type “AT” now and see “OK” as a response
      The rest you should be able to figure out from the datasheet
      Once you’ve programmed your AT commands, you want to set switch to “To Broad” (This is a typo, should say board)
      Date Added: 04/06/2011 by Michael Doherty

      Switching to FT to upload program allows me to add the program without error AND have the shield attached. I have paired the computer by inputting the pin code but still have not achieved a solid state LED. I’ll try the resistor and see if I can make a connection.

  15. How can i send the byte data from arduino to android?? I have done to do the communication from android to arduino.

    THank you

  16. Hello, the code works properly well.
    I’m finding it extremely difficult to send EMG generated signals via my ITEADSTUDIO bt shield v2.1 to my PC using the ELECTRICGURU software as the simulator. Though it works properly fine if I should use USB. This is the code
    [code]/**********************************************************/
    /* Demo program for: */
    /* Board: SHIELD-EKG/EMG + Olimexino328 */
    /* Manufacture: OLIMEX */
    /* COPYRIGHT (C) 2012 */
    /* Designed by: Penko Todorov Bozhkov */
    /* Module Name: Sketch */
    /* File Name: ShieldEkgEmgDemo.ino */
    /* Revision: Rev.A */
    /* -> Added is suppport for all Arduino boards. */
    /* This code could be recompiled for all of them! */
    /* Date: 19.12.2012 */
    /* Built with Arduino C/C++ Compiler, version: 1.0.3 */
    /**********************************************************/
    /**********************************************************
    Purpose of this programme is to give you an easy way to
    connect Olimexino328 to ElectricGuru(TM), see:
    https://www.olimex.com/Products/EEG/OpenEEG/EEG-SMT/resources/ElecGuru40.zip
    where you’ll be able to observe yours own EKG or EMG signal.
    It is based on:
    ***********************************************************
    * ModularEEG firmware for one-way transmission, v0.5.4-p2
    * Copyright (c) 2002-2003, Joerg Hansmann, Jim Peters, Andreas Robinson
    * License: GNU General Public License (GPL) v2
    ***********************************************************
    For proper communication packet format given below have to be supported:
    ///////////////////////////////////////////////
    ////////// Packet Format Version 2 ////////////
    ///////////////////////////////////////////////
    // 17-byte packets are transmitted from Olimexino328 at 256Hz,
    // using 1 start bit, 8 data bits, 1 stop bit, no parity, 57600 bits per second.

    // Minimial transmission speed is 256Hz * sizeof(Olimexino328_packet) * 10 = 43520 bps.

    struct Olimexino328_packet
    {
    uint8_t sync0; // = 0xa5
    uint8_t sync1; // = 0x5a
    uint8_t version; // = 2 (packet version)
    uint8_t count; // packet counter. Increases by 1 each packet.
    uint16_t data[6]; // 10-bit sample (= 0 – 1023) in big endian (Motorola) format.
    uint8_t switches; // State of PD5 to PD2, in bits 3 to 0.
    };
    */
    /**********************************************************/
    #include
    #include
    //http://www.arduino.cc/playground/Main/FlexiTimer2

    // All definitions
    #define NUMCHANNELS 6
    #define HEADERLEN 4
    #define PACKETLEN (NUMCHANNELS * 2 + HEADERLEN + 1)
    #define SAMPFREQ 256 // ADC sampling rate 256
    #define TIMER2VAL (1024/(SAMPFREQ)) // Set 256Hz sampling frequency
    #define LED1 13
    #define CAL_SIG 9

    // Global constants and variables
    volatile unsigned char TXBuf[PACKETLEN]; //The transmission packet
    volatile unsigned char TXIndex; //Next byte to write in the transmission packet.
    volatile unsigned char CurrentCh; //Current channel being sampled.
    volatile unsigned char counter = 0; //Additional divider used to generate CAL_SIG
    volatile unsigned int ADC_Value = 0; //ADC current value

    //~~~~~~~~~~
    // Functions
    //~~~~~~~~~~

    /****************************************************/
    /* Function name: Toggle_LED1 */
    /* Parameters */
    /* Input : No */
    /* Output : No */
    /* Action: Switches-over LED1. */
    /****************************************************/
    void Toggle_LED1(void)
    {

    if((digitalRead(LED1))==HIGH){ digitalWrite(LED1,LOW); }
    else{ digitalWrite(LED1,HIGH); }

    }

    /****************************************************/
    /* Function name: toggle_GAL_SIG */
    /* Parameters */
    /* Input : No */
    /* Output : No */
    /* Action: Switches-over GAL_SIG. */
    /****************************************************/
    void toggle_GAL_SIG(void)
    {

    if(digitalRead(CAL_SIG) == HIGH){ digitalWrite(CAL_SIG, LOW);
    }
    else{ digitalWrite(CAL_SIG, HIGH);
    }

    }

    /****************************************************/
    /* Function name: setup */
    /* Parameters */
    /* Input : No */
    /* Output : No */
    /* Action: Initializes all peripherals */
    /****************************************************/
    void setup()
    {

    noInterrupts(); // Disable all interrupts before initialization

    // LED1
    pinMode(LED1, OUTPUT); //Setup LED1 direction
    digitalWrite(LED1,LOW); //Setup LED1 state
    pinMode(CAL_SIG, OUTPUT);

    //Write packet header and footer
    TXBuf[0] = 0xa5; //Sync 0
    TXBuf[1] = 0x5a; //Sync 1
    TXBuf[2] = 2; //Protocol version
    TXBuf[3] = 0; //Packet counter
    TXBuf[4] = 0x02; //CH1 High Byte
    TXBuf[5] = 0x00; //CH1 Low Byte
    TXBuf[6] = 0x02; //CH2 High Byte
    TXBuf[7] = 0x00; //CH2 Low Byte
    TXBuf[8] = 0x02; //CH3 High Byte
    TXBuf[9] = 0x00; //CH3 Low Byte
    TXBuf[10] = 0x02; //CH4 High Byte
    TXBuf[11] = 0x00; //CH4 Low Byte
    TXBuf[12] = 0x02; //CH5 High Byte
    TXBuf[13] = 0x00; //CH5 Low Byte
    TXBuf[14] = 0x02; //CH6 High Byte
    TXBuf[15] = 0x00; //CH6 Low Byte
    TXBuf[2 * NUMCHANNELS + HEADERLEN] = 0x01; // Switches state

    // Timer2
    // Timer2 is used to setup the analag channels sampling frequency and packet update.
    // Whenever interrupt occures, the current read packet is sent to the PC
    // In addition the CAL_SIG is generated as well, so Timer1 is not required in this case!
    FlexiTimer2::set(TIMER2VAL, Timer2_Overflow_ISR);
    FlexiTimer2::start();

    // Serial Port
    Serial.begin(57600);
    //Set speed to 57600 bps

    // MCU sleep mode = idle.
    //outb(MCUCR,(inp(MCUCR) | (1<<SE)) & (~(1<<SM0) | ~(1<<SM1) | ~(1<<SM2)));

    interrupts(); // Enable all interrupts after initialization has been completed
    }

    /****************************************************/
    /* Function name: Timer2_Overflow_ISR */
    /* Parameters */
    /* Input : No */
    /* Output : No */
    /* Action: Determines ADC sampling frequency. */
    /****************************************************/
    void Timer2_Overflow_ISR()
    {
    // Toggle LED1 with ADC sampling frequency /2
    Toggle_LED1();

    //Read the 6 ADC inputs and store current values in Packet
    for(CurrentCh=0;CurrentCh> 8)); // Write High Byte
    TXBuf[((2*CurrentCh) + HEADERLEN + 1)] = ((unsigned char)(ADC_Value & 0x00FF)); // Write Low Byte
    }

    // Send Packet
    for(TXIndex=0;TXIndexToggle frequency
    counter = 0;
    toggle_GAL_SIG(); // Generate CAL signal with frequ ~10Hz
    }
    }

    /****************************************************/
    /* Function name: loop */
    /* Parameters */
    /* Input : No */
    /* Output : No */
    /* Action: Puts MCU into sleep mode. */
    /****************************************************/
    void loop()
    {

    __asm__ __volatile__ (“sleep”);

    }[code]

    I’m using OLIMEX Shield Ekg-Emg as the EMG generator

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>