{"id":1298,"date":"2022-10-26T05:33:34","date_gmt":"2022-10-26T05:33:34","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1298"},"modified":"2022-10-26T05:33:37","modified_gmt":"2022-10-26T05:33:37","slug":"working-with-stm32-and-acceleration-sensor-adxl345-in-i2c-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1298","title":{"rendered":"Working with STM32 and Acceleration Sensor: ADXL345 in I2C mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"333\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L.jpg\" alt=\"\" class=\"wp-image-1299\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L.jpg 500w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L-300x200.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L-400x266.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/41RNUutQb0L-250x167.jpg 250w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall see how to interface STM32F4 with acceleration sensor ADXL345 using I2C mode. <\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Feature of ADXL345.<\/li><li>Developing the driver.<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to \u00b116 g. Digital output data is formatted as 16-bit twos complement and is accessible through either a SPI (3- or 4-wire) or I2 C digital interface.<\/p>\n\n\n\n<p>The ADXL345 is well suited for mobile device applications. It measures the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion or shock. Its high resolution (3.9 mg\/LSB) enables measurement of inclination changes less than 1.0\u00b0.<\/p>\n\n\n\n<p>Several special sensing functions are provided. Activity and inactivity sensing detect the presence or lack of motion by comparing the acceleration on any axis with user-set thresholds. Tap sensing detects single and double taps in any direction. Freefall sensing detects if the device is falling. These functions can be mapped individually to either of two interrupt output pins. An integrated memory management system with a 32-level first in, first out (FIFO) buffer can be used to store data to minimize host processor activity and lower overall system power consumption.<\/p>\n\n\n\n<p>Low power modes enable intelligent motion-based power management with threshold sensing and active acceleration measurement at extremely low power dissipation.<\/p>\n\n\n\n<p>The ADXL345 is supplied in a small, thin, 3 mm \u00d7 5 mm \u00d7 1 mm, 14-lead, plastic package.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"682\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-682x1024.png\" alt=\"\" class=\"wp-image-1300\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-682x1024.png 682w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-200x300.png 200w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-768x1153.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-1023x1536.png 1023w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-1150x1727.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-750x1126.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-400x601.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM-250x375.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.36-AM.png 1168w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"671\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-1024x671.png\" alt=\"\" class=\"wp-image-1301\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-1024x671.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-300x197.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-768x504.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-1150x754.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-750x492.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-400x262.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM-250x164.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-7.53.54-AM.png 1214w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Developing the Driver:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Since the utilize the I2C communication, the I2C pins of STM32F411 Nucleo-64 is  used as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"674\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout-1024x674.png\" alt=\"\" class=\"wp-image-1302\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout-1024x674.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout-300x198.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout-768x506.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout-750x494.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout-400x263.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout-250x165.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/I2C_ADXL345_pinout.png 1116w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the required function as as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>I2C write single byte (from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=524\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=524\" target=\"_blank\">here<\/a>).<\/li><li>I2C read multiple bytes (from <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=549\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=549\" target=\"_blank\">here<\/a>).<\/li><\/ul>\n\n\n\n<p>After acquiring the required drivers for I2C, start off by creating a new header file with name of adxl345.h.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Within the header file, declare enum which holds the acceleration range:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef enum\n{\n\taccl_2g=0,\n\taccl_4g=1,\n\taccl_8g=2,\n\taccl_16g=3\n}adxl345Parameters;<\/pre><\/div>\n\n\n\n<p>The ranges as follows which they have been taken from the datasheet of adxl345.:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"365\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-1024x365.png\" alt=\"\" class=\"wp-image-1303\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-1024x365.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-300x107.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-768x274.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-1150x410.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-750x268.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-400x143.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM-250x89.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.03.36-AM.png 1530w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Also, declare struct which will hold the acceleration results:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">typedef struct\n{\n\n\tfloat ax;\n\tfloat ay;\n\tfloat az;\n\n}accleration_values_t;<\/pre><\/div>\n\n\n\n<p>Also, the following functions:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void adxl345_init(adxl345Parameters param);\nvoid adxl345_update();\nvoid adxl345_get_values(accleration_values_t * values);\n\n<\/pre><\/div>\n\n\n\n<p>Hence, the header file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#ifndef ADXL345_H_\n#define ADXL345_H_\n\n#include &quot;stdint.h&quot;\n\ntypedef enum\n{\n\taccl_2g=0,\n\taccl_4g=1,\n\taccl_8g=2,\n\taccl_16g=3\n}adxl345Parameters;\n\ntypedef struct\n{\n\n\tfloat ax;\n\tfloat ay;\n\tfloat az;\n\n}accleration_values_t;\nSe\nvoid adxl345_init(adxl345Parameters param);\nvoid adxl345_update();\nvoid adxl345_get_values(accleration_values_t * values);\n\n\n\n\n\n\n#endif \/* ADXL345_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>Now, create adxl345.c source file and include adxl345.h and the i2c header file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;adxl345.h&quot;\n#include &quot;i2c.h&quot;<\/pre><\/div>\n\n\n\n<p>Also define some registers and the address of adxl345:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define DEVICE_ADDR    \t\t\t(0x53) \/*adxl345 address*\/\n#define DATA_FORMAT_R   \t\t(0x31) \/*data format register*\/\n#define POWER_CTL_R \t\t\t(0x2D) \/*Power control register*\/\n#define DATA_START_ADDR\t\t\t(0x32) \/*Start address to read the values*\/\n\n#define\tRESET\t\t\t\t\t(0x00) \/*Reset value*\/\n#define SET_MEASURE_B\t\t    (0x08) \/*Put adxl345 into measurement mode*\/<\/pre><\/div>\n\n\n\n<p>Variables that hold the values:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint8_t data_rec[6];\nuint8_t acc_range;\nint16_t x,y,z;<\/pre><\/div>\n\n\n\n<p>Now, to initialize the adxl345, the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set the acceleration range.<\/li><li>Reset the power register.<\/li><li>Set the sensor in measurement mode.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"178\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-1024x178.png\" alt=\"\" class=\"wp-image-1304\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-1024x178.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-300x52.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-768x134.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-1150x200.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-750x130.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-400x70.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM-250x43.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.21.49-AM.png 1530w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"188\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-1024x188.png\" alt=\"\" class=\"wp-image-1305\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-1024x188.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-300x55.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-768x141.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-1150x211.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-750x137.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-400x73.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM-250x46.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.23.14-AM.png 1452w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void adxl345_init(adxl345Parameters param)\n\t{\n\t\tacc_range=param;\n\t\ti2c_writeByte(DEVICE_ADDR, DATA_FORMAT_R, param);\n\t\ti2c_writeByte(DEVICE_ADDR, POWER_CTL_R, RESET);\n\t\ti2c_writeByte(DEVICE_ADDR,POWER_CTL_R, SET_MEASURE_B);\n\t}<\/pre><\/div>\n\n\n\n<p>To update the sensor data read multiple data starting from address of DATAX0 up to DATAZ1:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void adxl345_update()\n\t{\n\n\n\n\t\ti2c_ReadMulti(DEVICE_ADDR, DATA_START_ADDR, 6, (char*)data_rec);\n\n\t\tx = ((data_rec[1]&lt;&lt;8)|data_rec[0]);\n\t\ty = ((data_rec[3]&lt;&lt;8)|data_rec[2]);\n\t\tz = ((data_rec[5]&lt;&lt;8)|data_rec[4]);\n\n\t}\n<\/pre><\/div>\n\n\n\n<p>To get the new values:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void adxl345_get_values(accleration_values_t * values)\n\t{\n\tfloat divider;\n\tswitch(acc_range)\n\t{\n\t\tcase accl_2g: divider=0.003906; \/*1\/256*\/\tbreak;\n\t\tcase accl_4g: divider=0.0078125;\/*1\/128*\/\tbreak;\n\t\tcase accl_8g: divider=0.01563;\t\/*1\/64*\/\tbreak;\n\t\tcase accl_16g: divider=0.03125;\t\/*1\/32*\/\tbreak;\n\t}\n\n\t\tvalues-&gt;ax=x*divider;\n\t\tvalues-&gt;ay=y*divider;\n\t\tvalues-&gt;az=z*divider;\n\t}<\/pre><\/div>\n\n\n\n<p>To get the divider, you need to divide the result by the sensitivity:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"150\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-1024x150.png\" alt=\"\" class=\"wp-image-1306\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-1024x150.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-300x44.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-768x112.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-1536x225.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-2048x300.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-1150x168.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-750x110.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-400x59.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.26.04-AM-250x37.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Then store the values to the structure passed by the user.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the source file as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\n#include &quot;adxl345.h&quot;\n#include &quot;i2c.h&quot;\n\n\n\n#define DEVICE_ADDR    \t\t\t(0x53) \/*adxl345 address*\/\n#define DATA_FORMAT_R   \t\t(0x31) \/*data format register*\/\n#define POWER_CTL_R \t\t\t(0x2D) \/*Power control register*\/\n#define DATA_START_ADDR\t\t\t(0x32) \/*Start address to read the values*\/\n\n#define\tRESET\t\t\t\t\t(0x00) \/*Reset value*\/\n#define SET_MEASURE_B\t\t    (0x08) \/*Put adxl345 into measurement mode*\/\n\n\/*Variables which hold some variable*\/\nuint8_t data_rec[6];\nuint8_t acc_range;\nint16_t x,y,z;\nvoid adxl345_init(adxl345Parameters param)\n\t{\n\t\tacc_range=param;\n\t\ti2c_writeByte(DEVICE_ADDR, DATA_FORMAT_R, param);\n\t\ti2c_writeByte(DEVICE_ADDR, POWER_CTL_R, RESET);\n\t\ti2c_writeByte(DEVICE_ADDR,POWER_CTL_R, SET_MEASURE_B);\n\t}\n\n\nvoid adxl345_update()\n\t{\n\n\n\n\t\ti2c_ReadMulti(DEVICE_ADDR, DATA_START_ADDR, 6, (char*)data_rec);\n\n\t\tx = ((data_rec[1]&lt;&lt;8)|data_rec[0]);\n\t\ty = ((data_rec[3]&lt;&lt;8)|data_rec[2]);\n\t\tz = ((data_rec[5]&lt;&lt;8)|data_rec[4]);\n\n\t}\n\nvoid adxl345_get_values(accleration_values_t * values)\n\t{\n\tfloat divider;\n\tswitch(acc_range)\n\t{\n\t\tcase accl_2g: divider=0.003906; \/*1\/256*\/\tbreak;\n\t\tcase accl_4g: divider=0.0078125;\/*1\/128*\/\tbreak;\n\t\tcase accl_8g: divider=0.01563;\t\/*1\/64*\/\tbreak;\n\t\tcase accl_16g: divider=0.03125;\t\/*1\/32*\/\tbreak;\n\t}\n\n\t\tvalues-&gt;ax=x*divider;\n\t\tvalues-&gt;ay=y*divider;\n\t\tvalues-&gt;az=z*divider;\n\t}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In main.c file:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;i2c.h&quot;\n#include &quot;delay.h&quot;\n#include &quot;adxl345.h&quot;\n#include &quot;stdio.h&quot;\naccleration_values_t accleration_values;\nextern void uart2_rxtx_init(void);\nint main(void)\n{\n\tuart2_rxtx_init();\n\ti2c_init();\n\tadxl345_init(accl_4g);\n\twhile(1)\n\t\t{\n\t\t\tadxl345_update();\n\t\t\tadxl345_get_values(&amp;accleration_values);\n\t\t\tprintf(&quot;ax=%0.5f\\tay=%0.5f\\taz=%0.5f\\r\\n&quot;,accleration_values.ax,accleration_values.ay,accleration_values.az);\n\t\t\tdelay(20);\n\n\t\t}\n\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\n\n\n\n<p>You may download the code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/ADXL345.zip\">ADXL345<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/ADXL345.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>Open your serial terminal application and set the baudrate to 115200 and you should get the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"710\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-1024x710.png\" alt=\"\" class=\"wp-image-1308\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-1024x710.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-300x208.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-768x533.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-1150x798.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-750x520.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-400x277.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM-250x173.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/10\/Screenshot-2022-10-26-at-8.31.58-AM.png 1168w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to interface STM32F4 with acceleration sensor ADXL345 using I2C mode. In this guide, we shall cover the following: Feature of ADXL345. Developing the driver. Code. Results. The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to \u00b116 g. Digital output [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,12],"tags":[],"class_list":["post-1298","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1298"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1298"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1298\/revisions"}],"predecessor-version":[{"id":1309,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1298\/revisions\/1309"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}