Pages: [1] 2   Go Down
Author Topic: How to read register value from ADXL345 Eval board?  (Read 3679 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI i am working on a project regarding using ADXL345 eval board (http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXLEvalBoard%20User_Guide2009-09-14.pdf)
and arduino Uno and few servo motors (as outputs)

Now i met a bottleneck. The connection is direct connection from adxl 345eval board to arduino. So i need to get the value out from the register (0x32 till 0x37) in adxl345. In the first stage, i am using a LED to indicate whether i can read the register value or not..but it fail anyway. Anyone can please check the error for me?

Btw noted that direct connection from adxl345 eval board to arduino is using USB connection. Arduino Uno -> ADXL345 eval board (USB connected)

Here is my code:
Code:
int ledPin = 2;                 // LED connected to digital pin 2
#include <Wire.h>
#define DEVICE (0x53)   //define the address for DEVICE

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(57600);  // start serial for output
  writeTo(DEVICE, 0x2D, 0);         //here is the part that I enable the bit in the register in 2D and 2E. for INT and Power configuration
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
  writeTo(DEVICE, 0x2E, 128);     
  writeTo(DEVICE, 0x2E, 2);
  writeTo(DEVICE, 0x2E, 1);
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

The read and write function:
Code:
void readfrom(int device, byte address)
{
  int temp = 0;
  Wire.beginTransmission(device); //start transmission to device
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission

  Wire.beginTransmission(device);
  Wire.requestFrom(device,1);
 
  while(Wire.available())
  {
    int c = Wire.read();
    if (c != temp){
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);}              // waits for a second
  else{
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000); }                 // waits for a second
  temp =c;
  }//end while loop.
  Wire.endTransmission(); //end transmission
}

void writeTo(int device, byte address, byte val) {
   Wire.beginTransmission(device); //start transmission to device
   Wire.write(address);        // send register address
   Wire.write(val);        // send value to write
   Wire.endTransmission(); //end transmission
}

The loop function:
Code:
readfrom (0x53, address);

Obviously i am using I2C library....now i am wondering that will this code works if i am using USB connection?any replied is highly appreciate... smiley
« Last Edit: March 19, 2012, 07:57:11 am by tlslaj0417 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In readFrom() you have an extra Wire.beginTransmission().
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

In readFrom() you have an extra Wire.beginTransmission().

sorry i have a mistake just now, i correct my code already. it is not extra Wire.beginTransmission(). just i forgot to end the transmission since i begin another transmission again...

Now the 1st consideration is that i am USB connection. so is that problem if i am using I2C for coding??
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The schematic (http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345_Eval_Board-v11.pdf) shows the ADXL345 is connected to A0,A1,A2,and A3 and appears to use Soft SPI.  Nothing is connected to the I2C bus so the Wire library won't help you any. Perhaps you should start with the source code from the product page (http://www.sparkfun.com/products/9814)
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

The schematic (http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345_Eval_Board-v11.pdf) shows the ADXL345 is connected to A0,A1,A2,and A3 and appears to use Soft SPI.  Nothing is connected to the I2C bus so the Wire library won't help you any. Perhaps you should start with the source code from the product page (http://www.sparkfun.com/products/9814)

thanks so much...before that i noticed from schematic diagram, it seem like did not using I2C but SPI.I cant confirm..and i have no advanced people who can guide me on this...thanks...but i did try the source code but it is not arduino code...it is just normal C programming code regarding on this board..may i know what can i do on this code?and what software u can recommend me to compile it?...

sorry for all these noob question. i am a beginner by the way. Thanks
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some of the functions can be adapted:
Code:
#define ACCEL_CS A0
#define ACCEL_SCK A1
#define ACCEL_DI A2
#define ACCEL_DO A3

#define SELECT_ACCEL()        digitalWrite(ACCEL_CS, LOW)
#define UNSELECT_ACCEL()    digitalWrite(ACCEL_CS, HIGH)

#define CLOCK_HIGH()         digitalWrite(ACCEL_SCK, HIGH)
#define CLOCK_LOW() digitalWrite(ACCEL_SCK, LOW)

