Skip to content

Instantly share code, notes, and snippets.

@bmander
Created May 2, 2012 15:41
Show Gist options
  • Save bmander/2577612 to your computer and use it in GitHub Desktop.
Save bmander/2577612 to your computer and use it in GitHub Desktop.
minimal arduino firmware to pipe accelerometer values from sparkfun SEN-10121 (serial 6DOF breakout) to serial
#define ADXL345_ADDR_ALT_LOW 0x53
#define FIMU_ACC_ADDR ADXL345_ADDR_ALT_LOW
#define ADXL345_POWER_CTL 0x2d
#define ADXL345_DATAX0 0x32
#define TO_READ (6)
#include <Wire.h>
int accelx;
int accely;
int accelz;
void writeto( byte device_address, byte address, byte val ){
Wire.beginTransmission(device_address); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
void imuinit(){
delay(5);
//power on accelerometer
writeto(FIMU_ACC_ADDR, ADXL345_POWER_CTL, 8);
}
void readfrom(int device_address, byte address, int num, byte _buff[]){
Wire.beginTransmission(device_address); // start transmission to device
Wire.write(address); // sends address to read from
Wire.endTransmission(); // end transmission
Wire.beginTransmission(device_address); // start transmission to device
Wire.requestFrom(device_address, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) // device may send less than requested (abnormal)
{
_buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); // end transmission
}
void readaccel(int *x, int *y, int *z){
byte _buff[6] ;
readfrom(FIMU_ACC_ADDR, ADXL345_DATAX0, TO_READ, _buff); //read the acceleration data from the ADXL345
// each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
// thus we are converting both bytes in to one int
*x = (((int)_buff[1]) << 8) | _buff[0];
*y = (((int)_buff[3]) << 8) | _buff[2];
*z = (((int)_buff[5]) << 8) | _buff[4];
}
void setup() {
Serial.begin(9600);
Wire.begin();
delay(5);
imuinit();
delay(5);
}
void loop() {
readaccel(&accelx,&accely,&accelz);
Serial.print( accelx );
Serial.print( "\t" );
Serial.print( accely );
Serial.print( "\t" );
Serial.print( accelz );
Serial.print("\n");
delay(60);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment