Home > Electronic Gadgets > Playing with Accelerometer and Bluetooth

Playing with Accelerometer and Bluetooth

Recently I was playing a little bit with a MMA7260Q triple axis accelerometer from Sparkfun. The brain is provided by an Arduino Pro Mini running at 3.3V. The talking is done by a bluetooth radio also obtained for hard money from the great folks of Sparkfun. The whole thing is easily assembled on a bread board and just worked right away. Behind the break you can find the code as well as an output log.

The Code

//Output via BT

// Breakout Board Pinout
// 10: GS1
// 11: GS2
// 12: sleep (low = sleep)
// A0: x-axis
// A1: y-axis
// A2: z-axis

byte gs1 = 10;                   // set sensitivity
byte gs2 = 11;                   // 00 = 1.5g, 01 = 2g, 10 = 4g, 11 = 6g
byte sleep = 12;                 // sleep
byte xpin = 0;                   // x-axis of the accelerometer
byte ypin = 1;                   // y-axis
byte zpin = 2;                   // z-axis
byte led = 13;

unsigned long time;
byte sensitivity = 0;
int x = 0;
int y = 0;
int z = 0;

void setup()
 Serial.begin(57600);        //max data rate for 8MHz Arduino
 pinMode(gs1, OUTPUT);
 pinMode(gs2, OUTPUT);
 pinMode(sleep, OUTPUT);
 digitalWrite(sleep, HIGH);
 digitalWrite(led, LOW);
 digitalWrite(led, HIGH);

void loop()
 if (Serial.available() > 0) {
  sensitivity = byte(Serial.read());

 x = AnalogToMG(analogRead(xpin));
 y = AnalogToMG(analogRead(ypin));
 z = AnalogToMG(analogRead(zpin));
 time = millis();

 Serial.print(time, DEC);
 Serial.print(", ");
 Serial.print(x, DEC);
 Serial.print(", ");
 Serial.print(y, DEC);
 Serial.print(", ");
 Serial.print(z, DEC);
 Serial.print(", ");
 Serial.print(AbsolutMG(x, y, z), DEC);

int AnalogToMG(int AnalogValue)
 return (AnalogValue -512);

int AbsolutMG(int xa, int xb, int xc)
 return int(sqrt(xa*xa+xb*xb+xc*xc));

void SetAccSensitivity(byte SetValue)
 switch (SetValue) {
 case 3:
  digitalWrite(gs1, HIGH);
  digitalWrite(gs2, HIGH);
 case 2:
  digitalWrite(gs1, HIGH);
  digitalWrite(gs2, LOW);
 case 1:
  digitalWrite(gs1, LOW);
  digitalWrite(gs2, HIGH);
  digitalWrite(gs1, LOW);
  digitalWrite(gs2, LOW);
  sensitivity = 0;

The Output

The acceleration values generated by the Arduino were send over the air by the BlueSMiRF. The serial terminal CuteCom allows logging of the values coming over SPP.

In the graph you can see, that the accelerometer chip was facing the x-axis up on startup. Then it was turned by 90 degrees around the y-axis so that z was facing down. The noise towards the end is just the result of violently shaking the sensor board.

Categories: Electronic Gadgets Tags:
  1. Benyamin Rusanto
    October 20, 2011 at 09:34

    Do you use a filter ? what kind of filter u filter

    • elims
      February 6, 2012 at 22:35

      No, I didn’t use a filter. What you see is the raw data.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: