Pages: [1]   Go Down
Author Topic: ADXL345 Accelerometer on I2C Arduino Due  (Read 1587 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have connected an ADXl345 to a due wired as suggested in this tutorial: http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html
and I have an issue with the data I receive. For some reason all I get is -1,-1,-1 for the data values. I had a similar issue when I tried it with SPI, and abandoned it because I don't have the ability to connect to the SPI pins on the Due. While I haven't checked tonight, I will connect this setup to my Arduino Uno and see if the problem persists (I have a hunch it won't.) If that is the case, are there any suggestions on what I should do to get it working on the Due?
Thank you for your help
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I checked using an Uno, and was able to get accurate data, so I don't really have any idea why I get the data I do on the Due. Unfortunately I need the accelerometer to function on an Arduino Due. Can anyone elaborate on this issue? Thank you
« Last Edit: June 07, 2013, 09:03:55 am by maysarahs » Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be easier to help if you could post your code. Or are you directly copy and pasting from the tutorial you linked? Do you think it is possible that the issue is caused because the Due uses 32 bit int's?

I know you said you had to use i2c but the following SPI example works on the Due and may be of some help:
Code:
/*
  Calibrate ADXL345
 
  Use this sketch to get offset and scaling factor to calibrate the ADXL345.
  Orient the sensor so that one of the six axes is aligned with gravity, type
  the character 'r' in the serial terminal to reset the min/max values, then
  slowly move the sensor around until the values stop increasing/decreasing.
  Use those values to calculate the proper scaling and offsets.
*/


// pins 4, 10, and 52 allow auto chip selection on the Due
#define ACCEL_SS_PIN  10 // SPI slave select pin for accel

// general SPI defines
#define READ   0x80
#define WRITE  0x00
#define MULTI  0x40
#define SINGLE 0x00

#include <SPI.h> // built in SPI library

uint8_t accel_buffer[6];
int16_t accel_raw[3];
int16_t accel_max[3] = {0,0,0};
int16_t accel_min[3] = {0,0,0};

void setup() {
  Serial.begin(115200);
  Serial.println("Serial communication initialized...");
 
  // initialize the SPI bus for the accelerometer
  SPI.begin(ACCEL_SS_PIN);
  SPI.setBitOrder(ACCEL_SS_PIN, MSBFIRST);
  SPI.setDataMode(ACCEL_SS_PIN, SPI_MODE3);
  // ADXL345 max clock speed is 5 MHz
  SPI.setClockDivider(ACCEL_SS_PIN, (F_CPU / 5000000L) + 1);
 
  // make sure that the device is connected
  SPI.transfer(ACCEL_SS_PIN, READ | SINGLE | 0x00, SPI_CONTINUE);
  uint8_t accel_name = SPI.transfer(ACCEL_SS_PIN, 0x00);
 
  if(accel_name != 0xE5) {
    Serial.println("ADXL345 not detected!");
    Serial.print(accel_name, HEX); Serial.println(" found, should be 0xE5");
    delay(1000);
  }
 
  // Register 0x2C: BW_RATE
  // use 2X oversampling with 400 Hz ODR (0x0C)
  // 800 Hz ODR (0x0D) is also available
  SPI.transfer(ACCEL_SS_PIN, WRITE | SINGLE | 0x2C, SPI_CONTINUE);
  SPI.transfer(ACCEL_SS_PIN, 0x0C);
 
  // Register 0x2D: POWER_CTL
  // put into measurement mode (0x08)
  SPI.transfer(ACCEL_SS_PIN, WRITE | SINGLE | 0x2D, SPI_CONTINUE);
  SPI.transfer(ACCEL_SS_PIN, 0x08);
 
  // Register 0x31: DATA_FORMAT
  // full resolution (0x08) at range of +/- 16 g (0x03)
  SPI.transfer(ACCEL_SS_PIN, WRITE | SINGLE | 0x31, SPI_CONTINUE);
  SPI.transfer(ACCEL_SS_PIN, 0x08 | 0x03);
 
  delay(100);
}


void loop() {
 
  // multibyte burst read of data registers (from 0x32 to 0x37)
  SPI.transfer(ACCEL_SS_PIN, READ | MULTI | 0x32, SPI_CONTINUE);
 
  for(int i = 0; i < 5; i++) {
    accel_buffer[i] = SPI.transfer(ACCEL_SS_PIN, 0x00, SPI_CONTINUE);
  }
  accel_buffer[5] = SPI.transfer(ACCEL_SS_PIN, 0x00);
 
  // combine the raw data (ADXL345 sends LSB first by default)
  //             _________ MSB ________   _____ LSB _____
  accel_raw[0] = (accel_buffer[1] << 8) | accel_buffer[0];
  accel_raw[1] = (accel_buffer[3] << 8) | accel_buffer[2];
  accel_raw[2] = (accel_buffer[5] << 8) | accel_buffer[4];
 
  for(int i = 0; i < 3; i++) {
    if(accel_raw[i] > accel_max[i]) {
      accel_max[i] = accel_raw[i];
    }
    if(accel_raw[i] < accel_min[i]) {
      accel_min[i] = accel_raw[i];
    }
   
    Serial.print(accel_min[i], DEC); Serial.print("/");
    Serial.print(accel_max[i], DEC); Serial.print("\t");
  }
  Serial.println();
 
  // listen to serial for a reset signal (character 'r')
  if(Serial.available() > 0) {
   
    int val = Serial.read();
   
    if(val == ((int) 'r')) {
      for(int i = 0; i < 3; i++) {
        accel_min[i] = 0;
        accel_max[i] = 0;
      }
    }
  }
 
  delay(50);
}
 
Logged

Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I´m trying the code for SPI but it  says FF found, what means that the accel is not conected, I´m using 10, 11, 12 and 13 pins, are they the correct ones?.

thanks
Roberto

Logged

The Holy Land
Offline Offline
Jr. Member
**
Karma: 1
Posts: 60
Arduino stones
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, not sure if this still relevant as three weeks have passed since your last post, but which pins are you using for data on the Due?
Logged

Pages: [1]   Go Up
Jump to: