Advertise here

ADXL345 Accelerometer sensor with SAMD21 Xplained pro

ADXL345 accelerometer  sensor & SAMD21 Xplained pro




in this article I will demonstrate to you how to use accelerometer ADXL343/ ADXL344/ADXL345 with Microchip SAM Devices based on ASF4 (here I used SAMD21 Xplained pro).



The library I wrote currently support I2C bus but soon will add a support for SPI as well.

connection:


 SAMD21 Xplained pro     Sensor

    PA08                  >>    SDA
PA09                  >>    SC
 VCC(3.3V)        >>   3.3V
                  GND                 >>   GND                 





to start the library you have to enable I2C Sync driver first and set the I2C slave device address

i2c_m_sync_set_slaveaddr(&I2C_ADXL345, ADXL34x_ADDRESS, I2C_M_SEVEN);

i2c_m_sync_enable(&I2C_ADXL345);

initialize the library and should return 0 if success.

adxl34x_init(&adxl345);

set the Data rate of the sensor using:

adxl34x_set_DataRate(&adxl345,ADXL34x_DATARATE_100_HZ);

set the sensor range from 2g to 16g using
adxl34x_set_Range(&adxl345,ADXL343_RANGE_16_G);

to get the xyz readings use:
adxl34x_refresh_xyz(&adxl345);
so the readings will be ready after that by calling:

adxl345.raw_x
adxl345.raw_y
adxl345.raw_z

the value will be from -256 to 256

if you want to calibrate the sensor (offset values) use:
adxl34x_set_zeroOffset(&adxl345);


after you upload the code and open the serial monitor (115200 bps) you should see the results as following


3 comments:

  1. when i connected samd21 explained pro with ADXL345 PA08-> SDA PA09->SCL And in my terminal i see all data are 0. any suggestions there ? I appreciate your help.

    ReplyDelete

Powered by Blogger.