char adxl345_read(char register_address){
char read_address=0x80 | register_address;
char register_value=0;

CLOCK_HIGH();
SELECT_ACCEL(); //Lower CS pin.

for(int bit=7; bit>=0; bit--){
CLOCK_LOW();
                digitalWrite(DI, read_address & (1<<bit));
delayMicroseconds(1);
CLOCK_HIGH();
delayMicroseconds(1);
}

for(int bit=7; bit>=0; bit--){
CLOCK_LOW();
delayMicroseconds(1);

CLOCK_HIGH();
delayMicroseconds(1);

register_value |= digitalRead(ADXL_DI)<<bit;
}

UNSELECT_ACCEL();

return register_value;
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

Some of the functions can be adapted:
Code:
#define ACCEL_CS A0
#define ACCEL_SCK A1
#define ACCEL_DI A2
#define ACCEL_DO A3

#define SELECT_ACCEL()        digitalWrite(ACCEL_CS, LOW)
#define UNSELECT_ACCEL()    digitalWrite(ACCEL_CS, HIGH)

#define CLOCK_HIGH()         digitalWrite(ACCEL_SCK, HIGH)
#define CLOCK_LOW() digitalWrite(ACCEL_SCK, LOW)

char adxl345_read(char register_address){
char read_address=0x80 | register_address;
char register_value=0;

CLOCK_HIGH();
SELECT_ACCEL(); //Lower CS pin.

for(int bit=7; bit>=0; bit--){
CLOCK_LOW();
                digitalWrite(DI, read_address & (1<<bit));
delayMicroseconds(1);
CLOCK_HIGH();
delayMicroseconds(1);
}

for(int bit=7; bit>=0; bit--){
CLOCK_LOW();
delayMicroseconds(1);

CLOCK_HIGH();
delayMicroseconds(1);

register_value |= digitalRead(ADXL_DI)<<bit;
}

UNSELECT_ACCEL();

return register_value;
}

from your advice, i notice that some are defined frm A0 - A3....may i know what i can use for this?is that for analog pin?because my intention is for usb connection..

i had modify my code to be like this:
setup:
Code:
int ledPin = 2;                 // LED connected to digital pin 13
int ledPin1 = 3;
int _spiPin;
byte buff[6] ;
#include <SPI.h>
#define DEVICE 0x00
#define TO_READ (6)

void setup()
{
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  Serial.begin(57600);  // start serial for output
  writeTo(DEVICE, 0x2D, 0);     
  writeTo(DEVICE, 0x2D, 16);
  writeTo(DEVICE, 0x2D, 8);
  writeTo(DEVICE, 0x2E, 128);     
  writeTo(DEVICE, 0x2E, 2);
  writeTo(DEVICE, 0x2E, 1);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);  // sets the digital pin as output
}

loop:
Code:
void loop()
{
  byte address = 0x32;
  int temp;
  int x,y,z;
  readfrom (DEVICE, address, TO_READ, buff);
  x = (((int)buff[1]) << 8) | buff[0];
  if (x!=temp){
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);}              // waits for a second
  else{
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000); }                 // waits for a second
  temp = x;
}
read and write function:
Code:
void readfrom(int device, byte address, int num, byte buff[])
{
  int temp = 0;
  address = 0x80 | address;
  if (num > 1) address = address | 0x40;
 
  digitalWrite(_spiPin, LOW);
  SPI.transfer(address);             // sends address to read from
  for(int i = 0; i < num; i++)
  {
    buff[i] = SPI.transfer(0x00);
  }
  //if (!SPI.available())digitalWrite(ledPin1, HIGH);
  digitalWrite(_spiPin, HIGH);
}

void writeTo(int device, byte address, byte val) {
  digitalWrite(_spiPin, LOW);
  SPI.transfer(address);             // send register address
  SPI.transfer(val);                // send value to write
  digitalWrite(_spiPin, HIGH);
}

i change it to use SPI...and why still wont work??i use a temp variable...to test whether the value is changing or not..if changing, then the LED would light up...but still fail to work on it........thanks and so appreciate your time..
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i change it to use SPI...and why still wont work??

See reply #3.  The accelerometer is NOT connected to the hardware SPI pins.  It is connected to A0-A3 (Pins 14-17).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

i change it to use SPI...and why still wont work??

See reply #3.  The accelerometer is NOT connected to the hardware SPI pins.  It is connected to A0-A3 (Pins 14-17).

A0-A3 u mean is ADC0 till ADC3??(pin 23-26)...because pins 14-17 is for SPI right?....correct me if i am wrong...thanks
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A0-A3 u mean is ADC0 till ADC3??(pin 23-26)...because pins 14-17 is for SPI right?....correct me if i am wrong...thanks

ARDUINO pins 14-17 a.k.a. A0-A3.  Yes, they are pins 23-26 on the ATmega chip.

Hardware SPI is on ARDUINO pins 10-13 which are pins 16-19 on the ATmega.

If you are going to use the Arduino libraries you should probably get used to calling the pins by their Arduino numbers to avoid confusion.  You can learn more about the naming here: https://spreadsheets.google.com/spreadsheet/pub?key=0AtfNMvfWhA_ccnRId19SNmVWTDE0MEtTOV9HOEdQa0E&gid=0
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

A0-A3 u mean is ADC0 till ADC3??(pin 23-26)...because pins 14-17 is for SPI right?....correct me if i am wrong...thanks

ARDUINO pins 14-17 a.k.a. A0-A3.  Yes, they are pins 23-26 on the ATmega chip.

Hardware SPI is on ARDUINO pins 10-13 which are pins 16-19 on the ATmega.

If you are going to use the Arduino libraries you should probably get used to calling the pins by their Arduino numbers to avoid confusion.  You can learn more about the naming here: https://spreadsheets.google.com/spreadsheet/pub?key=0AtfNMvfWhA_ccnRId19SNmVWTDE0MEtTOV9HOEdQa0E&gid=0

thanks for good reference..i will refer it when i calling the pins. and now i use the code u provided to me. as you said:
#define ACCEL_CS   A0
#define ACCEL_SCK   A1
#define ACCEL_DI           A2
#define ACCEL_DO   A3

