Monday, October 22, 2012

AVR Atmega ADXL345 accelerometer library 02 + processing

ADXL345 3-axis Accelerometer AVR Atmega library.



The ADXL345 is a small, thin, low power, 3-axis accelerometer with high
resolution (13-bit) measurement at up to ±16 g. Digital output data is
formatted as 16-bit twos complement and is acces-sible through either
a SPI (3- or 4-wire) or I2C digital interface.


User can setup measurement mode to 2/4/8/16G.
Calibration is suggested to get more accurate paramenters.


Setup parameters are stored in file adxl345.h
This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.


Code


Notes
  • read risk disclaimer
  • excuse my bad english

58 comments:

  1. hi, may i know how do u communicate the sensor with the PC?

    ReplyDelete
  2. through uart, you can find an uart library inside the code.
    on simple way is to connect the tx pin to a tx pin of a ttl to usb device .

    ReplyDelete
    Replies
    1. which software you use for the 3D real-time? can provide the code and the setting? thanks....

      Delete
    2. http://www.processing.org/, you can file sample code in this project download files.

      Delete
  3. hey! i am a newbie. i want to connect ADXL345 to atmega16. how can i do it and get the readings on the lcd. how ADC pins has to be used for the same. thank you.

    ReplyDelete
    Replies
    1. just compile the code you find here for your Atmega16, it should work!
      ror the pin look at the datasheed of your microcontroller to connect the correct one.
      for the LCD there are some library you should use, just google for it, i use the peter fleury one.
      good work ;)

      Delete
    2. thanks.but if some how i cannot use scl and sda pins of my Atmega16, can i still connect my adxl345 to it?
      i am sorry but i cannot find any code for adxl345 with atmega16. can u give me the exact link of the code and the pin configuration? thank you.

      Delete
    3. just look at the Atmel Atmega16 datasheet, Figure 1. Pinout ATmega16, SDA is pin 23, SCL is pin 22

      Delete
  4. Hello
    I have used your library for my project using same controller ATmega8. i am using hyper terminal for testing. it working but on terminal i am not receiving proper data. i am getting different symbols. please guide me. and also want to know how to use processing code to see 3D movement.

    ReplyDelete
    Replies
    1. to show data as numeric on you terminal, check that you have properly setup baudrate, also check atmega8 FCPU.
      than compile using DEBUG_ENABLED set to 1.
      for 3d movement, i use precessing, set DEBUG_ENABLED back to 0, and it should work.

      Delete
    2. hi Davide would you please help me to understand Interrupt pin function of ADXL345. i want ADXL345 to give interrupt to my ATmega8 if data is available or axis is changed.

      Delete
    3. i do not understaind what you mean with "axis is changed"
      anyway, you could find a list of all the event triggered by your accelleromenter at page 20 (INTERRUPTS) of the ADXL345 (analog devices) datasheet, as example i report you the DOUBLE_TAP
      --> i've not checkd it but this code!, i'm just writing it down here, it could or not works, just try

      for DOUBLE_TAP, set INT_ENABLE reg to 1<<5
      i2c_start_wait(ADXL345_ADDR | I2C_WRITE);
      i2c_write(0x2E);
      i2c_write(1<<5);
      connect INT1 of your accell to INT0 of your Atmega and enable interrupt
      EICRA |= (1 << ISC00); // set INT0 to trigger on ANY logic change
      EIMSK |= (1 << INT0); // Turns on INT0
      then the interrupt handler
      ISR (INT0_vect) {
      // interrupt code here
      }

      Delete
  5. Thanks man. but it's not working. i want my ATmega8 to sleep until the ADXL345 gives interrupt for DATA change. when position of ADXL345 changed my ATmega8 receives interrupt from ADXL and transmit the data to Xbee on air

    ReplyDelete
    Replies
    1. i've never us it, does the DATA_READY interrupt can help you?

      Delete
  6. i have chacked all interrupts but nothing is working in my case

    ReplyDelete
    Replies
    1. try debugging with you scope/logic analiz., first check if data_ready makes the adxl345 works, then check if your atmega int is enabled (check this even without the accellerometer). also try with some other arduino library around here, maybe you can find usefull snippet there.

      Delete
  7. Thank you very much for your suggession

    ReplyDelete
  8. i am facing new problem now. i am getting this type of data.
    0.00000 0.00000 0.00000 NAN NAN
    what can be the reason for it? can you guess?

    ReplyDelete
  9. Hello Devide, I have done some coding and tesing for interrupt request from ADXL345 to ATmega8. i am getting low level interrupt from ADXL to ATmega8 but when it reads ax,ay,az values using adxl345_getdata(&ax, &ay, &az) function, ATmega hangs up and stop running. I am confused why it happens. please throw some lights on my doubts.

    ReplyDelete
  10. i fegure out why ATmega8 was hanging in between while using sleep mode. now i am using ADXL345 in DATA_READY interrupt mode on int2 pin in active low by setting INT_INVERT high. i am getting continues interrupt from ADXL345. Am i doing wrong?
    please do reply

    ReplyDelete
    Replies
    1. DATA_READY gives you continous interrupt, cause accellerometer sent data, even if you are not moving it, as reference take the accellerometer datasheet

      Delete
  11. If i wanna do something like this:
    1) Sleep my micro controller
    2) Wake up when any movement sensed from ADXL345

    then what should i do?

    ReplyDelete
    Replies
    1. use somethink like ADXL362, or MMA7660FC, with auto-wake up mode.

      Delete
  12. ADXL362?
    Is it same as ADXL345 in operation WISE?

    ReplyDelete
    Replies
    1. i think you should get a look to the datasheet.

      Delete
    2. of course I will. Thank you very much for your reply

      Delete
  13. hi davide,, i am having some basic questions. i am using a 5v supply to atmega board. is there some level shifters i should be using before connecting to the sda and scl of the adxl345. please help. whats is the value of vcc shown here ??

    ReplyDelete
    Replies
    1. typical voltages used for sda and scl in adxl345 is +5V, my adxl345 works as in the schematics posted here. connect sda and scl as above. as supply for the adxl345 board i use +5v, but check that your chip is mounted on a board with a voltage regulator, or use a voltage regulator, because supply voltage range for adxl345 is 2.0 V to 3.6 V.

      Delete
  14. i have the same adxl345 breakout board as yours . it has a voltage regulator mounted on it. i found that the scl clock has been defined 10000 l in the program which should have been 100000 for 100khz clock. also i am working with a 16 mhz clock for ATMEGA 16,, SO I CHANGED the uart baud rate to 9600. can u think of any other changes i should be doing in the program to make it work with atmega16. thankyou for your help!!

    ReplyDelete
    Replies
    1. yes, for 16Mhz you can use 100khz for i2c. uart speed can be set to 9600 if you want.

      Delete
    2. i have made sure i have connected things properly. but i am not getting anyting on hyperterminal. i have set debug enabled to 1.crccheck to 0.get atttitude to 0. i ve got a brand new accelerometer. so can u think of where i might get it wrong?

      Delete
    3. start checking that your uart works, with simple uart_putc.
      then check if simple parameters are sent to terminal.
      more in general, do it at small steps.

      Delete
  15. i am pretty sure that my uart works. i ve tested with programs . can u suggest how to check this twi interface. i used a scope in scl pins. i could not get any variations. also how long can this twi wires be? will length be a factor? also ( iam very new) pls forgive,,

    ReplyDelete
    Replies
    1. if you can, use a logic analyzer to check what's going on your i2c bus. at first use short cables (<30cm). use pullup resistor for scl and sda as suggested in the above picture.

      Delete
    2. my program hangs at this line of twimaster.c
      while(!(TWCR& (1<<TWINT)));

      . what could be the reason for this??? can u please help!!

      Delete
    3. check that you have correctly initializated i2c.
      check your i2c connection.
      try with other i2c device.
      check with a logic analyzer.
      read i2c registers and debug with those reading.

      Delete
    4. i also want one more clarification !! my breakout board has cs pin which i have left unconnected. in the datasheet for spi it says that it should be pulled high. also i have left sdo pin unconnected . i see from the video that i ve the same breakout board such as yours!! any suggestions??

      Delete
    5. with VCC, GND, SCL, SDA connected, it should works.

      Delete
  16. hi, can you throw some light on how to callibrate the sensor,, some step to step details would help me,, thanks in advance

    ReplyDelete
    Replies
    1. for an accellerometer, you have to adjust gain and offset. Take a look here: http://davidegironi.blogspot.it/2013/01/accellerometer-calibration-helper-01.html

      Delete
  17. my application is to find the angular displacement,, i am using the sensor to find the steering position of the vehicle. in that case how do i fix the sensor reference

    ReplyDelete
    Replies
    1. for the angular displacement, isn't better to use a gyro?

      Delete
  18. hy, thx for your code, programm !

    could you help me with your programm a bit

    i use an adxl330 which should be nearly the same,
    it works fine and i rewrote the input not on seral change i just read it everytime in the draw loop

    -> whats the advantage of readin on serial event ?

    and how is your data range - from my datasheet the values inputed in my analog inputs x,y,z are between 2,7 and 3,3 so 0,6V are split up in 1023 steps

    i detect the stationary values and substract them so that i get a "ground" 0,0,0

    so how does your program handle the input values - are they in an percentage range ? should i map my input values from 0 to 100

    i scaled them down just for test purposes and made a quick test - it works so far

    but the axis jumps (mainly i guess because of my simple "grounding" initilasing)

    thx for answers

    ReplyDelete
    Replies
    1. Thank for your feedback, i've nevere use the adxl330, my comment below is about this adxl345.
      Read using an event, instead of a loop, it's better cause the event it is catched when the uart has some char to read.
      The raw values read from the accellerometer, are conveted firmware side.
      Gravity is 1g, pressure on axis is (counts @ 1g - counts @ -1g) / sensitivity g, let's suppose for a 2g range a sensitivity of 8-bit (256 counts max, from -128 @ -2g, to 128 @ 2g), the value @ 1g should be 128/2, and -128/2 @ -1g, so (64 - -64) / 2 = 64. Given this value you can convert raw value to g force reading.

      Delete
  19. Hello dear friend
    I'm desperately need help
    In the little time I have to connect my microcontroller with 6 sensors
    The sensors are as follows

    hmc5843
    mp3v5004dp
    sht11
    gt723
    o2a2
    tgs4161

    Do you have the code?
    I am very very very grateful if you help

    ReplyDelete
    Replies
    1. Hello, in this blog you can find code for the compass, i've never tryid it but maybe the sht11 can be compatible whit the dht11, that code you can find here in that blog.

      Delete
  20. Hi Davide, thanks for your work!

    I have a question about the calibration of the sensor.

    In your main.c there is a adxl345_calibrateoffset(); but is commented out in the program code.

    Your adxl345.c has a adxl345_writeoffset in it, but the funktion is never used.
    Where did i geht the offsetx, y, and x values to call the function?

    ReplyDelete
    Replies
    1. Hello the accelcal_docalibrationclient function it is used by my calibration helper script, you can find it here: http://davidegironi.blogspot.it/2013/01/accellerometer-calibration-helper-01.html
      The adxl345_writeoffset it is used to write the offset to the offset adxl345 registers. I do not use this function because i compute the offset inside the microcontroller (see adxl345_getdata function: ADXL345_OFFSETX, ADXL345_OFFSETY, ADXL345_OFFSETZ).
      You can get the offset by calibration the accelerometer. You can use the calibration helper, or even you can not use my helper and run the calibration by hand.

      Delete
    2. hello davide can you send me arduino code my mail id is anvesh.mech993@gmail.com plzz

      Delete
    3. Hello. In this post you can find the code for ATmega compiled with avrgcc. You can build an Arduino library from that code, or you can use the Adafruit ADXL345 Arduino library.

      Delete
  21. This comment has been removed by the author.

    ReplyDelete
  22. Hi,
    Please can you tell me whether we can map the X Y Z values from ADXL345 on an X-Y graph on processing ?
    Please help !!!

    ReplyDelete
    Replies
    1. Hello, I'm sorry but I do not understand what are you asking. The values from this X/Y/Z accel. are mapped to pitch and roll by the adxl345_getpitchroll function. Then pitch and roll are transmitted to the processing sketch.

      Delete
  23. Hi,

    I have the exact component you use in your video and I want to program it for at ATMEGA324A-PU. In order to use it in the I2C mode, is it necessary only to connect the SCL, SDA, 5V and GND pins? I read here https://fluxworkshop.com/products/bdaa100040-adxl345-3-axis-acceleration-module-10-pin that "The method is selected by driving the CS pin to 0V for SPI or 3.3V for I2C.". Shouldn't the CS pin be connected or does the module does this?
    Thanks!

    ReplyDelete
    Replies
    1. Hello, yes you have to connect it using the i2c bus, don'f forget the two pull-up resistors on the SCL,SDA pins. No need to connect the CS PIN.

      Delete
  24. hi , I could not download library if possible Email it to me.

    Thank
    mojtabarosee1@gmail.com

    ReplyDelete