Projects‎ > ‎

Bluetooth

Goals
Use an Arduino and the Sparkfun Bluesmirf Silver to communicate with a computer and Android device
Use an Android device to drive a robot

Hardware Set 1
CEENBoT robot with breadboard
Arduino Pro Mini 3.3V 8MHz w/ ATmega328
Sparkfun FTDI Basic 3.3V
Right angle header added to Pro Mini provides direct connection to FTDI Basic
Sparkfun BlueSMiRF Silver
Sparkfun FTDI Basic 5V
Straight header added to BlueSMiRF provides connection to breadboard
Male and female jumper wires


Software Set 1

Install
Download and install Arduino development environment
Download and install FTDI drivers
Connect FTDI to Pro Mini - FTDI plugs directly into right angle header
Configure serial port > System Preferences > Network > USB Modem > Default > Apply
Test by completing Arduino blink test

Connection to FTDI Basic and Configuration Commands
Setting configuration of BlueSMiRF
Connect FTDI Basic 5V to BlueSMiRF
BLK - ground to ground
RED - VCC to VCC
BLUE - TX to RX, RX to TX
Create a connection in CoolTerm
Connection > Options
Serial Port > Port: usbserial, 115200, 8,  None, 1 
Terminal >Mode: Raw, Enter key: CR+LF, Local Echo

Type and send three dollar signs to set modem to command mode: $$$
IMPORTANT:  Must send during the first 60 seconds after powering up (According to manual, only for remote BT configuration?)

Stat light, red light on BlueSMiRF, flashes slowly in operating mode, flashes fast in command mode
After sending $$$ in the first 60 seconds, should get a CMD response and rapid flashing of stat light
Enter the letter D or d to display the current configurations


NOTE: Pincod=1234, connection pin code

h = help list of all commands
r,1 = reload, similar to power cycle
$$$ = start command mode (three dollar signs)
--- = stop command mode (three minus signs)
d = display configuration
sn,name = name device
sp,pincod = reset the pin code, also clears paired device list, can enter same pin code just to clear paired device list

Connection to Mac Computer (10.6.8, Snow Leopard)
The connection to a computer with bluetooth worked well
Turn on the Bluetooth status on menu bar with System Preferences > Bluetooth > Check box at bottom
On a Mac use Setup Bluetooth device from the Bluetooth status in the menu bar
Make sure BlueSMiRF is discoverable. Power cycling BlueSMiRF will start discovery.
Select the device and the Mac creates a serial port for the BlueSMiRF
The Mac attempts to send a passcode of 0000
Use the Passcode Options button at the bottom, then select Do not use a passcode

Once the device is setup, use CoolTerm to make a connection
Realize, CoolTerm will be making two connections:
CoolTerm_5: USB to FTDI to BlueSMiRF serial interface
CoolTerm_6: Mac Bluetooth to BlueSMiRF through the a Bluetooth virtual serial port 


Connection to PC Computer (Win7)
TODO

Connection to Android Device

3.0 Honeycomb
BlueTerm app worked fine

4.0 Ice Cream Sandwich (ICS)
Bluetooth was changed in ICS to make it more secure. This caused Bluetooth to work differently. Read more
BlueTerm does not work. SENA B Term does not work.
Android Bluetooth Chat for 4.0 (API 15) from the SDK worked.
In BluetoothChatService.java, set the UUID for insecure connections

    private static final UUID MY_UUID_INSECURE =
           UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

When making the connection within Bluetooth chat on Android, 
use the menu (upper right) and select Connect a device - INSECURE

Joysticks on Android
NOTE: Testing showed software joysticks are not a great user interface on a Android device, too hard to control
NOTE: Looking at using vertical SeekBars (sliders)

Combined the Android sample Bluetooth Chat and the Joystick Widget into one Android app
Found out through testing that it seems characters from 1 to 127 can be sent
Did not do detailed testing however, but set the integers sent accordingly
Changed Joystick Widget to return integers from -31 to 31
Added 64 to left joystick values to get 1 to 63
Added 96 to right joystick values to get 65 to 127 
A single character string will be sent by the Bluetooth Chat sendMessage when the joystick value changes
The left and right joystick values can be separated by Arduino based on number range
Used the computer connection to BlueSMiRF to test the joystick communication
Commented out Bluetooth chat messages in BluetoothActivityTwoStick, in the handler method

Connection to Arduino
Used the SoftwareSerial library included with the current Arduino development environment (IDE)
There were some references to the NewSoftSerial library, but that has been included as SoftwareSerial
The SoftwareSerial library was used to create software defined RX and TX using Arduino pins 2 and 3
Connect the RX of Arduino to the TX of BlueSMiRF, and TX to RX, and be sure to connect the GNDs as well
Values from the BlueSMiRF, from Android, were written to the serial monitor in the IDE

