Pages: [1]   Go Down
Author Topic: ADXL345  (Read 572 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to hook up my 3 axis accelerometer which has the ADXL345 on it (a similar device is sold from sparkfun.com).
I used the code exactly from http://codeyoung.blogspot.com/2009/11/adxl345-accelerometer-breakout-board.html

BUT all I get is 0 0 0 as an output.

I have an arduino mega if that makes a difference?

HELP!!!
« Last Edit: May 16, 2010, 05:23:18 pm by baronep » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
have an arduino mega if that makes a difference

It does.
What pins did you connect the I2C lines to?

Should be pin 20 and 21 on the Mega, not the analogue pins as it is on the Duemilanove.
« Last Edit: May 17, 2010, 01:24:54 am by GrooveFlotilla » Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino da King
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also check your analogReference. Are you using internal or external. Ok this should not affect on I2C bus but still check it or you might get weird results.

--jp
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@jpkh - I'm curious - how does the source of the analogue reference voltage affect I2C?
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

no, I had the i2c connected to 20 & 21 as labeled on the board
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does the accelerometer have any other i2c registers you could read for a sanity check?
Does it have a sleep mode?
Any alternate addresses that need to be jumpered?
Is the i2c giving any errors or timeouts?
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't believe that it has any other i2c registers. I also tried using the spi interface and I kept receiving an output of 255 which is '11111111' in binary so I don't understand whats going on? Is there a way to test and see if the part works?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the device does have other registers - see the datasheet.
Do you have access to an oscilloscope?
Logged

"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.

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't believe that it has any other i2c registers
Looking at the datasheet, the device has about 30 I2C registers, including a device ID at address zero.

Have you tried some debug prints?
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, sorry. I haven't tried to do that yet, but will the power settings on register 2d affect the ability for it to send even the device id?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I tried to read from the 0x00 register with a power on setting before it and still nothing. I get a -1 returned for any register I test for.

BTW I think I saw on the data sheet LSB (least significant bit first), but in the i2c/spi libraries its MSB??? If so how do i compensate???
« Last Edit: May 18, 2010, 06:51:38 am by baronep » Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you certain you've got SCL and SDA wired correctly?

Can you post your code?
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have moved on from I2c because I was unsure about the address of my device and that doesn't apply with spi and because SPI has better performance. I am not at home but I will post my code when I get there.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is my code:

// define spi bus pins
#define SLAVESELECT 53
#define SPICLOCK 52
#define DATAOUT 51      //MOSI
#define DATAIN 50       //MISO
#define UBLB(a,b)  ( ( (a) << smiley-cool | (b) )
#define UBLB19(a,b) ( ( (a) << 16 ) | (b) )

//Addresses
#define REVID 0x00      //ASIC Revision Number

char rev_in_byte;          
int temp_in;
unsigned long x;
unsigned long y;
unsigned long z;

void setup()
{
  byte clr;
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SPICLOCK,OUTPUT);
  pinMode(SLAVESELECT,OUTPUT);
  digitalWrite(SLAVESELECT,HIGH); //disable device  
  
  SPCR = B01011111; //MPIE=0, SPE=1 (on), DORD=0 (MSB first), MSTR=1 (master), CPOL=0 (clock idle when low), CPHA=0 (samples MOSI on rising edge), SPR1=0 & SPR0=0 (500kHz)
  
  clr=SPSR;
  clr=SPDR;
  delay(10);
  Serial.begin(9600);
  delay(500);

  Serial.print("Command Register set to ");
  Serial.println(SPCR, DEC);

  char tmp = read_register(0x31);

  Serial.print("Read from reg 0x31 ");
  Serial.println(tmp, DEC);

  rev_in_byte = read_register(REVID);
  
  Serial.print("Device id ");
  Serial.println(rev_in_byte, DEC);

  tmp &= 0xEF;

  Serial.print("Writing back to reg 0x31 ");
  Serial.println(tmp, DEC);

  write_register(0x31, tmp);
  
  Serial.println("Set to wakeup ");
  write_register(0x2d, 0x08);
  
  char tmp2 = read_register(0x31);

  Serial.print("Re - Read from reg 0x31 ");
  Serial.println(tmp2, DEC);

  Serial.println("Setup complete");
}

void loop()
{
  // Todo
  char tmp3 = read_register(0x00);
  Serial.print("Read from reg 0x00 ");
  Serial.println(tmp3, DEC);
  delay(250);
}

char spi_transfer(volatile char data)
{
  SPDR = data;                    // Start the transmission
  while (!(SPSR & (1<<SPIF)))     // Wait for the end of the transmission
  {
  };
  return SPDR;                    // return the received byte
}


char read_register(char register_name)
{
    char in_byte;
    register_name <<= 2;
    register_name &= B11110100; //Read command
  
    digitalWrite(SLAVESELECT,LOW); //Select SPI Device
    delay(10);
    spi_transfer(register_name); //Write byte to device
    in_byte = spi_transfer(0x00); //Send nothing, but we should get back the register value
    digitalWrite(SLAVESELECT,HIGH);
    delay(10);    
    return(in_byte);

}

void write_register(char register_name, char register_value)
{
    register_name <<= 2;
    register_name |= B00000010; //Write command

    digitalWrite(SLAVESELECT,LOW); //Select SPI device
    spi_transfer(register_name); //Send register location
    spi_transfer(register_value); //Send value to record into register
    digitalWrite(SLAVESELECT,HIGH);
}
Logged

Pages: [1]   Go Up
Jump to: