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
hi, may i know how do u communicate the sensor with the PC?
ReplyDeletethrough uart, you can find an uart library inside the code.
ReplyDeleteon simple way is to connect the tx pin to a tx pin of a ttl to usb device .
which software you use for the 3D real-time? can provide the code and the setting? thanks....
Deletehttp://www.processing.org/, you can file sample code in this project download files.
Deletehey! 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.
ReplyDeletejust compile the code you find here for your Atmega16, it should work!
Deleteror 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 ;)
thanks.but if some how i cannot use scl and sda pins of my Atmega16, can i still connect my adxl345 to it?
Deletei 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.
just look at the Atmel Atmega16 datasheet, Figure 1. Pinout ATmega16, SDA is pin 23, SCL is pin 22
DeleteHello
ReplyDeleteI 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.
to show data as numeric on you terminal, check that you have properly setup baudrate, also check atmega8 FCPU.
Deletethan compile using DEBUG_ENABLED set to 1.
for 3d movement, i use precessing, set DEBUG_ENABLED back to 0, and it should work.
THANKS DEVIDE
Deletehi 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.
Deletei do not understaind what you mean with "axis is changed"
Deleteanyway, 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
}
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
ReplyDeletei've never us it, does the DATA_READY interrupt can help you?
Deletei have chacked all interrupts but nothing is working in my case
ReplyDeletetry 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.
DeleteThank you very much for your suggession
ReplyDeletei am facing new problem now. i am getting this type of data.
ReplyDelete0.00000 0.00000 0.00000 NAN NAN
what can be the reason for it? can you guess?
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.
ReplyDeletei 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?
ReplyDeleteplease do reply
DATA_READY gives you continous interrupt, cause accellerometer sent data, even if you are not moving it, as reference take the accellerometer datasheet
DeleteIf i wanna do something like this:
ReplyDelete1) Sleep my micro controller
2) Wake up when any movement sensed from ADXL345
then what should i do?
use somethink like ADXL362, or MMA7660FC, with auto-wake up mode.
DeleteADXL362?
ReplyDeleteIs it same as ADXL345 in operation WISE?
i think you should get a look to the datasheet.
Deleteof course I will. Thank you very much for your reply
Deletehi 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 ??
ReplyDeletetypical 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.
Deletei 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!!
ReplyDeleteyes, for 16Mhz you can use 100khz for i2c. uart speed can be set to 9600 if you want.
Deletei 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?
Deletestart checking that your uart works, with simple uart_putc.
Deletethen check if simple parameters are sent to terminal.
more in general, do it at small steps.
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,,
ReplyDeleteif 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.
Deletemy program hangs at this line of twimaster.c
Deletewhile(!(TWCR& (1<<TWINT)));
. what could be the reason for this??? can u please help!!
check that you have correctly initializated i2c.
Deletecheck your i2c connection.
try with other i2c device.
check with a logic analyzer.
read i2c registers and debug with those reading.
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??
Deletewith VCC, GND, SCL, SDA connected, it should works.
Deletehi, can you throw some light on how to callibrate the sensor,, some step to step details would help me,, thanks in advance
ReplyDeletefor an accellerometer, you have to adjust gain and offset. Take a look here: http://davidegironi.blogspot.it/2013/01/accellerometer-calibration-helper-01.html
Deletemy 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
ReplyDeletefor the angular displacement, isn't better to use a gyro?
Deletehy, thx for your code, programm !
ReplyDeletecould 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
Thank for your feedback, i've nevere use the adxl330, my comment below is about this adxl345.
DeleteRead 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.
Hello dear friend
ReplyDeleteI'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
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.
DeleteHi Davide, thanks for your work!
ReplyDeleteI 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?
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
DeleteThe 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.
hello davide can you send me arduino code my mail id is anvesh.mech993@gmail.com plzz
DeleteHello. 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.
DeleteThis comment has been removed by the author.
ReplyDeleteHi,
ReplyDeletePlease can you tell me whether we can map the X Y Z values from ADXL345 on an X-Y graph on processing ?
Please help !!!
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.
DeleteHi,
ReplyDeleteI 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!
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.
Deletehi , I could not download library if possible Email it to me.
ReplyDeleteThank
mojtabarosee1@gmail.com
Hello, you've got mail.
Delete