From the values received, the ranges were used to send left and right values on ports 5 and 6 using PWM
A resistor-capacitor (RC) filter was used to convert the Arduino PWM to voltage
Connect 22 ohm resistor to port (5 or 6), connect other end of resistor to positive end of 100 uF capacitor
Connect the negative end of capacitor to ground
Connect the ADC (1 or 2) to the junction between the resistor and capacitor (positive end of capacitor)
Use two RC filters, one for each channel (port 5 to ADC1, port 6 to ADC2)

Arduino Program Listing

// Android to BlueSMiRF Bluetooth serial to Arduino to CEENBoT
// Android sends values 0 to 63 from the left joystick and 64 to 127 from the right joystick
// values less than 64 will be output to pin 5, values greater than 64 to pin 6
// an RC filter will smooth the PWM output with pin 5 going to ADC1 and pin 6 to ADC2

// a software serial port is created on pins 2 (RX) and 3 (TX) to receive data from BlueSMiRF
// so still will be able use normal serial communication to the computer on pins 0 and 1
// this allows programming and serial monitoring on the computer while using BlueSMiRF

// the BlueSMiRF modem defaults to 115K BAUD which is too fast for Arduino to receive from Android
// the best comm rate seems to be 57.6k BAUD, slower rates like 9600 BAUD seem to be drop characters
// to change the BAUD rate, connect to BlueSMiRF with a FDTI serial board and computer terminal program
// enter command mode (send $$$ from terminal), then the command (send su,57), then exit (send ---)

// include the software serial library to create a virtual serial port
#include <SoftwareSerial.h>

// create the software serial port on pins 2 (RX) and 3 (TX)
SoftwareSerial ss(2, 3); // (RX, TX)

// value received from BlueSMiRF modem and sent to CEENBoT
int c;

// set the BAUD rate and setup serial debugging
void setup()
{
  ss.begin(57600);
  Serial.begin(9600);
  Serial.println("Start serial communication");
}

// receive a message from Android, monitor on Arduino, send to CEENBoT
// left joystick/motor value on pin 5, right joystick/motor value on pin 6
void loop()
{
  if (ss.available()) {
     c = ss.read();
     if (c < 64) {
        c = c * 4;
        analogWrite(5, c);
        Serial.print("L5: ");
        Serial.println(c);
     } else {
        c = (c-64) * 4;
        analogWrite(6, c);
        Serial.print("R6: ");
        Serial.println(c);
     }
   }
}


Connection to CEENBoT
Used the ADC tool to read ADC1 and ADC2 and convert the values to left and right motor speeds
The ADC values would vary and at times were inconsistant
Some smoothing was done in the CEENBoT programming to improve the driving
Using C and the CEENBoT API library would be easier
The serial port could be used to connect directly with BlueSMiRF

Android virtual joysticks to BlueSMiRF Silver to Arduino Pro Mini to RC Filters to CEENBoT ADC.
Not really road worthy, but a good learning experience.

Arduino input from Android to BlueSMiRF is very consistant
Virtual on screen joysticks are difficult to control


Using the BlueSMiRF to Arduino directly
The Arduino can be used to control continuous rotation servos
This code was used on the Arduino

// include the software serial library to create a virtual serial port and servo objects
#include <SoftwareSerial.h>
#include <Servo.h>

// create the software serial port on pins 2 (RX) and 3 (TX)
SoftwareSerial ss(2, 3); // (RX, TX)

// create servo objects
Servo servoL;
Servo servoR;

// value received from BlueSMiRF modem and sent to servos
int c;

// set the BAUD rate and setup serial debugging
void setup()
{
  ss.begin(57600);
  servoL.attach(5); // left servo PWM on pin 5
  servoR.attach(6); // right servo PWM on pin 6
  Serial.begin(9600);
  Serial.println("Start serial communication");
}

// receive a message from Android, monitor on Arduino, send to CEENBoT
// left joystick/motor value on pin 5, right joystick/motor value on pin 6
void loop()
{
  if (ss.available()) {
     c = ss.read();
     if (c < 64) {
        // scale 0-63 to 0-180, and reverse
        c = 180 - 360 * c / 128;
        servoL.write(c);
        Serial.print("L5: ");
        Serial.println(c);
     } else {
        // scale 64-127 to 0-180
        c = 360 * (c-64) / 128;
        servoR.write(c);
        Serial.print("R6: ");
        Serial.println(c);
     }
     delay(30);
   }
}



 

References
Comments