Monday, December 23, 2013

Good data rate from Arduino over Bluetooth Serial

I am designing a better wireless data transmitter.  To do this, I have decided to switch from XBee to a bluetooth module.  I purchased the following:


Bluetooth Module Breakout - Roving Networks (RN-41)
https://www.sparkfun.com/products/12579

The Roving Networks module is easy to use once you figure out how to pair it with your PC.  Here is how I did it:

2.  Pair the module with your PC using the default pairing code on the Roving module "1234"
3.  Find out the mapped serial port.  On my computer this produced two ports, only one of which worked for connecting to the module.
4.  Set the appropriate baud rate.  The module comes pre-configured for 115200 baud, however, I found it helpful to set the baud to 9600 to get started by putting pin GPIO7 to VDD.
5.  Program the module (including name) using PuTTY as follows:
     a.  Ready the PuTTY connection 
     b.  Power on the module
     c.  Connect via PuTTY within the first minute and enter programming mode by sending $$$ to the module
     d.  Program the module using instructions in:  https://www.sparkfun.com/datasheets/Wireless/Bluetooth/rn-bluetooth-um.pdf

I hooked it up to my Arduino Mega powered from the 3.3V output and with the transmit pin of Serial1 connected to the receive pin of the RN-41 through a voltage divider: 

TX1 (pin 18) via 10K/20K voltage divider to TX pin on the RN-41

I also added some LEDs for status output on pins PIO5 and PIO2 which together help me understand the status of the module (PIO5 blinks 1Hz when waiting for connection and 10Hz 10Hz when in command mode and PIO2 is on solid when connection is made)

Here is what it looks like:



I then wrote a little Arduino sketch for testing how fast I could get data to the PC via bluetooth.  I am interested in sending analog values to the module, so I simulated that by reading analog channels on the Arduino (for now).  I intend to send that data as unsigned integers with 16 bit precision so what I am doing is sending over 16 ASCII characters ("ABCDEFGHIJKLMNO_").

Here is my code:

unsigned int val;
unsigned int fakeVal[8];
char *p_val;
int count;
int reportCount;
int nChans;
unsigned long t0;
char str[17] = "ABCDEFGHIJKLMNO_";

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  count = 0;
  reportCount = 2000;
  nChans = 8;
  
  p_val = &str[0];
  //p_val = (char*)&val;
  t0 = millis();
}

void loop() {

  int i;
  for (i=0;i<nChans;i++)
  {
    val = analogRead(i); // 100 microseconds
    Serial1.write(p_val[i*2]);
    Serial1.write(p_val[i*2+1]);
    delayMicroseconds(97);
  }
  
  if (++count==reportCount) {
    Serial.print("Simulating ");
    Serial.print(nChans);
    Serial.println(" 16 bit channels");
    Serial.print("Data rate: ");
    Serial.print(1000.0*((double)reportCount)/((double)(millis()-t0)));
    Serial.println(" values per channel per sec");
    count = 0; 
    t0 = millis();
  }
}

I am using PuTTY to monitor the arriving data and things look pretty good sending 16 bytes at 500 Hz.  


The theoretical limit of an 115200 bps connection is 115200/16/8 = 900 Hz, however, I find that the connection is unstable (i.e. module spontaneously disconnects) at anything much over 600 Hz.  This may be a PuTTY problem so we'll see if I can get up to the max.

As for power requirements.  Bluetooth pulls about 50 mA of current in default power mode.
Here are my power readings:

Setting  Output (dBM)  Current Draw (mA)
                         Approximate via FLUKE Multimeter
SY,0004       4              50
SY,0001       0              43
SY,FFFC      -4              40
SY,FFF8      -8              40
SY,FFF4      -12             50-80 (choppy connection?)

This range of current draw is similar to what is described for the XBee so this thing may still play nice with the battery though I do not know if the battery will survive this thing PLUS an arduino... only one way to know :)