{"id":101364,"date":"2021-01-13T15:04:57","date_gmt":"2021-01-13T15:04:57","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=101364"},"modified":"2025-03-17T11:35:51","modified_gmt":"2025-03-17T11:35:51","slug":"esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino\/","title":{"rendered":"ESP8266 NodeMCU with MPU-6050 Accelerometer, Gyroscope and Temperature Sensor (Arduino)"},"content":{"rendered":"\n<p>In this guide you&#8217;ll learn how to use the MPU-6050 accelerometer and gyroscope module with the ESP8266 NodeMCU. The MPU-6050 IMU (Inertial Measurement Unit) is a 3-axis accelerometer and 3-axis gyroscope sensor. The accelerometer measures the gravitational acceleration and the gyroscope measures the rotational velocity. Additionally, this module also measures temperature. This sensor is ideal to determine the orientation of a moving object.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU MPU-6050 Module Accelerometer Gyroscope Temperature Sensor Arduino\" class=\"wp-image-101199\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure><\/div>\n\n\n<p>We have a similar guide for the ESP32: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-mpu-6050-accelerometer-gyroscope-arduino\/\">ESP32 with MPU-6050 Accelerometer, Gyroscope and Temperature Sensor (Arduino)<\/a><\/p>\n\n\n\n<p>In this guide we&#8217;ll cover two examples:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#1\">Get gyroscope, acceleration and temperature readings (Serial Monitor)<\/a><\/li>\n\n\n\n<li><a href=\"#2\">Display gyroscope and acceleration readings on OLED display<\/a><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing the MPU-6050 Gyroscope Accelerometer Sensor<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/makeradvisor.com\/tools\/mpu-6050-3-axis-accelerometer-and-gyroscope-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">MPU-6050<\/a> is a module with a 3-axis accelerometer and a 3-axis gyroscope.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MPU-6050 Module Accelerometer Gyroscope Temperature Sensor\" class=\"wp-image-101201\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>The gyroscope measures rotational velocity (rad\/s), this is the change of the angular position over time along the X, Y and Z axis (roll, pitch and yaw). This allows us to determine the orientation of an object.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1200\" height=\"1088\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/roll-pitch-yaw.png?resize=1200%2C1088&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Roll Pitch Yaw Angles\" class=\"wp-image-101026\" style=\"width:416px;height:377px\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/roll-pitch-yaw.png?w=1484&amp;quality=100&amp;strip=all&amp;ssl=1 1484w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/roll-pitch-yaw.png?resize=300%2C272&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/roll-pitch-yaw.png?resize=1024%2C928&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/roll-pitch-yaw.png?resize=768%2C696&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p>The accelerometer measures acceleration (rate of change of the object&#8217;s velocity). It senses static forces like gravity (9.8m\/s<sup>2<\/sup>) or dynamic forces like vibrations or movement. The MPU-6050 measures acceleration over the X, Y an Z axis. Ideally, in a static object the acceleration over the Z axis is equal to the gravitational force, and it should be zero on the X and Y axis.<\/p>\n\n\n\n<p>Using the values from the accelerometer, it is possible to calculate the roll and pitch angles using trigonometry. However, it is not possible to calculate the yaw. <\/p>\n\n\n\n<p>We can combine the information from both sensors to get more accurate information about the sensor orientation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MPU-6050 Pinout<\/h2>\n\n\n\n<p>Here&#8217;s the pinout for the MPU-6050 sensor module.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span class=\"rnthl rntcred\">VCC<\/span><\/td><td>Power the sensor (3.3V or 5V)<\/td><\/tr><tr><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><td>Common GND<\/td><\/tr><tr><td><span class=\"rnthl rntclblue\">SCL<\/span><\/td><td>SCL pin for I2C communication (GPIO 5)<\/td><\/tr><tr><td><span class=\"rnthl rntclgreen\">SDA<\/span><\/td><td>SDA pin for I2C communication (GPIO 4)<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">XDA<\/span><\/td><td>Used to interface other I2C sensors with the MPU-6050<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">XCL<\/span><\/td><td>Used to interface other I2C sensors with the MPU-6050<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">AD0<\/span><\/td><td>Use this pin to change the I2C address<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">INT<\/span><\/td><td>Interrupt pin &#8211; can be used to indicate that new measurement data is available<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Arduino IDE<\/h2>\n\n\n\n<p>We\u2019ll program the ESP8266 NodeMCU board using Arduino IDE. So, make sure you have the ESP8266 add-on installed. Follow the next tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\" title=\"\">Install the ESP8266 NodeMCU Board in Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you prefer using VS Code + PlatformIO IDE, follow the next guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/vs-code-platformio-ide-esp32-esp8266-arduino\/\">Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Libraries<\/h3>\n\n\n\n<p>There are different ways to get readings from the sensor. In this tutorial, we&#8217;ll use the Adafruit MPU6050 library. To use this library you also need to install the Adafruit Unified Sensor library and the Adafruit Bus IO Library.<\/p>\n\n\n\n<p>Open your Arduino IDE and go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p>Type \u201c<strong>adafruit mpu6050<\/strong>\u201d on the search box and install the library.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/Install-Adafruit-MPU6050-library-Arduino-IDE.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install Adafruit MPU6050 Gyroscope Accelerometer Sensor Library Arduino IDE\" class=\"wp-image-101015\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/Install-Adafruit-MPU6050-library-Arduino-IDE.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/Install-Adafruit-MPU6050-library-Arduino-IDE.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/Install-Adafruit-MPU6050-library-Arduino-IDE.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n<p>Then, search for \u201c<strong>Adafruit Unified Sensor<\/strong>\u201d. Scroll all the way down to find the library and install it.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install Adafruit Unified Library Arduino IDE\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Finally, search for \u201c<strong>Adafruit Bus IO<\/strong>\u201d and install it.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Install-Adafruit-Bus-IO-library-Arduino-IDE.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Install-Adafruit-Bus-IO-library-Arduino-IDE.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install Adafruit Bus IO Library Arduino IDE\" class=\"wp-image-101462\" style=\"width:786px;height:443px\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Install-Adafruit-Bus-IO-library-Arduino-IDE.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Install-Adafruit-Bus-IO-library-Arduino-IDE.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Install-Adafruit-Bus-IO-library-Arduino-IDE.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/figure><\/div>\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\n\n\n\n<p>If you&#8217;re using VS Code with PaltformIO, copy the following lines to the <em>platformio.ini<\/em> file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>lib_deps = adafruit\/Adafruit MPU6050 @ ^2.0.3\n    adafruit\/Adafruit Unified Sensor @ ^1.1.4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1\">Getting MPU-6050 Sensor Readings: Accelerometer, Gyroscope and Temperature<\/h2>\n\n\n\n<p>In this section you&#8217;ll learn how to get sensor readings from the MPU-6050 sensor: acceleration (x, y, z), angular velocity (x, y, z) and temperature.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>For this example you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mpu-6050-3-axis-accelerometer-and-gyroscope-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">MPU-6050 Accelerometer Gyroscope<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266<\/a>\u00a0(read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best ESP8266 development boards<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic Diagram &#8211; ESP8266 NodeMCU with MPU-6050<\/h3>\n\n\n\n<p>Wire the ESP8266 to the MPU-6050 sensor as shown in the following schematic diagram: connect the SCL pin to <span class=\"rnthl rntclblue\">GPIO 5<\/span> and the SDA pin to <span class=\"rnthl rntclgreen\">GPIO 4<\/span>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"591\" height=\"533\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?resize=591%2C533&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MPU6050 Accelerometer Gyroscope Wiring to ESP8266 NodeMCU Schematic Diagram Circuit\" class=\"wp-image-101372\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?w=591&amp;quality=100&amp;strip=all&amp;ssl=1 591w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?resize=300%2C271&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">Code &#8211; Getting MPU-6050 Sensor Readings: Accelerometer, Gyroscope and Temperature<\/h3>\n\n\n\n<p>The Adafruit library provides several examples for this sensor. In this section, we&#8217;ll take a look at the basic example that prints the sensor readings in the Serial Monitor.<\/p>\n\n\n\n<p>Go to <strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>Adafruit MPU6050<\/strong> &gt; <strong>basic_readings<\/strong>. The following code should load.<\/p>\n\n\n\n<p>It gets the angular velocity (gyroscope) on the x, y and z axis, the acceleration on the x, y and z axis and the temperature.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/\/ Basic demo for accelerometer readings from Adafruit MPU6050\n\n\/\/ ESP32 Guide: https:\/\/RandomNerdTutorials.com\/esp32-mpu-6050-accelerometer-gyroscope-arduino\/\n\/\/ ESP8266 Guide: https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino\/\n\/\/ Arduino Guide: https:\/\/RandomNerdTutorials.com\/arduino-mpu-6050-accelerometer-gyroscope\/\n\n#include &lt;Adafruit_MPU6050.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;Wire.h&gt;\n\nAdafruit_MPU6050 mpu;\n\nvoid setup(void) {\n  Serial.begin(115200);\n  while (!Serial)\n    delay(10); \/\/ will pause Zero, Leonardo, etc until serial console opens\n\n  Serial.println(&quot;Adafruit MPU6050 test!&quot;);\n\n  \/\/ Try to initialize!\n  if (!mpu.begin()) {\n    Serial.println(&quot;Failed to find MPU6050 chip&quot;);\n    while (1) {\n      delay(10);\n    }\n  }\n  Serial.println(&quot;MPU6050 Found!&quot;);\n\n  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);\n  Serial.print(&quot;Accelerometer range set to: &quot;);\n  switch (mpu.getAccelerometerRange()) {\n  case MPU6050_RANGE_2_G:\n    Serial.println(&quot;+-2G&quot;);\n    break;\n  case MPU6050_RANGE_4_G:\n    Serial.println(&quot;+-4G&quot;);\n    break;\n  case MPU6050_RANGE_8_G:\n    Serial.println(&quot;+-8G&quot;);\n    break;\n  case MPU6050_RANGE_16_G:\n    Serial.println(&quot;+-16G&quot;);\n    break;\n  }\n  mpu.setGyroRange(MPU6050_RANGE_500_DEG);\n  Serial.print(&quot;Gyro range set to: &quot;);\n  switch (mpu.getGyroRange()) {\n  case MPU6050_RANGE_250_DEG:\n    Serial.println(&quot;+- 250 deg\/s&quot;);\n    break;\n  case MPU6050_RANGE_500_DEG:\n    Serial.println(&quot;+- 500 deg\/s&quot;);\n    break;\n  case MPU6050_RANGE_1000_DEG:\n    Serial.println(&quot;+- 1000 deg\/s&quot;);\n    break;\n  case MPU6050_RANGE_2000_DEG:\n    Serial.println(&quot;+- 2000 deg\/s&quot;);\n    break;\n  }\n\n  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);\n  Serial.print(&quot;Filter bandwidth set to: &quot;);\n  switch (mpu.getFilterBandwidth()) {\n  case MPU6050_BAND_260_HZ:\n    Serial.println(&quot;260 Hz&quot;);\n    break;\n  case MPU6050_BAND_184_HZ:\n    Serial.println(&quot;184 Hz&quot;);\n    break;\n  case MPU6050_BAND_94_HZ:\n    Serial.println(&quot;94 Hz&quot;);\n    break;\n  case MPU6050_BAND_44_HZ:\n    Serial.println(&quot;44 Hz&quot;);\n    break;\n  case MPU6050_BAND_21_HZ:\n    Serial.println(&quot;21 Hz&quot;);\n    break;\n  case MPU6050_BAND_10_HZ:\n    Serial.println(&quot;10 Hz&quot;);\n    break;\n  case MPU6050_BAND_5_HZ:\n    Serial.println(&quot;5 Hz&quot;);\n    break;\n  }\n\n  Serial.println(&quot;&quot;);\n  delay(100);\n}\n\nvoid loop() {\n  \/* Get new sensor events with the readings *\/\n  sensors_event_t a, g, temp;\n  mpu.getEvent(&amp;a, &amp;g, &amp;temp);\n\n  \/* Print out the values *\/\n  Serial.print(&quot;Acceleration X: &quot;);\n  Serial.print(a.acceleration.x);\n  Serial.print(&quot;, Y: &quot;);\n  Serial.print(a.acceleration.y);\n  Serial.print(&quot;, Z: &quot;);\n  Serial.print(a.acceleration.z);\n  Serial.println(&quot; m\/s^2&quot;);\n\n  Serial.print(&quot;Rotation X: &quot;);\n  Serial.print(g.gyro.x);\n  Serial.print(&quot;, Y: &quot;);\n  Serial.print(g.gyro.y);\n  Serial.print(&quot;, Z: &quot;);\n  Serial.print(g.gyro.z);\n  Serial.println(&quot; rad\/s&quot;);\n\n  Serial.print(&quot;Temperature: &quot;);\n  Serial.print(temp.temperature);\n  Serial.println(&quot; degC&quot;);\n\n  Serial.println(&quot;&quot;);\n  delay(500);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_MPU6050_Basic_Demo.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Start by including the required libraries for the MPU-6050 sensor: <span class=\"rnthl rntliteral\">Adafruit_MPU6050<\/span> and <span class=\"rnthl rntliteral\">Adafruit_Sensor<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Adafruit_MPU6050.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">Adafruit_MPU6050<\/span> object called <span class=\"rnthl rntliteral\">mpu<\/span> to handle the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_MPU6050 mpu;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the serial monitor at a baud rate of 115200.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the MPU-6050 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!mpu.begin()) {\n  Serial.println(\"Sensor init failed\");\n  while (1)\n    yield();\n}<\/code><\/pre>\n\n\n\n<p>Set the accelerometer measurement range:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mpu.setAccelerometerRange(MPU6050_RANGE_8_G);<\/code><\/pre>\n\n\n\n<p>Set the gyroscope measurement range:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mpu.setGyroRange(MPU6050_RANGE_500_DEG);<\/code><\/pre>\n\n\n\n<p>Set the filter bandwidth:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span> we&#8217;ll get sensor readings and display them in the Serial Monitor.<\/p>\n\n\n\n<p>First, you need to get new sensor events with the current readings.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors_event_t a, g, temp;\nmpu.getEvent(&amp;a, &amp;g, &amp;temp);<\/code><\/pre>\n\n\n\n<p>Finally, print the readings. For the acceleration:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">a.acceleration.x<\/span>: gets acceleration on the x axis;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">a.acceleration.y<\/span>: gets acceleration on the y axis;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">a.acceleration.z<\/span>: gets acceleration on the z axis.<\/li>\n<\/ul>\n\n\n\n<p>The acceleration is measured in meters per second square (m\/s<sup>2<\/sup>)<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Acceleration X: \");\nSerial.print(a.acceleration.x);\nSerial.print(\", Y: \");\nSerial.print(a.acceleration.y);\nSerial.print(\", Z: \");\nSerial.print(a.acceleration.z);\nSerial.println(\" m\/s^2\");<\/code><\/pre>\n\n\n\n<p>To get gyroscope readings:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">g.gyro.x<\/span>: gets angular velocity on the x axis;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">g.gyro.y<\/span>: gets angular velocity on the y axis;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">g.gyro.z<\/span>: gets angular velocity on the z axis.<\/li>\n<\/ul>\n\n\n\n<p>The angular velocity is measured in radians per seconds (rad\/s).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Rotation X: \");\nSerial.print(g.gyro.x);\nSerial.print(\", Y: \");\nSerial.print(g.gyro.y);\nSerial.print(\", Z: \");\nSerial.print(g.gyro.z);\nSerial.println(\" rad\/s\");<\/code><\/pre>\n\n\n\n<p>Finally, print the temperature &#8211; it is measured in Celsius degrees. To access the temperature reading use <span class=\"rnthl rntliteral\">temp.temperature<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Temperature: \");\nSerial.print(temp.temperature);\nSerial.println(\" degC\");<\/code><\/pre>\n\n\n\n<p>New sensor readings are displayed every 500 milliseconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(500);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to your ESP8266 board. Go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>and select the ESP8266 board you\u2019re using. Go to <strong>Tools <\/strong>&gt; <strong>Port <\/strong>and select the port your board is connected to. Then, click the Upload button.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200, press the on-board RST button. The sensor measurements will be displayed.<\/p>\n\n\n\n<p>Move the sensor orientation and see the values changing accordingly.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"445\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Sensor-Readings-Serial-Monitor.png?resize=639%2C445&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MPU-6050 Sensor Readings Serial Monitor Arduino IDE\" class=\"wp-image-101021\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Sensor-Readings-Serial-Monitor.png?w=639&amp;quality=100&amp;strip=all&amp;ssl=1 639w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Sensor-Readings-Serial-Monitor.png?resize=300%2C209&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\">Sensor Calibration<\/h4>\n\n\n\n<p>Ideally, when the sensor is static, the gyroscope values should be zero on all axis, which doesn&#8217;t happen in our case. When the sensor is static, these are the gyroscope values we get:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>x: 0.06 rad\/s<\/li>\n\n\n\n<li>y: -0.02 rad\/s<\/li>\n\n\n\n<li>z: 0.00 rad\/s<\/li>\n<\/ul>\n\n\n\n<p>On practical applications, you need to take the error into account and correct the values in the code to get more accurate readings.<\/p>\n\n\n\n<p>The same happens for the acceleration values. The acceleration on the z axis should be closer to the gravitational force (9,8 m\/s<sup>2<\/sup>) and it should be closer to zero on the x and y axis. In our case, these are the approximate values we get when the sensor is static:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>x: 0.71 m\/s<sup>2<\/sup><\/li>\n\n\n\n<li>y: 0.28 m\/s<sup>2<\/sup><\/li>\n\n\n\n<li>z: 9.43 m\/s<sup>2<\/sup><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2\">Display MPU-6050 Readings on OLED Display<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino-Demonstration.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino-Demonstration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU MPU6050 Module Accelerometer Gyroscope Temperature Sensor Arduino Demonstration\" class=\"wp-image-101200\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino-Demonstration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino-Demonstration.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n<p>The Adafruit MPU6050 library provides an example that dipslays the MPU-6050 gyroscope and accelerometer readings on an OLED display.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>Here are the parts required to complete this example:<\/p>\n\n\n\n<ul id=\"block-20d88f2a-6847-4e0b-a050-3bc5f026cbe4\" class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mpu-6050-3-axis-accelerometer-and-gyroscope-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\">MPU-6050 Accelerometer Gyroscope<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266<\/a>\u00a0(read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best ESP8266 development boards<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">0.96 inch I2C OLED Display SSD1306<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li>\n<\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic Diagram &#8211; ESP8266 NodeMCU with MPU-6050 and OLED Display<\/h3>\n\n\n\n<p>Wire all the parts as shown in the following schematic diagram. Because the OLED display and the MPU-6050 sensors use different I2C addresses, we can connect them to the same I2C bus (same pins on the ESP8266).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-OLED-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"841\" height=\"877\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-OLED-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?resize=841%2C877&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU with MPU6050 and OLED Display Wiring Schematic Diagram Circuit\" class=\"wp-image-101373\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-OLED-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?w=841&amp;quality=100&amp;strip=all&amp;ssl=1 841w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-OLED-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?resize=288%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 288w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/MPU6050-OLED-ESP8266-NodeMCU-Wiring-Schematic-Diagram-Circuit.png?resize=768%2C801&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 841px) 100vw, 841px\" \/><\/a><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\">Learn more about using the OLED display with the ESP8266: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-0-96-inch-oled-display-with-arduino-ide\/\">ESP8266 NodeMCU OLED Display with Arduino IDE<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code &#8211; Display MPU-6050 Sensor Readings on OLED Display<\/h3>\n\n\n\n<p>To use this example, make sure you have the Adafruit SSD1306 library installed. This library can be installed through the Arduino Library Manager.<\/p>\n\n\n\n<p>Go to <strong>Sketch <\/strong>&gt; <strong>Library<\/strong> &gt; <strong>Manage Libraries<\/strong> and search for &#8220;<strong>SSD1306<\/strong>&#8221; and install the SSD1306 library from Adafruit.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"788\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=788%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install Library I2C OLED Display SSD1306 Arduino IDE\" class=\"wp-image-85156\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?w=788&amp;quality=100&amp;strip=all&amp;ssl=1 788w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/figure><\/div>\n\n\n<p>For this example, copy the following code or go to <strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>Adafruit MPU6050 <\/strong>&gt; <strong>MPU6050_oled<\/strong>.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/\/ Basic OLED demo for accelerometer readings from Adafruit MPU6050\n\n\/\/ ESP32 Guide: https:\/\/RandomNerdTutorials.com\/esp32-mpu-6050-accelerometer-gyroscope-arduino\/\n\/\/ ESP8266 Guide: https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino\/\n\/\/ Arduino Guide: https:\/\/RandomNerdTutorials.com\/arduino-mpu-6050-accelerometer-gyroscope\/\n\n#include &lt;Adafruit_MPU6050.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n\nAdafruit_MPU6050 mpu;\nAdafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &amp;Wire);\n\nvoid setup() {\n  Serial.begin(115200);\n  \/\/ while (!Serial);\n  Serial.println(&quot;MPU6050 OLED demo&quot;);\n\n  if (!mpu.begin()) {\n    Serial.println(&quot;Sensor init failed&quot;);\n    while (1)\n      yield();\n  }\n  Serial.println(&quot;Found a MPU-6050 sensor&quot;);\n\n  \/\/ SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally\n  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { \/\/ Address 0x3C for 128x64\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\n    for (;;)\n      ; \/\/ Don't proceed, loop forever\n  }\n  display.display();\n  delay(500); \/\/ Pause for 2 seconds\n  display.setTextSize(1);\n  display.setTextColor(WHITE);\n  display.setRotation(0);\n}\n\nvoid loop() {\n  sensors_event_t a, g, temp;\n  mpu.getEvent(&amp;a, &amp;g, &amp;temp);\n\n  display.clearDisplay();\n  display.setCursor(0, 0);\n\n  Serial.print(&quot;Accelerometer &quot;);\n  Serial.print(&quot;X: &quot;);\n  Serial.print(a.acceleration.x, 1);\n  Serial.print(&quot; m\/s^2, &quot;);\n  Serial.print(&quot;Y: &quot;);\n  Serial.print(a.acceleration.y, 1);\n  Serial.print(&quot; m\/s^2, &quot;);\n  Serial.print(&quot;Z: &quot;);\n  Serial.print(a.acceleration.z, 1);\n  Serial.println(&quot; m\/s^2&quot;);\n\n  display.println(&quot;Accelerometer - m\/s^2&quot;);\n  display.print(a.acceleration.x, 1);\n  display.print(&quot;, &quot;);\n  display.print(a.acceleration.y, 1);\n  display.print(&quot;, &quot;);\n  display.print(a.acceleration.z, 1);\n  display.println(&quot;&quot;);\n\n  Serial.print(&quot;Gyroscope &quot;);\n  Serial.print(&quot;X: &quot;);\n  Serial.print(g.gyro.x, 1);\n  Serial.print(&quot; rps, &quot;);\n  Serial.print(&quot;Y: &quot;);\n  Serial.print(g.gyro.y, 1);\n  Serial.print(&quot; rps, &quot;);\n  Serial.print(&quot;Z: &quot;);\n  Serial.print(g.gyro.z, 1);\n  Serial.println(&quot; rps&quot;);\n\n  display.println(&quot;Gyroscope - rps&quot;);\n  display.print(g.gyro.x, 1);\n  display.print(&quot;, &quot;);\n  display.print(g.gyro.y, 1);\n  display.print(&quot;, &quot;);\n  display.print(g.gyro.z, 1);\n  display.println(&quot;&quot;);\n\n  display.display();\n  delay(100);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP\/ESP_MPU6050_Basic_OLED.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Start by including the required libraries for the MPU-6050 sensor and for the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Adafruit_MPU6050.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">Adafruit_MPU6050<\/span> object called <span class=\"rnthl rntliteral\">mpu<\/span> to handle the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_MPU6050 mpu;<\/code><\/pre>\n\n\n\n<p>Create an <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> object called <span class=\"rnthl rntliteral\">display<\/span> to handle the OLED display. This is for a display with 128&#215;64 pixels.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &amp;Wire);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the serial monitor at a baud rate of 115200.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the MPU-6050 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (!mpu.begin()) {\n  Serial.println(\"Sensor init failed\");\n  while (1)\n    yield();\n}<\/code><\/pre>\n\n\n\n<p>Initialize the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally\nif (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { \/\/ Address 0x3C for 128x64\n  Serial.println(F(\"SSD1306 allocation failed\"));\n  for (;;)\n    ; \/\/ Don't proceed, loop forever\n}\ndisplay.display();<\/code><\/pre>\n\n\n\n<p>Set the font size and color for the display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setTextSize(1);\ndisplay.setTextColor(WHITE);\ndisplay.setRotation(0);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span> is where we&#8217;ll get the sensor readings and display them on the OLED.<\/p>\n\n\n\n<p>Start by creating events for each measurement, accelerometer, gyroscope and temperature.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors_event_t a, g, temp;<\/code><\/pre>\n\n\n\n<p>Get new sensor readings.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>mpu.getEvent(&amp;a, &amp;g, &amp;temp);<\/code><\/pre>\n\n\n\n<p>Clear the display in each <span class=\"rnthl rntliteral\">loop()<\/span> to write new readings.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.clearDisplay();<\/code><\/pre>\n\n\n\n<p>Set the display cursor to (0,0) &#8211; the upper left corner. It will start writing text from that location.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setCursor(0, 0);<\/code><\/pre>\n\n\n\n<p>The following lines print the accelerometer readings in the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Accelerometer \");\nSerial.print(\"X: \");\nSerial.print(a.acceleration.x, 1);\nSerial.print(\" m\/s^2, \");\nSerial.print(\"Y: \");\nSerial.print(a.acceleration.y, 1);\nSerial.print(\" m\/s^2, \");\nSerial.print(\"Z: \");\nSerial.print(a.acceleration.z, 1);\nSerial.println(\" m\/s^2\");<\/code><\/pre>\n\n\n\n<p>The following lines display the acceleration x, y an z values on the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.println(\"Accelerometer - m\/s^2\");\ndisplay.print(a.acceleration.x, 1);\ndisplay.print(\", \");\ndisplay.print(a.acceleration.y, 1);\ndisplay.print(\", \");\ndisplay.print(a.acceleration.z, 1);\ndisplay.println(\"\");<\/code><\/pre>\n\n\n\n<p>Display the gyroscope readings on the Serial Monitor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Gyroscope \");\nSerial.print(\"X: \");\nSerial.print(g.gyro.x, 1);\nSerial.print(\" rps, \");\nSerial.print(\"Y: \");\nSerial.print(g.gyro.y, 1);\nSerial.print(\" rps, \");\nSerial.print(\"Z: \");\nSerial.print(g.gyro.z, 1);\nSerial.println(\" rps\");<\/code><\/pre>\n\n\n\n<p>Finally, print the gyroscope readings on the display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.println(\"Gyroscope - rps\");\ndisplay.print(g.gyro.x, 1);\ndisplay.print(\", \");\ndisplay.print(g.gyro.y, 1);\ndisplay.print(\", \");\ndisplay.print(g.gyro.z, 1);\ndisplay.println(\"\");<\/code><\/pre>\n\n\n\n<p>Lastly, call <span class=\"rnthl rntliteral\">display.display()<\/span> to actually show the readings on the OLED.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.display();<\/code><\/pre>\n\n\n\n<p>New readings are displayed every 100 milliseconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(100);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to your ESP8266 board. Go to <strong>Tools <\/strong>&gt; <strong>Board <\/strong>and select the ESP8266 board you\u2019re using. Go to <strong>Tools <\/strong>&gt; <strong>Port <\/strong>and select the port your board is connected to. Then, click the upload button.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200, press the on-board RST button. The sensor measurements will be displayed both on the Serial Monitor and on the OLED display.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-OLED-Demonstration-ESP32-ESP8266.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MPU-6050 Module Accelerometer Gyroscope Temperature Sensor OLED Demonstration ESP8266 NodeMCU\" class=\"wp-image-101202\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-OLED-Demonstration-ESP32-ESP8266.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-OLED-Demonstration-ESP32-ESP8266.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Move the sensor and see the values changing.<\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"639\" height=\"445\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Sensor-Readings-OLED-Example-Serial-Monitor.png?resize=639%2C445&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MPU-6050 Sensor Readings OLED Example Serial Monitor\" class=\"wp-image-101023\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Sensor-Readings-OLED-Example-Serial-Monitor.png?w=639&amp;quality=100&amp;strip=all&amp;ssl=1 639w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/MPU6050-Sensor-Readings-OLED-Example-Serial-Monitor.png?resize=300%2C209&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 639px) 100vw, 639px\" \/><\/figure><\/div>\n\n\n<p>You can watch the video demonstration:<\/p>\n\n\n<div style=\"text-align:center\"><iframe src=\"https:\/\/player.vimeo.com\/video\/501858826?color=ff9933&title=1&byline=0&portrait=0\" width=\"720\" height=\"405\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen><\/iframe><\/div><\/br>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>The MPU-6050 is an accelerometer and gyroscope. It measures acceleration on the x, y and z axis as well as angular velocity. This module also measures temperature.<\/p>\n\n\n\n<p>This sensor modules communicates via I2C communication protocol. So, the wiring is very simple. Just connect the sensor to the ESP8266 default I2C pins.<\/p>\n\n\n\n<p>In this tutorial you&#8217;ve learned how to wire the sensor and get sensor readings. We hope you\u2019ve found this getting started guide useful. We have guides for other popular sensors:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/\">ESP8266 with\u00a0<strong>DHT11\/DHT22 Temperature and Humidity Sensor<\/strong>\u00a0using Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-bme280-arduino-ide\/\">ESP8266 with\u00a0<strong>BME280<\/strong>\u00a0using Arduino IDE (<strong>Pressure, Temperature, Humidity<\/strong>)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide\/\">ESP8266 <strong>DS18B20 Temperature Sensor<\/strong>\u00a0with Arduino IDE (Single, Multiple, Web Server)<\/a><\/li>\n\n\n\n<li><span style=\"font-size: inherit; background-color: initial;\"><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-bme680-sensor-arduino\/\">ESP8266 with <\/a><\/span><strong style=\"font-size: inherit; background-color: initial;\"><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-bme680-sensor-arduino\/\">BME680 Environmental Sensor<\/a><\/strong><span style=\"font-size: inherit; background-color: initial;\"><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-bme680-sensor-arduino\/\"> (Gas, Pressure, Humidity, Temperature)<\/a><\/span><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266 (eBook)<\/a><\/strong><\/li>\n\n\n\n<li><a style=\"font-size: inherit; background-color: initial;\" href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 NodeMCU Projects and Tutorials\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide you&#8217;ll learn how to use the MPU-6050 accelerometer and gyroscope module with the ESP8266 NodeMCU. The MPU-6050 IMU (Inertial Measurement Unit) is a 3-axis accelerometer and 3-axis &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU with MPU-6050 Accelerometer, Gyroscope and Temperature Sensor (Arduino)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino\/#more-101364\" aria-label=\"Read more about ESP8266 NodeMCU with MPU-6050 Accelerometer, Gyroscope and Temperature Sensor (Arduino)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":101199,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,265,246,300,240,264,1],"tags":[],"class_list":["post-101364","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-project","category-uncategorized"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/ESP8266-NodeMCU-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Arduino.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/101364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=101364"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/101364\/revisions"}],"predecessor-version":[{"id":168061,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/101364\/revisions\/168061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/101199"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=101364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=101364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=101364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}