{"id":1460,"date":"2023-01-08T05:52:02","date_gmt":"2023-01-08T05:52:02","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1460"},"modified":"2023-01-08T05:52:05","modified_gmt":"2023-01-08T05:52:05","slug":"working-with-stm32-and-acceleration-sensor-adxl335-analog-sensor","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1460","title":{"rendered":"Working with STM32 and Acceleration Sensor: ADXL335 Analog Sensor"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"693\" height=\"379\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Untitled.png\" alt=\"\" class=\"wp-image-1462\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Untitled.png 693w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Untitled-300x164.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Untitled-400x219.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Untitled-250x137.png 250w\" sizes=\"(max-width: 693px) 100vw, 693px\" \/><figcaption>ADXL335<\/figcaption><\/figure><\/div>\n\n\n\n<p>In this guide, we shall see how to interface ADXL335 Accelerometer sensor with STM32F4. Since this sensor is analog based sensor, we shall use DMA to handle the data from the adxl335. To setup multichannel ADC with DMA, please refer to this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=319\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=319\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/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>ADXL335.<\/li><li>Connection.<\/li><li>Developing the code<\/li><li>Code.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. ADXL335:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>The ADXL335 is a small, thin, low power, complete 3-axis accel- erometer with signal conditioned voltage outputs. The product measures acceleration with a minimum full-scale range of \u00b13&nbsp;<em>g<\/em>. It can measure the static acceleration of gravity in tilt-sensing applications, as well as dynamic acceleration resulting from motion, shock, or vibration.<\/p>\n\n\n\n<p>The user selects the bandwidth of the accelerometer using the CX, CY, and CZ\u00a0capacitors at the XOUT, YOUT, and ZOUT\u00a0pins. Bandwidths can be selected to suit the application, with a range of 0.5 Hz to 1600 Hz for the X and Y axes, and a range of 0.5 Hz to 550 Hz for the Z axis.<\/p>\n\n\n\n<p><strong>FEATURES<\/strong><\/p>\n\n\n\n<p><strong>3-axis sensing<br>Small, low profile package<\/strong><\/p>\n\n\n\n<p><strong>4 mm \u00d7 4 mm \u00d7 1.45 mm LFCSP<br>Low power : 350 \u03bcA (typical) Single-supply operation: 1.8 V to 3.6 V 10,000&nbsp;<\/strong><strong><em>g&nbsp;<\/em><\/strong><strong>shock survival<\/strong><\/p>\n\n\n\n<p><strong>Excellent temperature stability<br>BW adjustment with a single capacitor per axis RoHS\/WEEE lead-free compliant<\/strong><\/p>\n\n\n\n<p><strong>APPLICATIONS<\/strong><\/p>\n\n\n\n<p><strong>Cost sensitive, low power, motion- and tilt-sensing applications<br>Mobile devices<br>Gaming systems<\/strong><\/p>\n\n\n\n<p><strong>Disk drive protection Image stabilization Sports and health devices<\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"824\" height=\"780\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.26.45-AM.png\" alt=\"\" class=\"wp-image-1463\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.26.45-AM.png 824w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.26.45-AM-300x284.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.26.45-AM-768x727.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.26.45-AM-750x710.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.26.45-AM-400x379.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.26.45-AM-250x237.png 250w\" sizes=\"(max-width: 824px) 100vw, 824px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Connection:<\/h2>\n\n\n\n<p>The connection as following:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><td>ADXL335 <\/td><td>STM32F4 Nucleo-64<\/td><\/tr><tr><td>Vcc<\/td><td>5V<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>Xout<\/td><td>PA0<\/td><\/tr><tr><td>Yout<\/td><td>PA1<\/td><\/tr><tr><td>Zout<\/td><td>PA2<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"667\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-1024x667.png\" alt=\"\" class=\"wp-image-1464\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-1024x667.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-300x195.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-768x500.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-1536x1000.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-2048x1334.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-1150x749.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-750x488.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-400x260.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.17.05-AM-250x163.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3. Developing the code:<\/h2>\n\n\n\n<p>We start off by creating new c source file and header file with the name of adxl335.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>With in header file, we shall declare a data structure to hold the acceleration values 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;}\">typedef struct acc_data_t{\nfloat accx;\nfloat accy;\nfloat accz;\n}acc_data_t;<\/pre><\/div>\n\n\n\n<p>Then the two following functions 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;}\">void adxl335_init();\n\nvoid get_accel_data(acc_data_t *acc);<\/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 ADXL335_H_\n#define ADXL335_H_\n\n#include &quot;stdint.h&quot;\n\ntypedef struct acc_data_t{\nfloat accx;\nfloat accy;\nfloat accz;\n}acc_data_t;\n\n\nvoid adxl335_init();\n\nvoid get_accel_data(acc_data_t *acc);\n\n\n#endif \/* ADXL335_H_ *\/\n<\/pre><\/div>\n\n\n\n<p>Now, for the source code,  we start with adxl335_init which will be initializing the ADC with DMA.<\/p>\n\n\n\n<p>The ADC with DMA for multichannel configuration 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;}\">#define LEN_3_CH 0x02\n\n#define CH0 0x00\n#define CH1 0x01\n#define CH4 0x04\n\nuint16_t acc_data[3];\n\n\nvoid adxl335_init()\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\t\/*Configure PA0, PA1 and PA4 as analog*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0|GPIO_MODER_MODE1|GPIO_MODER_MODE4;\n\n\t\/*Enable clock access to ADC1*\/\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN;\n\n\t\/*Enable scan mode *\/\n\n\tADC1-&gt;CR1|=ADC_CR1_SCAN;\n\n\n\t\/*Enable the following:\n\t * DMA request.\n\t * Continuous mode.\n\t * *\/\n\n\tADC1-&gt;CR2|=ADC_CR2_CONT|ADC_CR2_DDS|ADC_CR2_DMA;\n\n\t\/*Set the length of conversion to be 3.*\/\n\tADC1-&gt;SQR1|=(LEN_3_CH&lt;&lt;ADC_SQR1_L_Pos);\n\n\t\/*Set the sequence as following:\n\t * PA0(CH0), PA1(CH1), PA4(CH4)\n\t * *\/\n\n\tADC1-&gt;SQR3|=(CH0&lt;&lt;ADC_SQR3_SQ1_Pos)|(CH1&lt;&lt;ADC_SQR3_SQ2_Pos)|(CH4&lt;&lt;ADC_SQR3_SQ3_Pos);\n\n\t\/*DMA Setup*\/\n\n\t\/*DMA related setup*\/\n\tRCC-&gt;AHB1ENR\t\t|=RCC_AHB1ENR_DMA2EN;\n\tDMA2_Stream0-&gt;CR\t&amp;=~DMA_SxCR_EN;\n\twhile(DMA2_Stream0-&gt;CR ==DMA_SxCR_EN){;}\n\tDMA2_Stream0-&gt;CR\t|=DMA_SxCR_MSIZE_0|DMA_SxCR_PSIZE_0|DMA_SxCR_MINC|DMA_SxCR_CIRC;\n\tDMA2_Stream0-&gt;PAR\t =(uint32_t)(&amp;(ADC1-&gt;DR));\n\tDMA2_Stream0-&gt;M0AR \t =(uint32_t )(acc_data);\n\tDMA2_Stream0-&gt;NDTR\t =3;\n\n\t\/*Launch the DMA*\/\n\tDMA2_Stream0-&gt;CR \t|=DMA_SxCR_EN;\n\n\t\/*Launch the ADC*\/\n\tADC1-&gt;CR2\t\t\t|=ADC_CR2_ADON;\n\n\tADC1-&gt;CR2|=ADC_CR2_SWSTART;\n\n\n}<\/pre><\/div>\n\n\n\n<p>After that, we need a helper function that will convert the ADC value to acceleration 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\nstatic float adc_to_acc(uint16_t adc_value)\n{\n\treturn  ((( ( (float)(adc_value * 3.3)\/RawMax) - 1.65 ) \/ 0.330)*9.81);\n\n}<\/pre><\/div>\n\n\n\n<p>Now for the get_accel_data function. Since the function take a pointer to the data structure acc_data_t as pointer, it is possible to get the members of the structure using -&gt; operator.<\/p>\n\n\n\n<p>Hence, we can get the acceleration values 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;}\">\nvoid get_accel_data(acc_data_t *acc)\n{\n\n\tfloat ax_scalled=adc_to_acc(acc_data[0]);\n\tfloat ay_scalled=adc_to_acc(acc_data[1]);\n\tfloat az_scalled=adc_to_acc(acc_data[2]);\n\n\n\tacc-&gt;accx=ax_scalled;\n\tacc-&gt;accy=ay_scalled;\n\tacc-&gt;accz=az_scalled;\n\n\n}\n<\/pre><\/div>\n\n\n\n<p>Hence, the source 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;stm32f4xx.h&quot;\n#include &quot;adxl335.h&quot;\n\n#define LEN_3_CH 0x02\n\n#define CH0 0x00\n#define CH1 0x01\n#define CH4 0x04\n\n#define RawMax  4095\n\nuint16_t acc_data[3];\n\n\nvoid adxl335_init()\n{\n\t\/*Enable clock access to GPIOA*\/\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\t\/*Configure PA0, PA1 and PA4 as analog*\/\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODE0|GPIO_MODER_MODE1|GPIO_MODER_MODE4;\n\n\t\/*Enable clock access to ADC1*\/\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN;\n\n\t\/*Enable scan mode *\/\n\n\tADC1-&gt;CR1|=ADC_CR1_SCAN;\n\n\n\t\/*Enable the following:\n\t * DMA request.\n\t * Continuous mode.\n\t * *\/\n\n\tADC1-&gt;CR2|=ADC_CR2_CONT|ADC_CR2_DDS|ADC_CR2_DMA;\n\n\t\/*Set the length of conversion to be 3.*\/\n\tADC1-&gt;SQR1|=(LEN_3_CH&lt;&lt;ADC_SQR1_L_Pos);\n\n\t\/*Set the sequence as following:\n\t * PA0(CH0), PA1(CH1), PA4(CH4)\n\t * *\/\n\n\tADC1-&gt;SQR3|=(CH0&lt;&lt;ADC_SQR3_SQ1_Pos)|(CH1&lt;&lt;ADC_SQR3_SQ2_Pos)|(CH4&lt;&lt;ADC_SQR3_SQ3_Pos);\n\n\t\/*DMA Setup*\/\n\n\t\/*DMA related setup*\/\n\tRCC-&gt;AHB1ENR\t\t|=RCC_AHB1ENR_DMA2EN;\n\tDMA2_Stream0-&gt;CR\t&amp;=~DMA_SxCR_EN;\n\twhile(DMA2_Stream0-&gt;CR ==DMA_SxCR_EN){;}\n\tDMA2_Stream0-&gt;CR\t|=DMA_SxCR_MSIZE_0|DMA_SxCR_PSIZE_0|DMA_SxCR_MINC|DMA_SxCR_CIRC;\n\tDMA2_Stream0-&gt;PAR\t =(uint32_t)(&amp;(ADC1-&gt;DR));\n\tDMA2_Stream0-&gt;M0AR \t =(uint32_t )(acc_data);\n\tDMA2_Stream0-&gt;NDTR\t =3;\n\n\t\/*Launch the DMA*\/\n\tDMA2_Stream0-&gt;CR \t|=DMA_SxCR_EN;\n\n\t\/*Launch the ADC*\/\n\tADC1-&gt;CR2\t\t\t|=ADC_CR2_ADON;\n\n\tADC1-&gt;CR2|=ADC_CR2_SWSTART;\n\n\n}\n\n\n\nstatic float adc_to_acc(uint16_t adc_value)\n{\n\treturn  ((( ( (float)(adc_value * 3.3)\/RawMax) - 1.65 ) \/ 0.330)*9.81);\n\n}\n\n\nvoid get_accel_data(acc_data_t *acc)\n{\n\n\tfloat ax_scalled=adc_to_acc(acc_data[0]);\n\tfloat ay_scalled=adc_to_acc(acc_data[1]);\n\tfloat az_scalled=adc_to_acc(acc_data[2]);\n\n\n\tacc-&gt;accx=ax_scalled;\n\tacc-&gt;accy=ay_scalled;\n\tacc-&gt;accz=az_scalled;\n\n\n}\n<\/pre><\/div>\n\n\n\n<p>For main.c, first add the adxl335 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;adxl335.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare the structure that will hold the data:<\/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;}\">acc_data_t adxl335_data;<\/pre><\/div>\n\n\n\n<p>Within main function:<\/p>\n\n\n\n<p>Initialize the sensor:<\/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;}\">adxl335_init();<\/pre><\/div>\n\n\n\n<p>Within while(1) loop:<\/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;}\">get_accel_data(&amp;adxl335_data);<\/pre><\/div>\n\n\n\n<p>print the 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;}\">printf(&quot;Acc  x=%0.2f y=%0.2f z=%0.2f\\r\\n&quot;,adxl335_data.accx,adxl335_data.accy,adxl335_data.accz);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. 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\/2023\/01\/adxl335.zip\">adxl335<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/adxl335.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\">5. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>After uploading the code, open serial terminal program and set the baudrate to 115200 and you should get the following results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"701\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-1024x701.png\" alt=\"\" class=\"wp-image-1466\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-1024x701.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-300x205.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-768x526.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-1150x787.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-750x513.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-400x274.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM-250x171.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2023\/01\/Screenshot-2023-01-08-at-8.39.13-AM.png 1166w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall see how to interface ADXL335 Accelerometer sensor with STM32F4. Since this sensor is analog based sensor, we shall use DMA to handle the data from the adxl335. To setup multichannel ADC with DMA, please refer to this guide here. In this guide, we shall cover the following: ADXL335. Connection. Developing [&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-1460","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\/1460"}],"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=1460"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1460\/revisions"}],"predecessor-version":[{"id":1467,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1460\/revisions\/1467"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}