BlueTooth Wireless

Overview

YOU NEED TO USE A USB 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.

Parts

These modems work as a serial (RX/TX) pipe. Any serial stream from 9600 to 115200bps can be passed seamlessly from your computer to your Arduino.

This requires both Arduino and Processing to be installed on your computer. Tested on MacOSX.

Additional hardware: BlueSMirF Silver (available: ask your instructor), LED, wires, LiPo battery (and Bluetooth dongle if your computer does not have an internal Bluetooth modem).

Download Processing at http://processing.org/download/.The values from the bluetooth are sent to Processing. Processing also includes graphical tools. Processing is a simple way to create drawings, animations and interactive graphics. Visualization of data coming from an Arduino is fairly straightforward.

Wiring Diagram

Connect your LED to your wiring board on Pin 8 in this tutorial. 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 the Arduino, connect the GND pin to the GROUND pin on Arduino. Then connect the Rx pin to the Tx pin of Wiring (digital pin 3) and connect the Tx pin to the Rx pin of Arduino (digital pin 2). Once it is connected, plug your battery into the board, if everything is connected right a small red LED will start blinking on 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 RN42-XXXX. Select to connect to it. It will ask you for a passkey. The passkey for the Bluetooth modem we used is ‘1234'.

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.

Connected your Arduino with the serial USB cable and upload the code below to your Arduino nano.

Paste the Procesing code into Processing. 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.

Your computer will establish a connection with the Bluetooth modem– you will see a red light begin to blink on the modem. A green light will stay on once a connection has been established. It 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 Arduino nano board via bluetooth. Now press any key on your keyboard. The Processing interface will show a black rectangle when a key is pressed and watch the LED turn on and off.

Arduino Code

/**
 * @file: Arduino control led via bluetooth
 * @author Daniela Faas (dfaas@mit.edu)
 * @date: 3/31/2011
 *
 * @section DESCRIPTION
 * This code allows control of an LED via BlueSmirf Silver module
 * http://wiring.org.co/learning/tutorials/bluetooth/
 * Modified by DF.
 *
 */
 
// global variables
int message = 0;           // variable to receive data from the serial port
#define ledpin 8            // LED connected to pin 48 (on-board LED)
//--- Function: Setup ()
void setup()
{
  pinMode(ledpin, OUTPUT);     // pin 48 (on-board LED) as OUTPUT
  Serial.begin(9600);          // start serial communication at 9600bps
}
//--- Function: loop ()
void loop()
{
  if( Serial.available() > 0 )        // if data is available to read
  {
    message = Serial.read();          // read it and store it in 'val'
    if( message == 'R' )              // if 'R' was received
    {
      digitalWrite(ledpin, HIGH);     // turn ON the LED
    }
    else if (message == 'r')
    {
      digitalWrite(ledpin, LOW);       // otherwise turn it OFF
    }
  }
  delay(100);                         // wait 100ms for next reading
}

Processing Code

/**
 * @file: Processing control led via bluetooth
 * @author Daniela Faas (dfaas@mit.edu)
 * @date: 3/31/2011
 *
 * @section DESCRIPTION
 * This code allows control of an LED via BlueSmirf Silver module
 * http://wiring.org.co/learning/tutorials/bluetooth/
 * Modified by DF.
 *
 */
//import class to set up serial connection with wiring board
import processing.serial.*;     //  Load serial library
Serial arduinoPort;             //  Set arduinoPort as serial connection
//--- Function: setup
void setup()
{
  //set up windowh
  size(200, 200);
  noStroke();
 
  // 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)
  arduinoPort = new Serial(this, Serial.list()[4], 9600);
}
//--- Function: draws background and rectangles
void draw()
{
  background(255);
  //Turn LED on and off if buttons pressed where
  //R = on (high) and r = off (low)
  if(keyPressed == true)
  {
     arduinoPort.write('R');
     println("on");
     fill(0);
  }
  else
  {
     arduinoPort.write('r');
     println("off");
     fill(255);
  }
  rect(25, 25, 50, 50);
}

Troubleshooting

Baud width defaults for Bluetooth modems are 115200 baud.

In Procesing: arduinoPort = new Serial(this, Serial.list()[4], 9600);

In Arduino: Serial.begin(9600);

If you're on Mac OS X (Snow Leopard) and have troubles setting it with Arduino, you might find this tutorial useful.To change the baud width: Bluesmirf + Mac OS X with ZTerm.

References

Bluetooth Modem - BlueSMiRF Gold

Bluetooth Modem - BlueSMiRF Silver

Setting up a Wireless serial connection with your Wiring hardware using Bluetooth

http://www.sparkfun.com/tutorials/67