(edit sidebar)
Intro to Physical Computing Syllabus

Research & Learning

Other Class pages

Shop Admin

ITP Help Pages
Tom's pcomp site
DanO's pcomp site


Bluetooth

Overview

There are a few methods of wireless serial communication:

Infrared (IR)
This is how television remotes work. IR transmitters send bits using pulses of infrared light and require a line of sight between the transmitter and receiver.

Radio/RF
This is how garage door openers work. RF transmitters send bits on pulses of radio energy. RF doesn’t require a line of sight but can be blocked by large metal objects. In order to differentiate the signal from other ambient radio or light waves, the data is sent on a carrier wave. If the receiver detects data at a carrier wave it’s listening for, it knows to read the data, and if it doesn’t, then it knows nothing is being sent. The actual information is encoded by slightly varying the frequency of the carrier wave (called frequency modulation, or FM) or its signal strength (called amplitude modulation, or AM) to indicate a logic 0 or 1. There are many different types of wireless serial communication. This tutorial focuses on Bluetooth serial communication.

Bluetooth is a high-speed RF communication protocol intended for connection between various mobile devices. The high speeds allow it to carry rich data like digitized audio and video. Once two devices make contact over a Bluetooth connection, the radios handle error correction, noise, and crosstalk for you. Its range is limited to about 10m (but some claim up to 100m!). It is primarily designed for one-to-one connections, and once two devices are connected they ignore all other Bluetooth devices nearby until the connection is broken.

Bluetooth devices can be considered modems because they convert electrical signals (TTL serial carried over wires) into Bluetooth serial carried over radio signals and back. A modem is a device that converts one type of signal into another, and connects one object to one other object. Your home cable/DSL modem is one example - it takes the digital data from your home computer, converts it to a signal that can be carried across the cable line, and connects to another modem on the other end. That modem is connected to your ISP’s network. All modems are designed to open a connection to another modem, negotiate the terms of data exchange, carry on an exchange, then disconnect. To do this, they have to have two operating modes, usually referred to as command mode, in which you talk to the modem, and data mode, in which you talk through the modem. Like modems, Bluetooth serial devices have two modes: command mode and data mode.

A Bluetooth radio has a unique address that other devices can use to identify it. To begin communication, the radio scans the area around it. If it discovers other devices, it can request a connection and begin conversing. Different modules have different commands, but in general you can:

  • Get the device’s address
  • Tell the device to scan for other devices
  • Connect to a given address
  • Disconnect from a given address
  • Enter or leave command mode

In order to use Bluetooth for physical computing, you’ll need a Bluetooth serial module. The one you'll use here is Sparkfun’s Bluetooth Mate. If you’re communicating from one microcontroller to the other, you’ll need two Bluetooth modules. Otherwise, you can use your laptop with a Bluetooth receiver (many have them built in, but if yours doesn’t, you can buy a Bluetooth adapter). The Bluetooth Mate has two interfaces: two of its pins, marked RX and TX, are an asynchronous serial port that can communicate with a microcontroller. It also has a radio that communicates using the Bluetooth communications protocol. It acts as a modem, translating between the Bluetooth and regular asynchronous serial protocols.

Bluetooth can carry many other services besides serial data, but this device can only carry serial data. To use it, you’ll need to tell your computer that you want to make a serial connection. On a PC or Mac with Bluetooth built in, you’ll establish and configure the serial connection using the Bluetooth control panel. If you have an adapter, you’ll be able to define a new serial port after installing the drivers according to the radio’s instructions.

To switch the Bluetooth Mate from data mode to command mode, send the string $$$. To exit command mode, send the string ---\r (the \r is a carriage return, ASCII 13), and the Bluetooth modem switches back to data mode.

In this lab, you'll learn how to how to use a Bluetooth Mate with a Lily Pad Arduino. You will be able to receive serial values through Bluetooth to Processing wirelessly.

(:toc Table of Contents:)

Parts

For this lab you will need to have the following parts:

solderless breadboard
Solderless breadboard
+3.3V FTDI
+3.3V FTDI Board
Lily Pad LiPower
LilyPad Arduino Simple Board
Bluetooth mate
Bluetooth Mate
LithiumBattery
Lithium Battery
10K Resistor
10K Resistor
22-AWG Hookup Wire
12-AWG Hookup Wire
2Photocell
Photocell


Pair Bluetooth and Laptop (MAC OS)

