How to: Setup a bluetooth connection between Arduino and a PC/Mac

The following tutorial is based on the one available at the following link (http://playground.arduino.cc/Learning/Tutorial01). 

The cheapest way to connect your Arduino to your PC wirelessly is using Bluetooth. The drawbacks of this solution are the limited range, about 10 meters, and the low bandwidth speed. If your project requires either higher range or more bandwidth you should opt for a more expensive WiFi module.

This tutorial will show you how to setup a Bluetooth Module for use as a serial data connection. I am using this particular module but should be similar with others.

Screen Shot 2013-09-21 at 10.17.31

Before to start, you need to have either a built-in Bluetooth module or a Bluetooth dongle installed in your PC/Mac. In addition you also need to have installed on your system both Arduino Software and Processing.

Firstly you need to set up the bluetooth module whitin your Mac or PC.

Under Mac

Open System Preferences. Under the section Internet & Wireless click on the Bluetooth icon.

Now click on Set Up New Device. The Bluetooth Module will appear as linvor.  Select it and press Continue. You need a specific passcode to pair the Bluetooth module and your Mac. Therefore, click on Passcode Option and enter 1234 (other bluetooth modules may have different codes). Then just wait until the pairing is completed. No other configuration steps are required.

Under Windows 7

Right click the Bluetooth Icon in the System Tray and select Bluetooth Devices. Click on the Add a Device. The search should bring up a bluetooth device called linvor. Select linvor and click Next. When asked select the option Enter the device’s pairing code. Enter the code 1234 (other bluetooth modules may have different codes).

The device should now be successfully added to the computer.

Click Close to return to the Bluetooth Devices list. Right Click on the linvor device and select Properties. Click on the Services Tab and you should see the Serial Port service along with a port number. In my case is COM4. Take note of the port number because you will need it later.

Code

Connect your Arduino to your Computer using an USB cable as usual.

Open your Arduino Software, create a new sketch and copy the following code:


char val; // variable to receive data from the serial port
int ledpin = 13; // Arduino LED pin 13 (on-board LED)
void setup() {
   pinMode(ledpin, OUTPUT); // pin 13 (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
}

Once you have upload the code on your Arduino, disconnect the Arduino USB cable from your computer (this step is important). If you leave your Arduino connected to the computer with the USB cable the Bluetooth connection will not work! You can use an external battery or any other external source to power your Arduino.

Feel free to close the Arduino software. You will not need it anymore.

The following step is to connect the Bluetooth module to the Arduino as follows:

  • Connect the +5V bluetooth module Pin to the 5V  Arduino Pin
  • Connect the 0V bluetooth module Pin to the GND  Arduino Pin
  • Connect the TX bluetooth module Pin to the RX  Arduino Pin
  • Connect the RX bluetooth module Pin to the TX  Arduino Pin

Connect Arduino to a power source and the module Bluetooth led should start flashing.

Open Processing and copy the following 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;
   }
}

The only line you have to modify is the following:

port = new Serial(this, Serial.list()[2], 9600);

where [2] is the number of the serial port of your Bluetooth device (ex: [3] is COM4 in my case). If you run the Processing code you should see in the logs a list of all available ports. Make sure you select the right one for you.

Once the Bluetooth module is connected to your Computer the LED on the module will stop flashing and be on permanently.

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.

How to control Arduino with your voice

Adding speech recognition capability to your Arduino project is easier than you can imagine. I am currently working on an 4WD Arduino robot project. My robot can be controlled using only my voice. So far, I have implemented the following commands: go ahead, go back, turn left/right and stop.

If you want to add voice recognition to your project, you need to bear in mind that Arduino is not powerful enough to run a speech recognition code. But this is not a big issue. There are two alternative ways to do that:

  1. Using a Speech Recognition shield (Ex. https://www.sparkfun.com/products/10963).
  2. Connecting a PC to your Arduino. The PC computes the voice recognition algorithm and sends the command to the Arduino.

The Speech Recognition shield is a quite expensive solution. Moreover, I am already using a Motor Shield with my Arduino so I cannot use another shield.

That’s why I decided to go for the second option. My Arduino robot is connected to my PC with a Bluetooth connection, but you can use a USB cable or a wireless connection as well.

Before to start, you will need the following:

  • A PC with Microsoft Windows Vista, 7 or 8.
  • A microphone connected either to your PC or Arduino board.
  • Arduino has to be connected to your PC with one of the following communication interfaces:  USB (used as a virtual COM port), Bluetooth Adapter or Wireless Adapter.
  • The speech recognition software BitVoicer installed in your window PC. You will need to buy this software, but do not worry because it is actually very cheap and it works very well. BitVoicer requires an Internet connection for activation and installation of additional languages.

Once you are ready to start, you can download the BitVoicer manual from their website. Here it is the link.

The manual contains all the information you will need. It also provides two examples.

In the first example, the audio is captured by the computer’s microphone. A LED is connected to the digital pin 4 of the Arduino will turn on and off in response to a voice command captured by the computer’s microphone. In the second one, the microphone is directly connected to the Arduino board.

Once you understand how to implement these two basic examples, you will be ready to work on more advanced speech recognition project with your Arduino.