Phone and Arduino Bluetooth Communication

From ElectroDragon
Jump to navigation Jump to search

We have seen some nice arduino board with embedded Bluetooth functions, now we make a simple tutorial about how to use our LC-05 Module directly with phone.

What you will need

Bluetooth comuunication.JPG
  • Our LC-05 Module
  • Your cell phone, if you use android, install the bluetooth UART communication software like BluetoothSPP.
  • Your arduino board.

Make the connection like below

Bluetooth Module Arduino Description
3.3V 3.3V -
5V - Do not connect, it will not burn the BT module, but will burn other side, which could be UART connector (like pl2303 module), arduino, etc
GND GND -
RX D3 -
TX D2 -
KEY - No need to connect

How it work

To illustrate the connection between the Arduino the tablet we will be sending simple text messages back and forth between them. However, since the Arduino doesn’t really have a keyboard, we are going to plug the arduino into our pc and talk to it via a serial connection using usb.

The arduino code is fairly simple. In a nutshell is opens a serial connection to the computer, and a serial connection to the bluetooth mate. Any data received on one is written to the other and vice versa. Since we are going to be using the Arduino’s regular RX and TX lines for talking to the PC, we are going to have to use a software based serial connection on two other digital pins using the NewSoftSerial Arduino library which can be downloaded here. Just copy the folder in the zip to the libraries folder under your Arduno folder (for me it is at C:\Program Files\arduino-1.0\libraries)

Demo code

#include <SoftwareSerial.h>  

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps
  bluetooth.begin(9600);  // Start bluetooth serial at 9600
}

void loop()
{
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    // Send any characters the bluetooth prints to the serial monitor
    Serial.print((char)bluetooth.read());  
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
    // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print((char)Serial.read());
  }
  // and loop forever and ever!
}

Note

The following part is not necessary for our module, already removed from demo code:

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$$$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Temporarily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably

Replace to this code to read value and print on serial monitor

#include <SoftwareSerial.h>
char ReadFromBT;

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    ReadFromBT = mySerial.read();
    Serial.write(ReadFromBT);
    delay(500);
    
  if (Serial.available())
    mySerial.write(Serial.read());
}

Result

That takes care of the Arduino side of things. The Android bits are little bit tougher.

The Android project we are going to write is going to have to do a few things:

  1. Open a bluetooth connection
  2. Send data
  3. Listen for incoming data
  4. Close the connection

Documents