First, plug your FTDI board into your LilyPad.

The first time you use the Bluetooth Mate with your laptop, you need to introduce them to each other by using pairing. Once your computer has made contact with the Bluetooth module, you can connect to it like any other serial port. After choosing your board and serial port as usual, upload your code to your Lily Pad Arduino.


//Arduino code
void setup() {
 Serial.begin(115200); //The Bluetooth Mate has a baud rate of 115200, faster than the 9600 we're used to.
}

void loop() {
//
}

After uploading your sketch, you can unplug the FTDI board and plug in the Bluetooth Mate and lithium battery. The start LED on the Bluetooth Mate should start blinking. The pins on the Bluetooth Mate match with the Lily Pad's, so don't worry about the connection.

Mac Users: Now Turn on your Bluetooth settings and choose "Set Up Bluetooth Device" option.

Your machine will start searching for Bluetooth devices around you. Your Bluetooth device will be "FireFly-xxxx". It will show up as the port called FireFly-XXX-SPP.

After double clicking your device, laptop will try to pair with bluetooth. It is going to ask you to enter "0000". However, since we are not using a keyboard device, we need to go to "Passcode Options" and click "Do not use passcode with this device".

Pair Bluetooth and Laptop (Windows 7)

For Windows 7 users, click on the "Show Hidden Icons" icon in the taskbar to get to the "Bluetooth Devices". Click the "Bluetooth Devices Icon" and from the menu, choose "Open Settings". In the "Settings" window, choose the "Options" pane, and make sure that you've allowed other devices to find this computer, and that you've allowed them to connect as well. Then click the "COM ports" pane. If you've paired with you Bluetooth module before, there will be two ports indicated for it, one outgoing and one incoming. You're not using those though, because Windows tries to initiate contact when you do. You want the Bluetooth modem to initiate contact. Add a new incoming port. Note the port number, then open that port in your serial terminal program at 115200 bps.

Add a Photocell

We'll use a photocell here as an example of a way to get interesting data in to then sound out wirelessly via Bluetooth. Hook up the photocell like you do any other analog input sensor, similar to the FSRs in the Analog In lab.

Program your Lily Pad

Now, plug in your FTDI board again, and program your Lily Pad Arduino with the following code:

//Arduino Code:

 int photocellPin = 0;    // Analog input pin that the photocell is attached to
 int val = 0;   // value read from the analog sensor

 void setup() {
 Serial.begin(115200);
}

 void loop() {
  val = analogRead(photocellPin);
   Serial.println(val);
   Serial.flush();
   delay(200);
}

Test this code with the FTDI board on, and try to see if you are receiving any values on the Arduino serial monitor. Then you can switch to your Bluetooth device and battery.

Attention: If you are not using call and response method, you should put a delay at the end of your code. Otherwise, because of data overflowing, Bluetooth locks itself. Unlocking is possible, however we don't want that for now.

Program your Processing Sketch

Start your Processing sketch and type in following code to list available serial ports:


//Processing Code
import processing.serial.*;

void setup() {
//list all the available serial ports
println(Serial.list());
}

When you run this code, you should see values like this:

Look for the port that has tty and firefly in it, and note the number for the following example code:

//Processing code

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
  size(200, 200);
  println(Serial.list());  //list all the available serial ports
  //replace the 8 in the next line with your serial port number from the list above.  
  String portName = Serial.list()[8]; //My device is using "port 8" on my laptop
  myPort = new Serial(this, portName, 115200); //115200 is the data rate associated with this Bluetooth module
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    String myString = myPort.readStringUntil('\n');  //reads serial port until end of line
    if (myString != null) { //checks if myString is null
      myString = trim(myString);
      val = int(myString);  //converts string to integer
      background(255);             // Set background to white
      if (val < 150) {              // If the serial value is 0,
        fill(0);                   // set fill to black
      }
      else {                       // If the serial value is not 0,
        fill(204);                 // set fill to light gray
      }
      rect(50, 50, 100, 100);
    }
  }
}
 

When you first run this code, you will see a screen like this:

You need to type in "1234" as your passcode. After clicking "Pair", according to your sensor values, you will be able to see color changes on your rectangle.

Also, take a look at the "Connect" LED on your Bluetooth Mate - it should turn on and stay on as soon as you have established a connection between Bluetooth and your laptop.

  Edit | View | History | Print | Recent Changes | Search Page last modified on November 22, 2010, at 07:42 PM