Adam Watkins, Austin Kwong

Adam Watkins, Austin Kwong

Friday, December 13, 2013

Bluetooth + Arduino Tutorial

  1. Connect the Bluetooth shield’s power, ground, TX and RX pins to their respective pins on the Arduino Uno
  2. The following code is used for sending a character over a Bluetooth connection and receiving the same character in the Arduino serial monitor.

#include <SoftwareSerial.h> //this library is neccessary for serial connections

int rx_pin = 6; // set digital pin 6 to be receiving pin

// can be any digital pin

int tx_pin = 7; // set digital pin 7 to be transmitting pin

char cBuffer; // this is where we are going to store the received character

SoftwareSerial Bt(rx_pin,tx_pin); //creates SoftwareSerial object

// on the selected pins

void setup(){

Serial.begin(9600); // this is a connection between the arduino and

// the pc via USB

pinMode(rx_pin, INPUT); // receiving pin as INPUT

pinMode(tx_pin, OUTPUT); // transmitting pin as OUTPUT

bluetoothInitiate(); // this function will initiate our bluetooth (next section)

}

void loop(){

if(Bt.available()){ // this will check if there is anything being

// sent to the Bluetooth from another device

cBuffer = Bt.read(); // this will save anything that is being sent to the Bluetooth

Serial.print(cBuffer); // this will print to the local serial (tools->serial monitor)

}

// this is for recieving on the device with bluetooth, now we can make it send stuff too!

if(Serial.available()){ // this will check if any data is sent

// from the local terminal

cBuffer = Serial.read(); // get what the terminal sent

Bt.print(cBuffer); // and now send it to the master device

}

}

void bluetoothInitiate(){

Bt.begin(9600); // this sets the the module to run at the default bound rate

Bt.print("\r\n+STWMOD = 0\r\n"); //set the bluetooth work in slave mode

Bt.print("\r\n+STNA=SeeedBTSlave\r\n"); //set the bluetooth name as "SeeedBTSlave"

Bt.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me

Bt.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here

delay(2000); // This delay is required.

Bt.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable

delay(2000); // This delay is required.

Bt.flush();

Bt.print("Bluetooth connection established correctly!"); // if connection is successful then print to the master device

}

  1. Upload the code above onto your Arduino device, keep the Arduino IDE open.
  2. Download PUTTY from http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html and install. This allows you to communicate from your desktop to your Bluetooth device.
  3. Connect to your Bluetooth device from your computer. This is usually done by right clicking on the Bluetooth icon in your system tray and choosing “Add new device”.
  4. Our device is named “linvor” and the connection code is 1234

C Users 767525 Downloads Device and printers pngC Users 767525 Downloads Properties png

  1. Determine which COM port your Bluetooth device is connected to. This is done by first going to “Devices and Printers” from the start menu and right clicking on your Bluetooth device(in this case, “linvor”) and selecting properties
  2. Under the hardware tab, it should say COM# (i.e. For this example, it was connected to COM5). Write it down.
  3. Open PUTTY. Set it up according to this picture. In the serial line, put in what you wrote down from the previous step. In this example, it is COM5.
  4. For speed, we are using 9600.
  5. Click open, it should open a new command window.F Austin the silly goose PUTTY png
  6. Open your Arduino Serial Monitor F Austin the silly goose Bluetooth Tutorial Serial Monitor png
  7. You should receive the following message in your command window "Bluetooth connection established correctly!"F Austin the silly goose Command Window png
  8. If you received that message, you should be able to type any character into the command window and receive the same character back through your Arduino in the Arduino Serial Monitor!