TinyCircuits

Accelerometer Shield

Accelerometer Shield
« on: June 28, 2013, 11:46:36 AM »
Hi All,

Just got my starter kit yesterday from Kickstarter (waited for the accelerometer shield to be ready) set it up and so far so good!

I found this example sketch that might be helpful to others:

Code: [Select]
/**********************************************************
 * BMA250_I2C_Sketch.pde ----- Sample sketch for BMA250 Accelerometer using I2C
 *
 * A sample sketch that shows the basic functions of the BMA250 acclerometer.
 *
 *
 * 2011, DSS Circuits, Inc.  http://www.dsscircuits.com
 *
 **********************************************************/

#include <Wire.h>

#define BMA250              0x18
#define DELAY               64000
#define BW                  0x08 //7.81Hz bandwith
#define GSEL                0x05 //0x03 - 2g, 0x05 - 4, 0x08 - 8g, 0x0C - 16g

uint8_t dataArray[16];
int x,y,z;


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


void loop()
{
 
  read3AxisAcceleration();
  delayMicroseconds(DELAY);
}


byte read3AxisAcceleration()
{
  Wire.beginTransmission(BMA250);
  Wire.write(0x02);
  Wire.endTransmission();
  Wire.requestFrom(BMA250,7);
  for(int i = 0; i < 7;i++)
  {
    dataArray[i] = Wire.read();
  }
  if(!bitRead(dataArray[0],0)){return(0);}
 
  int8_t temp = dataArray[6];
  x = dataArray[1] << 8;
  x |= dataArray[0];
  x >>= 6;
  y = dataArray[3] << 8;
  y |= dataArray[2];
  y >>= 6;
  z = dataArray[5] << 8;
  z |= dataArray[4];
  z >>= 6;
  Serial.print("X = ");
  Serial.print(x);
  Serial.print("  ");
  Serial.print("Y = ");
  Serial.print(y);
  Serial.print("  ");
  Serial.print("Z = ");
  Serial.print(z);
  Serial.print("  Temperature(C) = ");
  Serial.println((temp*0.5)+24.0,1);
}

byte initializeBMA()
{
  Wire.beginTransmission(BMA250);
  Wire.write(0x0F); //set g
  Wire.write(GSEL);
  Wire.endTransmission();
  Wire.beginTransmission(BMA250);
  Wire.write(0x10); //set bandwith
  Wire.write(BW);
  Wire.endTransmission();
  return(0);
}

I found it here: http://dsscircuits.com/accelerometer-bma250.html but it was out of date, so I updated it. :)

Note that you need to change the serial monitor baud to match the Serial.begin(), so 115200 baud.

Hope this helps someone, it was quite a bit of searching to find it.
-ak

Re: Accelerometer Shield
« Reply #1 on: June 28, 2013, 02:45:22 PM »
Thanks for putting this up!  You beat me to it!  I'm updating the website with better instructions and examples for all the new boards, should have this up by this weekend.

Thanks!

Ken

Re: Accelerometer Shield
« Reply #2 on: June 28, 2013, 04:09:50 PM »
Sure thing Ken!

Just to sweeten the deal, here's a processing sketch that simply receives the serial stream from the previous code and prints the data out:

Code: [Select]
import processing.serial.*;
Serial myPort;
float value;

int x,y,z;
  int[] dataArray;

void setup()
{
  myPort = new Serial(this, Serial.list()[4], 115200);
}

void draw()
{
}

void serialEvent(Serial p) {
  // get message till line break (ASCII > 13)
  String message = myPort.readStringUntil(13);
  if (message != null) {
    println(message);
  }
}

Note that you may have to change the Serial.list()[] number to something other than 4 (I was able to just count down the options in the menu, fifth option, #4!).

thanks for the tiny hardware Ken!
ak

Re: Accelerometer Shield
« Reply #3 on: July 25, 2013, 01:03:57 AM »
The best part about testing the Accelerometer Shield was flying those tiny RC helicopters around the office!

Andy