January 25, 2013

Bluetooth with Arduino

I recently purchased a low cost Bluetooth module with serial interface. I decided to hook it up to my Arduino.

Arduino UNO does only have one UART that is used for the USB serial terminal so it can't be used but there is a Software UART available that uses regular I/O-pins. For details see:
http://arduino.cc/en/Reference/SoftwareSerial

It works well at 9600 baud but when i tried 115200 the reception failed and received characters was corrupted. This worked in my previous test both with an FTDI cable and a Bus Priate.

The baud rate of the module can be changed by sending a AT command before connecting to the module to a Bluetooth device. The command is:

AT+BAUD<index>

Where <index> is an hexadecimal number from 1 to C. The rates are: 1:1200, 2:2400, 3:4800, 4:9600, 5:19200, 6:38400, 7:57600, 8:115200, 9:230400, A:460800, B:921600, C:138240

WARNING: Do NOT set it higher than your device is capable of since you will need to communicate at the new rate after sending this command. I used an FTDI cable for changing back the configuration. 

The board is marked "JY-MCU BT_BOARD v1.03". The supply voltage is stated to 3.6 - 6 V on the board. But the RX and TX terminals are 3.3 Volt logic. Therefore a voltage divider as shown in the figure below is need for the TX output on the Arduino when connecting it to RX on the module. The TX on the module can be connected directly to the RX on the Arduino. I have put the two resistors on a veroboard.The values are R1=10k and R2=20k.

Below is the Arduino code I used for the tests. It transmits received Bluetooth data to the USB terminal and vice versa.


#include <SoftwareSerial.h>
const int RxPin = 10;
const int TxPin = 11;

SoftwareSerial Bluetooth(RxPin, TxPin); // RX, TX

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  Serial.println("Init done!");
  // set the data rate for the SoftwareSerial port
  Bluetooth.begin(9600);
}

void loop() // run over and over
{
  int data;

  if ((data = Bluetooth.read()) != -1)
   Serial.write(data);


  if ((data = Serial.read()) != -1)
   Bluetooth.write(data);
}



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.