Tuesday, May 28, 2013

Arduino - Bluetooth communication

Wireless communication with PC and Arduino board using bluetooth

By Mitchell Page Mpag5967 [at] mail.usyd.edu.au Key Centre of Design Computing and Cognition University of Sydney, Australia
>> back to tutorials page

PLEASE NOTE: this tutorial was written for Wiring but can easily be translated to arduino, just remember the rx and tx pins in arduino are 0 and 1 respectively.

See also this tutorial for using the BlueSmirf bluetooth device with Arduino.

Normally you would use a serial USB cable connected from your computer to your Wiring board for receiving data sent from a computer running processing to your board. This tutorial demonstrates how to execute this same process, but wirelessly using Bluetooth. We will test this by being able to turn an LED on/off wirelessly. You will need to use an adaptor to power your board for this tutorial. This tutorial covers communication from the computer to the board; not from the board to the computer.

Equipment:

You will need a Bluetooth Dongle for you computer and Bluetooth Modem for connecting to Wiring board. We specifically used the following (http://www.sparkfun.com/commerce/product_info.php?products_id=150 and http://www.sparkfun.com/commerce/product_info.php?products_id=158). This tutorial was developed for a computer running Windows XP Service Pack 2 (which has included Bluetooth support). It has not been tested on Macs or Linux.

Process:

Step 1. We firstly need to install the Bluetooth module onto the computer and install the drivers. If the module came with a CD, put this into your CD-ROM drive and then plug the module into one of your spare USB ports. Windows should automatically detect the device and if it asks for drivers, point to the CD-ROM drive. Once the drivers have been installed, Windows may need to reboot.

Step 2. Once your computer has rebooted, plug your Bluetooth module in (if it isn't already). A Bluetooth icon should appear in your taskbar (Windows XP SP2 has this Bluetooth support feature). Your Bluetooth module is now set up and running as a serial port. Now we need to set up the modem on the wiring board.

Step 3. We are going to create the setup in Figure 1. First of all connect your LED to your wiring board on whichever pin you want EXCEPT pins 2 or 3 (we have used Pin 8 in this tutorial). See this example for how to connect an LED (http://www.wiring.org.co/learning/examples/led_blinks.html). Once you have connected your LED to the board it is time to connect your Bluetooth modem. The Bluetooth modem we are dealing with has 4 labeled pins, PWR, GND, Rx and Tx. It is pretty straight forward: connect the PWR pin to the 5V pin on Wiring, connect the GND pin to the GROUND pin on wiring. Then connect the Rx pin to the Tx pin of Wiring (digital pin 3) and connect the Tx pin to the Rx pin of Wiring (digital pin 2). Once it is connected then plug your AC adaptor into the board, if everything is connected right a small green LED will start blinking on the Bluetooth modem.

Step 4. Now we need to set up a connection from the computer to the Bluetooth modem. With your Bluetooth module plugged in, double click the Bluetooth icon in the task bar and select �ADD� a device. It should find a device called BlueRadios � which is your modem. Select to connect to it. It will ask you for a passkey. The passkey for the Bluetooth modem we used is �default� (see http://www.sparkfun.com/datasheets/RF/BlueSMiRF_v1.pdf). If you used a different Bluetooth modem to the one we used, you will need to consult the product documentation to find the passkey. Once you have entered that passkey your computer will now remember that Bluetooth modem and be able to communicate with it now and in the future.

Step 5. We need to now find out which COM port our Bluetooth communication is on. Double click on the Bluetooth icon in the taskbar again and select the BlueRadios device from the devices tab, then click the COM Ports tab and take note of the port number the Bluetooth connection is using. In our case, the COM port is COM7

Step 6. Now that you have connected all your components, you need to upload your program to the Wiring board. YOU NEED TO USE A SERIAL CABLE TO UPLOAD YOUR PROGRAM � YOU CANNOT USE BLUETOOTH WIRELESS TO UPLOAD PROGRAMS TO THE BOARD. YOU ALSO NEED TO DISCONNECT THE RX AND TX WIRES FROM THE BLUETOOTH MODEM WHILE YOU UPLOAD. The code for the program we use is attached below. Once you have connected the serial USB cable, upload your program. Once it has uploaded successfully, reset your board and remove the cable.

Step 7. Now we need to configure the processing code. The code we used is attached below. You need to find out which number in the array the Bluetooth COM port is. Run the code once and take not of the COM ports which appear in the output pane. Eg. COM1 COM3 COM7 In our case, the Bluetooth was connected to COM7, which is the number 2 position in our serial array (COM1 = 0, COM3 = 1, COM7 = 2). So we update the processing code to the following so it will use the Bluetooth connection to send serial data. Once you have updated this code, run the program again (see Step 8).

Step 8. After you first run the program, the computer will establish a connection with the Bluetooth modems � you will see a red light begin to blink on the modem � it will finally stay red once it has established a connection but may take about a further 10 seconds before it can receive data. The connection process will take about 15 seconds all up. After about 15 to 20 seconds, your processing program will now be connected to the Wiring board wirelessy. Now press the buttons on the processing interface and watch the LED turn on and off.


Code:

Wiring code � this code will wait for signals from processing. When it receives a H signal, the LED on pin 8 will turn on. When it receives an L signal the LED on pin 8 will turn off.

Wiring code

char val; // variable to receive data from the serial port
int ledpin = 8; // LED connected to pin 48 (on-board LED)

void setup() {

  pinMode(ledpin, OUTPUT);  // pin 48 (on-board LED) as OUTPUT
  Serial.begin(9600);       // start serial communication at 9600bps

}

void loop() {

  if( Serial.available() )       // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'
  }
  if( val == 'H' )               // if 'H' was received
  {
    digitalWrite(ledpin, HIGH);  // turn ON the LED
  } else {
    digitalWrite(ledpin, LOW);   // otherwise turn it OFF
  }
  delay(100);                    // wait 100ms for next reading

}

Processing code � this code creates two buttons. When the left button is clicked it will send an H signal to the board and turn the LED on. Or if the right button is clicked it will send an L signal to Wiring board to turn the LED off.

Processing code

//import class to set up serial connection with wiring board
import processing.serial.*;

Serial port;

//button setup
color currentcolor;
RectButton rect1, rect2;
boolean locked = false;
void setup() {

  //set up window
  size(200, 200);
  color baseColor = color(102, 102, 102);
  currentcolor = baseColor;

  // List all the available serial ports in the output pane.
  // You will need to choose the port that the Wiring board is
  // connected to from this list. The first port in the list is
  // port #0 and the third port in the list is port #2.
  println(Serial.list());

  // Open the port that the Wiring board is connected to (in this case 1
  // which is the second open port in the array)
  // Make sure to open the port at the same speed Wiring is using (9600bps)
  port = new Serial(this, Serial.list()[2], 9600);

  // Define and create rectangle button #1
  int x = 30;
  int y = 100;
  int size = 50;
  color buttoncolor = color(153, 102, 102);
  color highlight = color(102, 51, 51);
  rect1 = new RectButton(x, y, size, buttoncolor, highlight);

  // Define and create rectangle button #2
  x = 90;
  y = 100;
  size = 50;
  buttoncolor = color(153, 153, 153);
  highlight = color(102, 102, 102);
  rect2 = new RectButton(x, y, size, buttoncolor, highlight);

}

void draw() {

  background(currentcolor);
  stroke(255);
  update(mouseX, mouseY);
  rect1.display();
  rect2.display();

}

void update(int x, int y) {

  if(locked == false) {

    rect1.update();
    rect2.update();
  } else {
    locked = false;
  }

  //Turn LED on and off if buttons pressed where
  //H = on (high) and L = off (low)
  if(mousePressed) {
    if(rect1.pressed()) {            //ON button
      currentcolor = rect1.basecolor;
      port.write('H');
    } else if(rect2.pressed()) {    //OFF button
      currentcolor = rect2.basecolor;
      port.write('L');
    }
  }

}

class Button {

  int x, y;
  int size;
  color basecolor, highlightcolor;
  color currentcolor;
  boolean over = false;
  boolean pressed = false;  

  void update()
  {
    if(over()) {
      currentcolor = highlightcolor;
    } else {
      currentcolor = basecolor;
    }
  }

  boolean pressed()
  {
    if(over) {
      locked = true;
      return true;
    } else {
      locked = false;
      return false;
    }   
  }

  boolean over()
  {
    return true;
  }

  void display()
  {

  }

}

class RectButton extends Button {

  RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
  {
    x = ix;
    y = iy;
    size = isize;
    basecolor = icolor;
    highlightcolor = ihighlight;
    currentcolor = basecolor;
  }

  boolean over()
  {
    if( overRect(x, y, size, size) ) {
      over = true;
      return true;
    } else {
      over = false;
      return false;
    }
  }

  void display()
  {
    stroke(255);
    fill(currentcolor);
    rect(x, y, size, size);
  }

}

boolean overRect(int x, int y, int width, int height) {

  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }

}

link to mitchell's blog



The Arduino BT is a microcontroller board originally was based on the ATmega168, but now is supplied with the 328 (datasheet) and the Bluegiga WT11 bluetooth module (details and datasheet [pdf]). It supports wireless serial communication over bluetooth (but is not compatible with Bluetooth headsets or other audio devices). It has 14 digital input/output pins (of which 6 can be used as PWM outputs and one can be used to reset the WT11 module), 6 analog inputs, a 16 MHz crystal oscillator, screw terminals for power, an ICSP header, and a reset button. It contains everything needed to support the microcontroller and can be programmed wirelessly over the Bluetooth connection. Instructions are available for getting started with the Arduino BT.

Summary
  • Microcontroller    ATmega328
  • Operating Voltage    5V
  • Input Voltage    2.5-12 V
  • Digital I/O Pins    14 (of which 6 provide PWM output)
  • Analog Input Pins    6
  • DC Current per I/O Pin    40 mA
  • DC Current for 3.3V Pin    500 mA (with a 1.5A capable power source)
  • DC Current for 5V Pin    1000 mA (with a 1.5A capable power source)
  • Flash Memory    32 KB (of which 2 KB used by bootloader)
  • SRAM    2 KB
  • EEPROM    1 KB
  • Clock Speed    16 MHz
  • BT Module    2.1 WT11i-A-AI4

Power

The Arduino BT can be powered via the V+ and GND screw terminals. The board contains a DC-DC convector that allows it to be powered with as little as 2.5V, a maximum of 12V. Higher voltages or reversed polarity in the power supply can damage or destroy the board. The protection for reverse polarity connection is ONLY on the screw terminal.

The power pins are as follows:
  1. +VIN. The input voltage to the Arduino board (i.e. the same as the V+ screw terminal). You can supply voltage through this pin, or, if supplying voltage via the screw terminals, access it through this pin. Warning: The protection for reverse polarity connection is ONLY on the screw terminal, do not attach negative voltages to this pin. It will damage the board.
  2. 5V. This pin outputs a regulated 5V from the regulator on the board. The board can be supplied with power either from the screw terminal (2.5V - 12V) or the VIN pin of the board (2.5V-12V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage your board. We don't advise it.
  3. GND. Ground pins.

Memory
The ATmega328 has 32 KB of flash memory for storing code (of which 2 KB is used for the bootloader). It has 1 KB of SRAM and 512 bytes of EEPROM (which can be read and written with the EEPROM library).

Input and Output
Each of the 14 digital pins on the BT can be used as an input or output, using pinMode(), digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions:
  1.  Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the Bluegiga WT11 module.
  2. External Interrupts: 2 and 3. These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.
  3. PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
  4. SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI communication, which, although provided by the underlying hardware, is not currently included in the Arduino language.
  5. BT Reset: 7. Connected to the reset line of the Bluegiga WT11 module, which is active high.
  6. LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.

The BT has 6 analog inputs, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and some low-level code. Additionally, some pins have specialized functionality:

    I2C: 4 (SDA) and 5 (SCL). Support I2C (TWI) communication using the Wire library (documentation on the Wiring website).

There are a couple of other pins on the board:

    AREF. Reference voltage for the analog inputs. Used with analogReference().

See also the mapping between Arduino pins and ATmega168/328 ports.

Bluetooth Communication

The Bluegiga WT11 module on the Arduino BT provides Bluetooth communication with computers, phones, and other Bluetooth devices. The WT11 communicates with the ATmega328 via serial (shared with the RX and TX pins on the board). It comes configured for 115200 baud communication. The module should be configurable and detectable by your operating system's bluetooth drivers, which should then provide a virtual com port for use by other applications. The Arduino software includes a serial monitor which allows simple textual data to be sent to and from the Arduino board over this bluetooth connection. The board can also be reprogrammed using this same wireless connection.

The WT11 is specially configured for use in the Arduino BT. Its name is set to ARDUINOBT and passcode to 12345. For details, see the complete initialization sketch.

Communication

The Arduino BT has a number of other facilities for communicating. The ATmega328's UART TTL (5V) serial communication is available on digital pins 0 (RX) and 1 (TX) as well as being connected to the WT11 module.

A SoftwareSerial library allows for serial communication on any of the BT's digital pins.

The ATmega328 also supports I2C (TWI) and SPI communication. The Arduino software includes a Wire library to simplify use of the I2C bus; see the documentation on the Wiring website for details. To use the SPI communication, please see the ATmega328 datasheet.

Programming

The Arduino BT can be programmed with the Arduino software (download). For details, see the reference and tutorials.

The ATmega328 on the Arduino BT comes preburned with a bootloader that allows you to upload new code to it without the use of an external hardware programmer. It communicates using the original STK500 protocol (reference, C header files).

You can also bypass the bootloader and program the ATmega328 through the ICSP (In-Circuit Serial Programming) header; see these instructions for details.

Physical Characteristics

The maximum length and width of the BT are approximately 3.2 and 2.1 inches respectively. Three screw holes allow the board to be attached to a surface or case. Note that the distance between digital pins 7 and 8 is 160 mil (0.16"), not an even multiple of the 100 mil spacing of the other pins.

Source: Arduino

Followers