{"id":1552,"date":"2019-05-03T18:46:00","date_gmt":"2019-05-03T10:46:00","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1552"},"modified":"2020-05-12T10:43:36","modified_gmt":"2020-05-12T02:43:36","slug":"how-to-use-a-esp32-development-board-to-read-from-an-adxl345-accelerometer","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-a-esp32-development-board-to-read-from-an-adxl345-accelerometer\/","title":{"rendered":"How to use a ESP32 development board to read from an ADXL345 accelerometer"},"content":{"rendered":"<p>When you want to detect movement of your <a href=\"https:\/\/www.techcoil.com\/glossary\/internet-of-things\/\" rel=\"noopener\" target=\"_blank\">IOT<\/a> project, you can use an <a href=\"https:\/\/www.amazon.com\/Adafruit-ADXL345-Triple-Axis-Accelerometer-ADA1231\/dp\/B01BT4N9BC\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=298b65c26703ad1d9cf7a530577487de&language=en_US\" rel=\"noopener\" target=\"_blank\">ADXL345 accelerometer<\/a>. With this in mind, let's look at how we can use a <a href=\"https:\/\/www.amazon.com\/HiLetgo-ESP-WROOM-32-Development-Microcontroller-Integrated\/dp\/B0718T232Z\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=6db31c4b16a66ea4217fdb8cd41a15d5&language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a> to read from an ADXL345 accelerometer.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP32-development-board-connected-to-ADXL345-accelerometer-sensor.jpg\" alt=\"ESP32 development board connected to ADXL345 accelerometer sensor\"\/><\/p>\n<h2>Wiring your ADXL345 sensor to your ESP32 development board<\/h2>\n<p>When you look at your ADXL345 sensor, you will find several pins: <strong>GND<\/strong>, <strong>3v3<\/strong>, <strong>CS<\/strong>, <strong>INT1<\/strong>, <strong>INT2<\/strong>, <strong>SDO<\/strong>, <strong>SDA<\/strong> and <strong>SCL<\/strong>. <\/p>\n<p>In order to read accelerometer values from the ESP32 development board, we need to connect:<\/p>\n<ul>\n<li><strong>GND<\/strong> pin of ADXL345 to one of the <strong>GND<\/strong> pin on the ESP32 development board<\/li>\n<li><strong>3v3<\/strong> pin of ADXL345 to one of the <strong>3v3<\/strong> pin on the ESP32 development board<\/li>\n<li><strong>SDA<\/strong> pin of ADXL345 to <strong>GPIO21<\/strong> on the ESP32 development board<\/li>\n<li><strong>SCL<\/strong> pin of ADXL345 to <strong>GPIO22<\/strong> pin on the ESP32 development board<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ADXL345-sensor-with-gpio-wires-connected-to-gnd-3v3-sda-and-scl-pins.jpg\" alt=\"ADXL345 sensor with gpio wires connected to gnd 3v3 sda and scl pins\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP32-development-board-with-gpio-wires-connected-to-3v3-gnd-gpio21-amd-gpio22-pins.jpg\" alt=\"ESP32 development board with gpio wires connected to 3v3 gnd gpio21 amd gpio22 pins\" \/><\/p>\n<h2>Enabling ESP32 Development on Arduino IDE<\/h2>\n<p>Since it is easy to write code and flash programs with Arduino IDE, we can use it to get our program into our ESP32 development board.<\/p>\n<p>In order to use Arduino IDE for this guide, be sure to <a href=\"https:\/\/www.techcoil.com\/blog\/enabling-esp32-development-on-arduino-ide\/\" rel=\"noopener\" target=\"_blank\">enable ESP32 development on Arduino IDE<\/a> before continuing.<\/p>\n<h2>Installing the required libraries from Adafruit to read from an ADXL345 accelerometer<\/h2>\n<p>After you had started your Arduino IDE, go to <strong>Tools -> Manage Libraries...<\/strong>. When you do that, the <strong>Library Manager<\/strong> window will appear.<\/p>\n<p>First, install the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" rel=\"noopener\" target=\"_blank\">Adafruit Unified Sensor Driver<\/a> library. In order to do so, filter the list of libraries with <strong>adafruit unified<\/strong>, locate <strong>Adafruit Unified Sensor by Adafruit<\/strong> and click on <strong>Install<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-IDE-1.8.9-Library-Manager-with-Adafruit-Unified-Sensor-as-one-of-the-installation-options.gif\" alt=\"Mac Arduino IDE 1.8.9 Library Manager with Adafruit Unified Sensor as one of the installation options\" \/><\/p>\n<p>Next, install the <a href=\"https:\/\/github.com\/adafruit\/Adafruit_ADXL345\" rel=\"noopener\" target=\"_blank\">Adafruit ADXL345 library<\/a>. In order to do so, filter the list of libraries with <strong>adafruit adxl345<\/strong>, locate <strong>Adafruit ADXL345 by Adafruit<\/strong> and click on <strong>Install<\/strong>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-IDE-1.8.9-Library-Manager-with-Adafruit-adxl345-library-as-one-of-the-installation-options.gif\" alt=\"Mac Arduino IDE 1.8.9 Library Manager with Adafruit adxl345 library as one of the installation options\" \/><\/p>\n<p>After you had installed these libraries, you will be able to use them to read from the ADXL345 accelerometer.<\/p>\n<h2>Flashing the example code from Adafruit<\/h2>\n<p>When you install the Adafruit ADXL345 library, you will find an example that shows you how to use the library. <\/p>\n<p>So where can you find the example to use the ADXL345 accelerometer?<\/p>\n<p>When you navigate to <strong><your_arduino_ide_sketchbook_location>\/libraries\/Adafruit_ADXL345\/examples\/sensortest<\/strong>, you should find a file named <strong>sensortest.ino<\/strong>: <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_Sensor.h&gt;\r\n#include &lt;Adafruit_ADXL345_U.h&gt;\r\n\r\n\/* Assign a unique ID to this sensor at the same time *\/\r\nAdafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);\r\n\r\nvoid displaySensorDetails(void)\r\n{\r\n  sensor_t sensor;\r\n  accel.getSensor(&amp;sensor);\r\n  Serial.println(&quot;------------------------------------&quot;);\r\n  Serial.print  (&quot;Sensor:       &quot;); Serial.println(sensor.name);\r\n  Serial.print  (&quot;Driver Ver:   &quot;); Serial.println(sensor.version);\r\n  Serial.print  (&quot;Unique ID:    &quot;); Serial.println(sensor.sensor_id);\r\n  Serial.print  (&quot;Max Value:    &quot;); Serial.print(sensor.max_value); Serial.println(&quot; m\/s^2&quot;);\r\n  Serial.print  (&quot;Min Value:    &quot;); Serial.print(sensor.min_value); Serial.println(&quot; m\/s^2&quot;);\r\n  Serial.print  (&quot;Resolution:   &quot;); Serial.print(sensor.resolution); Serial.println(&quot; m\/s^2&quot;);  \r\n  Serial.println(&quot;------------------------------------&quot;);\r\n  Serial.println(&quot;&quot;);\r\n  delay(500);\r\n}\r\n\r\nvoid displayDataRate(void)\r\n{\r\n  Serial.print  (&quot;Data Rate:    &quot;); \r\n  \r\n  switch(accel.getDataRate())\r\n  {\r\n    case ADXL345_DATARATE_3200_HZ:\r\n      Serial.print  (&quot;3200 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_1600_HZ:\r\n      Serial.print  (&quot;1600 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_800_HZ:\r\n      Serial.print  (&quot;800 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_400_HZ:\r\n      Serial.print  (&quot;400 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_200_HZ:\r\n      Serial.print  (&quot;200 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_100_HZ:\r\n      Serial.print  (&quot;100 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_50_HZ:\r\n      Serial.print  (&quot;50 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_25_HZ:\r\n      Serial.print  (&quot;25 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_12_5_HZ:\r\n      Serial.print  (&quot;12.5 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_6_25HZ:\r\n      Serial.print  (&quot;6.25 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_3_13_HZ:\r\n      Serial.print  (&quot;3.13 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_1_56_HZ:\r\n      Serial.print  (&quot;1.56 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_0_78_HZ:\r\n      Serial.print  (&quot;0.78 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_0_39_HZ:\r\n      Serial.print  (&quot;0.39 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_0_20_HZ:\r\n      Serial.print  (&quot;0.20 &quot;); \r\n      break;\r\n    case ADXL345_DATARATE_0_10_HZ:\r\n      Serial.print  (&quot;0.10 &quot;); \r\n      break;\r\n    default:\r\n      Serial.print  (&quot;???? &quot;); \r\n      break;\r\n  }  \r\n  Serial.println(&quot; Hz&quot;);  \r\n}\r\n\r\nvoid displayRange(void)\r\n{\r\n  Serial.print  (&quot;Range:         +\/- &quot;); \r\n  \r\n  switch(accel.getRange())\r\n  {\r\n    case ADXL345_RANGE_16_G:\r\n      Serial.print  (&quot;16 &quot;); \r\n      break;\r\n    case ADXL345_RANGE_8_G:\r\n      Serial.print  (&quot;8 &quot;); \r\n      break;\r\n    case ADXL345_RANGE_4_G:\r\n      Serial.print  (&quot;4 &quot;); \r\n      break;\r\n    case ADXL345_RANGE_2_G:\r\n      Serial.print  (&quot;2 &quot;); \r\n      break;\r\n    default:\r\n      Serial.print  (&quot;?? &quot;); \r\n      break;\r\n  }  \r\n  Serial.println(&quot; g&quot;);  \r\n}\r\n\r\nvoid setup(void) \r\n{\r\n#ifndef ESP8266\r\n  while (!Serial); \/\/ for Leonardo\/Micro\/Zero\r\n#endif\r\n  Serial.begin(9600);\r\n  Serial.println(&quot;Accelerometer Test&quot;); Serial.println(&quot;&quot;);\r\n  \r\n  \/* Initialise the sensor *\/\r\n  if(!accel.begin())\r\n  {\r\n    \/* There was a problem detecting the ADXL345 ... check your connections *\/\r\n    Serial.println(&quot;Ooops, no ADXL345 detected ... Check your wiring!&quot;);\r\n    while(1);\r\n  }\r\n\r\n  \/* Set the range to whatever is appropriate for your project *\/\r\n  accel.setRange(ADXL345_RANGE_16_G);\r\n  \/\/ accel.setRange(ADXL345_RANGE_8_G);\r\n  \/\/ accel.setRange(ADXL345_RANGE_4_G);\r\n  \/\/ accel.setRange(ADXL345_RANGE_2_G);\r\n  \r\n  \/* Display some basic information on this sensor *\/\r\n  displaySensorDetails();\r\n  \r\n  \/* Display additional settings (outside the scope of sensor_t) *\/\r\n  displayDataRate();\r\n  displayRange();\r\n  Serial.println(&quot;&quot;);\r\n}\r\n\r\nvoid loop(void) \r\n{\r\n  \/* Get a new sensor event *\/ \r\n  sensors_event_t event; \r\n  accel.getEvent(&amp;event);\r\n \r\n  \/* Display the results (acceleration is measured in m\/s^2) *\/\r\n  Serial.print(&quot;X: &quot;); Serial.print(event.acceleration.x); Serial.print(&quot;  &quot;);\r\n  Serial.print(&quot;Y: &quot;); Serial.print(event.acceleration.y); Serial.print(&quot;  &quot;);\r\n  Serial.print(&quot;Z: &quot;); Serial.print(event.acceleration.z); Serial.print(&quot;  &quot;);Serial.println(&quot;m\/s^2 &quot;);\r\n  delay(500);\r\n}\r\n<\/pre>\n<p>Open that file with your Arduino IDE and upload it to your ESP32 development board.<\/p>\n<p>After you upload it you should see similar output in the serial monitor:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-IDE-1.8.9-serial-monitor-output-for-Adafruit_ADXL345-example.jpg\" alt=\"Mac Arduino IDE 1.8.9 serial monitor output for Adafruit_ADXL345 example\"\/><\/p>\n<h2>Writing our own mini example program to read from the ADXL345 accelerometer<\/h2>\n<p>Since we have an example from Adafruit, it is easy to write our own program to read from the ADXL345 accelerometer. <\/p>\n<p>Given that in mind, the following is an example sketch to read from the ADXL345 accelerometer:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_Sensor.h&gt;\r\n#include &lt;Adafruit_ADXL345_U.h&gt;\r\n \r\n\/*Initialize an instance of Adafruit_ADXL345_Unified with a unique id*\/\r\nAdafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);\r\n\r\nvoid setup() {\r\n\r\n  Serial.begin(9600);\r\n  Serial.println(&quot;&quot;);\r\n  Serial.println(&quot;Small example to read from ADXL345 accelerometer&quot;);\r\n  \r\n}\r\n\r\nvoid loop() {\r\n  \r\n  \/*Read from ADXL345 accelerometer*\/\r\n  sensors_event_t event; \r\n  accel.getEvent(&amp;event);\r\n\r\n  \/* Display the results (acceleration is measured in m\/s^2) *\/\r\n  Serial.print(&quot;X: &quot;); Serial.print(event.acceleration.x); Serial.print(&quot;  &quot;);\r\n  Serial.print(&quot;Y: &quot;); Serial.print(event.acceleration.y); Serial.print(&quot;  &quot;);\r\n  Serial.print(&quot;Z: &quot;); Serial.print(event.acceleration.z); Serial.print(&quot;  &quot;);Serial.println(&quot;m\/s^2 &quot;);\r\n  \/*Take a one second break*\/\r\n  delay(1000);\r\n\r\n}\r\n<\/pre>\n<p>When you upload this program to your ESP32 development board, output similar to the following will appear on your Arduino IDE serial monitor:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-1.8.9-Serial-Monitor-showing-output-from-own-ESP32-reading-from-ADXL345-sensor.gif\" alt=\"Mac Arduino 1.8.9 Serial Monitor showing output from own ESP32 reading from ADXL345 sensor\" \/><\/p>\n<p><img decoding=\"async\" width=\"600\" height=\"900\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-use-a-ESP32-development-board-to-read-from-an-ADXL345-accelerometer.jpg\" alt=\"How to use a ESP32 development board to read from an ADXL345 accelerometer\" class=\"aligncenter size-full wp-image-1941\" \/><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-p2\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-p2&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-p2&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1552&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you want to detect movement of your <a href=\"https:\/\/www.techcoil.com\/glossary\/internet-of-things\/\" rel=\"noopener\" target=\"_blank\">IOT<\/a> project, you can use an <a href=\"https:\/\/www.amazon.com\/Adafruit-ADXL345-Triple-Axis-Accelerometer-ADA1231\/dp\/B01BT4N9BC\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=298b65c26703ad1d9cf7a530577487de&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">ADXL345 accelerometer<\/a>. With this in mind, let&#8217;s look at how we can use a <a href=\"https:\/\/www.amazon.com\/HiLetgo-ESP-WROOM-32-Development-Microcontroller-Integrated\/dp\/B0718T232Z\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=6db31c4b16a66ea4217fdb8cd41a15d5&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">ESP32 development board<\/a> to read from an ADXL345 accelerometer.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP32-development-board-connected-to-ADXL345-accelerometer-sensor.jpg\" alt=\"ESP32 development board connected to ADXL345 accelerometer sensor\"\/><\/p>\n","protected":false},"author":1,"featured_media":1558,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[635,636,588,630,484],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/ESP32-development-board-connected-to-ADXL345-accelerometer-sensor.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-p2","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1552"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1552"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1552\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1558"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}