Go Down

Topic: Datatype Error - Uno to Due Accelerometer Library Conversion (Read 736 times) previous topic - next topic


Hi All,

I am working on using Sparkfun's 6DOF IMU with the DUE. I originally had it paired with the UNO r3 and it was working great, but after connecting it to the DUE I am unable to accurately read any negative values from the accelerometer. It reads accurately from 0-90 degrees of rotation but any negative values on any of the axes gives me a completely wrong value that doesn't change with rotation. This leads me to believe that a bitwise operation (two's complement?) is being incorrectly performed.

A similar problem was posted http://arduino.cc/forum/index.php?topic=133031.0, but this is for an SPI connected device.

I believe the error is coming from this portion of the library:
Code: [Select]

void ADXL345::readAccel(int *xyz){
  readAccel(xyz, xyz + 1, xyz + 2);

// Reads the acceleration into three variable x, y and z
void ADXL345::readAccel(int *x, int *y, int *z) {
  readFrom(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];

//_buff is declared in the header files as:
byte _buff[6] ;    //6 bytes buffer for saving data read from the device

I will be the first to admit that I am weak on pointers, data types, and programming in general but hopefully someone can point me in the right direction  :).

Any help would be appreciated.



Are you calling the first function or the second one.

The second one should work as expected.   

The first one is very slightly questionable,  it should work but there is potential for it to
not work if the pointer calculation is wrong.


Apr 05, 2013, 08:48 am Last Edit: Apr 05, 2013, 08:52 am by AWOL Reason: 1
"int"s on the Due are 32 bit (four bytes), so a six byte buffer will not work.

Edit: Sorry, assumed the buffer was being cast to "int*", so need to see all your code.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


Thank you AWOL and Michinyon for responding! I apologize for just dumping the code on here but here is the library below:

Code: [Select]

*                                                                         *
* ADXL345 Driver for Arduino                                              *
*                                                                         *
*                                                                         *
* This program is free software; you can redistribute it and/or modify    *
* it under the terms of the GNU License.                                  *
* This program is distributed in the hope that it will be useful,         *
* but WITHOUT ANY WARRANTY; without even the implied warranty of          *
* GNU License V2 for more details.                                        *
*                                                                         *
#include "Arduino.h"

#ifndef ADXL345_h
#define ADXL345_h

/* -- ADXL345 addresses --*/
#define ADXL345_ADDR_ALT_HIGH 0x1D // ADXL345 address when ALT is connected to HIGH
#define ADXL345_ADDR_ALT_LOW  0x53 // ADXL345 address when ALT is connected to LOW

/* ------- Register names ------- */
#define ADXL345_DEVID 0x00
#define ADXL345_RESERVED1 0x01
#define ADXL345_THRESH_TAP 0x1d
#define ADXL345_OFSX 0x1e
#define ADXL345_OFSY 0x1f
#define ADXL345_OFSZ 0x20
#define ADXL345_DUR 0x21
#define ADXL345_LATENT 0x22
#define ADXL345_WINDOW 0x23
#define ADXL345_THRESH_ACT 0x24
#define ADXL345_THRESH_INACT 0x25
#define ADXL345_TIME_INACT 0x26
#define ADXL345_ACT_INACT_CTL 0x27
#define ADXL345_THRESH_FF 0x28
#define ADXL345_TIME_FF 0x29
#define ADXL345_TAP_AXES 0x2a
#define ADXL345_ACT_TAP_STATUS 0x2b
#define ADXL345_BW_RATE 0x2c
#define ADXL345_POWER_CTL 0x2d
#define ADXL345_INT_ENABLE 0x2e
#define ADXL345_INT_MAP 0x2f
#define ADXL345_INT_SOURCE 0x30
#define ADXL345_DATA_FORMAT 0x31
#define ADXL345_DATAX0 0x32
#define ADXL345_DATAX1 0x33
#define ADXL345_DATAY0 0x34
#define ADXL345_DATAY1 0x35
#define ADXL345_DATAZ0 0x36
#define ADXL345_DATAZ1 0x37
#define ADXL345_FIFO_CTL 0x38
#define ADXL345_FIFO_STATUS 0x39

#define ADXL345_BW_1600 0xF // 1111
#define ADXL345_BW_800  0xE // 1110
#define ADXL345_BW_400  0xD // 1101  
#define ADXL345_BW_200  0xC // 1100
#define ADXL345_BW_100  0xB // 1011  
#define ADXL345_BW_50   0xA // 1010
#define ADXL345_BW_25   0x9 // 1001
#define ADXL345_BW_12   0x8 // 1000
#define ADXL345_BW_6    0x7 // 0111
#define ADXL345_BW_3    0x6 // 0110

Interrupt PINs
INT1: 0
INT2: 1
#define ADXL345_INT1_PIN 0x00
#define ADXL345_INT2_PIN 0x01

Interrupt bit position
#define ADXL345_INT_DATA_READY_BIT 0x07
#define ADXL345_INT_SINGLE_TAP_BIT 0x06
#define ADXL345_INT_DOUBLE_TAP_BIT 0x05
#define ADXL345_INT_ACTIVITY_BIT   0x04
#define ADXL345_INT_FREE_FALL_BIT  0x02
#define ADXL345_INT_WATERMARK_BIT  0x01
#define ADXL345_INT_OVERRUNY_BIT   0x00

#define ADXL345_OK    1 // no error
#define ADXL345_ERROR 0 // indicates error is predent

#define ADXL345_NO_ERROR   0 // initial state
#define ADXL345_READ_ERROR 1 // problem reading accel
#define ADXL345_BAD_ARG    2 // bad method argument

class ADXL345
 bool status;           // set when error occurs
                        // see error code for details
 byte error_code;       // Initial state
 float gains[3];        // counts to Gs

 void init(int address);
 void powerOn();
 void readAccel(int* xyx);
 void readAccel(int* x, int* y, int* z);
 void get_Gxyz(float *xyz);

 void setTapThreshold(int tapThreshold);
 int getTapThreshold();
 void setAxisGains(float *_gains);
 void getAxisGains(float *_gains);
 void setAxisOffset(int x, int y, int z);
 void getAxisOffset(int* x, int* y, int*z);
 void setTapDuration(int tapDuration);
 int getTapDuration();
 void setDoubleTapLatency(int floatTapLatency);
 int getDoubleTapLatency();
 void setDoubleTapWindow(int floatTapWindow);
 int getDoubleTapWindow();
 void setActivityThreshold(int activityThreshold);
 int getActivityThreshold();
 void setInactivityThreshold(int inactivityThreshold);
 int getInactivityThreshold();
 void setTimeInactivity(int timeInactivity);
 int getTimeInactivity();
 void setFreeFallThreshold(int freeFallthreshold);
 int getFreeFallThreshold();
 void setFreeFallDuration(int freeFallDuration);
 int getFreeFallDuration();

 bool isActivityXEnabled();
 bool isActivityYEnabled();
 bool isActivityZEnabled();
 bool isInactivityXEnabled();
 bool isInactivityYEnabled();
 bool isInactivityZEnabled();
 bool isActivityAc();
 bool isInactivityAc();
 void setActivityAc(bool state);
 void setInactivityAc(bool state);

 bool getSuppressBit();
 void setSuppressBit(bool state);
 bool isTapDetectionOnX();
 void setTapDetectionOnX(bool state);
 bool isTapDetectionOnY();
 void setTapDetectionOnY(bool state);
 bool isTapDetectionOnZ();
 void setTapDetectionOnZ(bool state);

 void setActivityX(bool state);
 void setActivityY(bool state);
 void setActivityZ(bool state);
 void setInactivityX(bool state);
 void setInactivityY(bool state);
 void setInactivityZ(bool state);

 bool isActivitySourceOnX();
 bool isActivitySourceOnY();
 bool isActivitySourceOnZ();
 bool isTapSourceOnX();
 bool isTapSourceOnY();
 bool isTapSourceOnZ();
 bool isAsleep();

 bool isLowPower();
 void setLowPower(bool state);
 float getRate();
 void setRate(float rate);
 void set_bw(byte bw_code);
 byte get_bw_code();  

 byte getInterruptSource();
 bool getInterruptSource(byte interruptBit);
 bool getInterruptMapping(byte interruptBit);
 void setInterruptMapping(byte interruptBit, bool interruptPin);
 bool isInterruptEnabled(byte interruptBit);
 void setInterrupt(byte interruptBit, bool state);

 void getRangeSetting(byte* rangeSetting);
 void setRangeSetting(int val);
 bool getSelfTestBit();
 void setSelfTestBit(bool selfTestBit);
 bool getSpiBit();
 void setSpiBit(bool spiBit);
 bool getInterruptLevelBit();
 void setInterruptLevelBit(bool interruptLevelBit);
 bool getFullResBit();
 void setFullResBit(bool fullResBit);
 bool getJustifyBit();
 void setJustifyBit(bool justifyBit);
 void printAllRegister();
 void writeTo(byte address, byte val);

 void readFrom(byte address, int num, byte buff[]);
 void setRegisterBit(byte regAdress, int bitPos, bool state);
 bool getRegisterBit(byte regAdress, int bitPos);  
 byte _buff[6] ;    //6 bytes buffer for saving data read from the device
 int _dev_address;
void print_byte(byte val);

Thank you!


ADXL345.CPP (minus some functions to meet character limit)

Code: [Select]
#include "ADXL345.h"
#include <Wire.h>

#define TO_READ (6)      // num of bytes we are going to read each time (two bytes for each axis)

ADXL345::ADXL345() {
  status = ADXL345_OK;
  error_code = ADXL345_NO_ERROR;

  gains[0] = 0.00376390;
  gains[1] = 0.00376009;
  gains[2] = 0.00349265;

void ADXL345::init(int address) {
  _dev_address = address;

void ADXL345::powerOn() {
  //Turning on the ADXL345
  //writeTo(ADXL345_POWER_CTL, 0);     
  //writeTo(ADXL345_POWER_CTL, 16);
  writeTo(ADXL345_POWER_CTL, 8);

// Reads the acceleration into an array of three places
void ADXL345::readAccel(int *xyz){
  readAccel(xyz, xyz + 1, xyz + 2);

// Reads the acceleration into three variable x, y and z
void ADXL345::readAccel(int *x, int *y, int *z) {
  readFrom(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 ADXL345::get_Gxyz(float *xyz){
  int i;
  int xyz_int[3];
  for(i=0; i<3; i++){
    xyz[i] = xyz_int[i] * gains[i];

// Writes val to address register on device
void ADXL345::writeTo(byte address, byte val) {
  Wire.beginTransmission(_dev_address); // start transmission to device
  Wire.write(address);             // send register address
  Wire.write(val);                 // send value to write
  Wire.endTransmission();         // end transmission

// Reads num bytes starting from address register on device in to _buff array
void ADXL345::readFrom(byte address, int num, byte _buff[]) {
  Wire.beginTransmission(_dev_address); // start transmission to device
  Wire.write(address);             // sends address to read from
  Wire.endTransmission();         // end transmission

  Wire.beginTransmission(_dev_address); // start transmission to device
  Wire.requestFrom(_dev_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
  if(i != num){
    status = ADXL345_ERROR;
    error_code = ADXL345_READ_ERROR;
  Wire.endTransmission();         // end transmission

// Gets the range setting and return it into rangeSetting
// it can be 2, 4, 8 or 16
void ADXL345::getRangeSetting(byte* rangeSetting) {
  byte _b;
  readFrom(ADXL345_DATA_FORMAT, 1, &_b);
  *rangeSetting = _b & B00000011;

// Sets the range setting, possible values are: 2, 4, 8, 16
void ADXL345::setRangeSetting(int val) {
  byte _s;
  byte _b;

  switch (val) {
  case 2: 
    _s = B00000000;
  case 4: 
    _s = B00000001;
  case 8: 
    _s = B00000010;
  case 16:
    _s = B00000011;
    _s = B00000000;
  readFrom(ADXL345_DATA_FORMAT, 1, &_b);
  _s |= (_b & B11101100);
  writeTo(ADXL345_DATA_FORMAT, _s);
// gets the state of the SELF_TEST bit
bool ADXL345::getSelfTestBit() {
  return getRegisterBit(ADXL345_DATA_FORMAT, 7);

// Sets the SELF-TEST bit
// if set to 1 it applies a self-test force to the sensor causing a shift in the output data
// if set to 0 it disables the self-test force
void ADXL345::setSelfTestBit(bool selfTestBit) {
  setRegisterBit(ADXL345_DATA_FORMAT, 7, selfTestBit);

// Gets the state of the SPI bit
bool ADXL345::getSpiBit() {
  return getRegisterBit(ADXL345_DATA_FORMAT, 6);

// Sets the SPI bit
// if set to 1 it sets the device to 3-wire mode
// if set to 0 it sets the device to 4-wire SPI mode
void ADXL345::setSpiBit(bool spiBit) {
  setRegisterBit(ADXL345_DATA_FORMAT, 6, spiBit);

// Gets the state of the INT_INVERT bit
bool ADXL345::getInterruptLevelBit() {
  return getRegisterBit(ADXL345_DATA_FORMAT, 5);

// Sets the INT_INVERT bit
// if set to 0 sets the interrupts to active high
// if set to 1 sets the interrupts to active low
void ADXL345::setInterruptLevelBit(bool interruptLevelBit) {
  setRegisterBit(ADXL345_DATA_FORMAT, 5, interruptLevelBit);

// Gets the state of the FULL_RES bit
bool ADXL345::getFullResBit() {
  return getRegisterBit(ADXL345_DATA_FORMAT, 3);

// Sets the FULL_RES bit
// if set to 1, the device is in full resolution mode, where the output resolution increases with the
//   g range set by the range bits to maintain a 4mg/LSB scal factor
// if set to 0, the device is in 10-bit mode, and the range buts determine the maximum g range
//   and scale factor
void ADXL345::setFullResBit(bool fullResBit) {
  setRegisterBit(ADXL345_DATA_FORMAT, 3, fullResBit);

// Gets the state of the justify bit
bool ADXL345::getJustifyBit() {
  return getRegisterBit(ADXL345_DATA_FORMAT, 2);

// Sets the JUSTIFY bit
// if sets to 1 selects the left justified mode
// if sets to 0 selects right justified mode with sign extension
void ADXL345::setJustifyBit(bool justifyBit) {
  setRegisterBit(ADXL345_DATA_FORMAT, 2, justifyBit);

// set/get the gain for each axis in Gs / count
void ADXL345::setAxisGains(float *_gains){
  int i;
  for(i = 0; i < 3; i++){
    gains[i] = _gains[i];
void ADXL345::getAxisGains(float *_gains){
  int i;
  for(i = 0; i < 3; i++){
    _gains[i] = gains[i];

// Sets the OFSX, OFSY and OFSZ bytes
// OFSX, OFSY and OFSZ are user offset adjustments in twos complement format with
// a scale factor of 15,6mg/LSB
// OFSX, OFSY and OFSZ should be comprised between
void ADXL345::setAxisOffset(int x, int y, int z) {
  writeTo(ADXL345_OFSX, byte (x)); 
  writeTo(ADXL345_OFSY, byte (y)); 
  writeTo(ADXL345_OFSZ, byte (z)); 

// Gets the OFSX, OFSY and OFSZ bytes
void ADXL345::getAxisOffset(int* x, int* y, int*z) {
  byte _b;
  readFrom(ADXL345_OFSX, 1, &_b); 
  *x = int (_b);
  readFrom(ADXL345_OFSY, 1, &_b); 
  *y = int (_b);
  readFrom(ADXL345_OFSZ, 1, &_b); 
  *z = int (_b);

float ADXL345::getRate(){
  byte _b;
  readFrom(ADXL345_BW_RATE, 1, &_b);
  _b &= B00001111;
  return (pow(2,((int) _b)-6)) * 6.25;

void ADXL345::setRate(float rate){
  byte _b,_s;
  int v = (int) (rate / 6.25);
  int r = 0;
  while (v >>= 1)
  if (r <= 9) {
    readFrom(ADXL345_BW_RATE, 1, &_b);
    _s = (byte) (r + 6) | (_b & B11110000);
    writeTo(ADXL345_BW_RATE, _s);

void ADXL345::set_bw(byte bw_code){
  if((bw_code < ADXL345_BW_3) || (bw_code > ADXL345_BW_1600)){
    status = false;
    error_code = ADXL345_BAD_ARG;
    writeTo(ADXL345_BW_RATE, bw_code);

byte ADXL345::get_bw_code(){
  byte bw_code;
  readFrom(ADXL345_BW_RATE, 1, &bw_code);
  return bw_code;

byte ADXL345::getInterruptSource() {
  byte _b;
  readFrom(ADXL345_INT_SOURCE, 1, &_b);
  return _b;

void print_byte(byte val){
  int i;
  for(i=7; i>=0; i--){
    Serial.print(val >> i & 1, BIN);

Go Up