then which pins i should connect to with all these 4 pins?or because of soft connection. i no need to connect it through any hardware?i just define it with these four pins?
Logged

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


ARDUINO pins 14-17 a.k.a. A0-A3.  Yes, they are pins 23-26 on the ATmega chip.

Hardware SPI is on ARDUINO pins 10-13 which are pins 16-19 on the ATmega.

If you are going to use the Arduino libraries you should probably get used to calling the pins by their Arduino numbers to avoid confusion.  You can learn more about the naming here: https://spreadsheets.google.com/spreadsheet/pub?key=0AtfNMvfWhA_ccnRId19SNmVWTDE0MEtTOV9HOEdQa0E&gid=0

hi John....sorry for use up your time for this. In the progress i edit my code till like this:
setup:
Code:
char adxl345_read(char address);
int ledPin = 2;
#define ACCEL_CS A0
#define ACCEL_SCK A1
#define ACCEL_DI A2
#define ACCEL_DO A3
#define DATAX0 0x32 //X-Axis Data 0
#define DATAX1 0x33 //X-Axis Data 1
#define DATAY0 0x34 //Y-Axis Data 0
#define DATAY1 0x35 //Y-Axis Data 1
#define DATAZ0 0x36 //Z-Axis Data 0
#define DATAZ1 0x37 //Z-Axis Data 1
#define SELECT_ACCEL()        digitalWrite(ACCEL_CS, LOW)
#define UNSELECT_ACCEL()    digitalWrite(ACCEL_CS, HIGH)

#define CLOCK_HIGH()         digitalWrite(ACCEL_SCK, HIGH)
#define CLOCK_LOW() digitalWrite(ACCEL_SCK, LOW)
#include <SPI.h>

void setup(){
  SPI.begin();
  Serial.begin(57600);
  pinMode(ledPin, OUTPUT);
}
read function:
Code:
char adxl345_read(char register_address){
char read_address=0x80 | register_address;
char register_value=0;
        char temp = 0;

CLOCK_HIGH();
SELECT_ACCEL(); //Lower CS pin.

        if (register_value!=temp)
            {
              digitalWrite(ledPin, HIGH);   // sets the LED on
              delay(1000);
             }
        else{
        digitalWrite(ledPin, LOW);    // sets the LED off
        delay(1000); }                 // waits for a second

for(int bit=7; bit>=0; bit--){
CLOCK_LOW();
                digitalWrite(ACCEL_DI, read_address & (1<<bit));
delayMicroseconds(1);
CLOCK_HIGH();
delayMicroseconds(1);
}

for(int bit=7; bit>=0; bit--){
CLOCK_LOW();
delayMicroseconds(1);

CLOCK_HIGH();
delayMicroseconds(1);

register_value |= digitalRead(ACCEL_DI)<<bit;
}

UNSELECT_ACCEL();
        temp = register_value;
return register_value;
}
Loop:
Code:
void loop(){
  adxl345_read(DATAX0);
}

same i use ledpin to indicate the availability of the register_value....but still no given any response....following the reference that u gave:
https://spreadsheets.google.com/spreadsheet/pub?key=0AtfNMvfWhA_ccnRId19SNmVWTDE0MEtTOV9HOEdQa0E&gid=0

i connect the A0-A3 to pin 11-13 (while pin 10 is not for SPI right?)...so is there any error?

Again, thanks for sparing your time on me. Appreciate so much smiley
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8821
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Probably need:
Code:
    pinMode(ACCEL_CS, OUTPUT);
    pinMode(ACCEL_SCK, OUTPUT);
    pinMode(ACCEL_DI, OUTPUT);
    pinMode(ACCEL_DO, INPUT);

You don't seem to include any output in your sketch so I don't understand how you detect if it is working or not.

You can remove references to the SPI library since the adxl345_read() function does software SPI.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

Probably need:
Code:
    pinMode(ACCEL_CS, OUTPUT);
    pinMode(ACCEL_SCK, OUTPUT);
    pinMode(ACCEL_DI, OUTPUT);
    pinMode(ACCEL_DO, INPUT);

You don't seem to include any output in your sketch so I don't understand how you detect if it is working or not.

You can remove references to the SPI library since the adxl345_read() function does software SPI.

while i am testing my circuit...would u mind to tell me what is the exact of software SPI and hardware SPI???software is that mean that is not exact hardware connection???..thanks

Plus: i do output to test it is working or not..i test with light up a LED...smiley
i test it if the register value change all the time thats mean register != temp (not equal to the value before) then the LED is light up.
Code:
if (register_value!=temp)
            {
              digitalWrite(ledPin, HIGH);   // sets the LED on
              delay(1000);
             }
« Last Edit: March 22, 2012, 09:00:33 am by tlslaj0417 » Logged

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

Hi, i follow your advise, add all these
    pinMode(ACCEL_CS, OUTPUT);
    pinMode(ACCEL_SCK, OUTPUT);
    pinMode(ACCEL_DI, OUTPUT);
    pinMode(ACCEL_DO, INPUT);

but still no giving any output.... smiley-confuse...really stuck now...
Logged

Pages: [1] 2   Go Up
Jump to: