{"id":101585,"date":"2021-02-16T15:26:58","date_gmt":"2021-02-16T15:26:58","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=101585"},"modified":"2021-02-16T15:27:00","modified_gmt":"2021-02-16T15:27:00","slug":"arduino-mpu-6050-accelerometer-gyroscope","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/arduino-mpu-6050-accelerometer-gyroscope\/","title":{"rendered":"Arduino Guide for MPU-6050 Accelerometer and Gyroscope Sensor"},"content":{"rendered":"\n<p>Learn how to use the MPU-6050 accelerometer and gyroscope module with the Arduino board. 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 for determining the orientation of a moving object.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino Board MPU6050 Module Accelerometer Gyroscope Temperature Sensor\" class=\"wp-image-101568\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>We have a similar guide for the ESP32 and ESP8266 boards:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-mpu-6050-accelerometer-gyroscope-arduino\/\">ESP32 with MPU-6050 Accelerometer, Gyroscope and Temperature Sensor (Arduino)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino\/\">ESP8266 NodeMCU with MPU-6050 Accelerometer, Gyroscope and Temperature Sensor (Arduino)<\/a><\/li><\/ul>\n\n\n\n<p>In this guide we&#8217;ll cover two examples:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"#1\">Get gyroscope, acceleration and temperature readings (Serial Monitor)<\/a><\/li><li><a href=\"#3\">Gyroscope and accelerometer readings on Serial Plotter<\/a><\/li><li><a href=\"#2\">Display gyroscope and acceleration readings on OLED display<\/a><\/li><\/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\n<div class=\"wp-block-image is-style-default\"><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\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\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/12\/roll-pitch-yaw.png?resize=416%2C377&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Roll Pitch Yaw Angles\" class=\"wp-image-101026\" width=\"416\" height=\"377\" 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: 416px) 100vw, 416px\" \/><\/figure><\/div>\n\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, and 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 accelerometer&#8217;s values, 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 (A5)<\/td><\/tr><tr><td><span class=\"rnthl rntclgreen\">SDA<\/span><\/td><td>SDA pin for I2C communication (A4)<\/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\">Installing Libraries<\/h2>\n\n\n\n<p>There are various 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\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" 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\" width=\"786\" height=\"443\" 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\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\n<div class=\"wp-block-image is-style-default\"><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\n<p>Finally, search for \u201c<strong>Adafruit Bus IO<\/strong>\u201d and install it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" 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\" width=\"786\" height=\"443\" 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\" \/><\/figure><\/div>\n\n\n\n<p>After installing the libraries, restart your Arduino IDE.<\/p>\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\"><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><li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino <\/a>(read <a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best Arduino Starter Kits<\/a>)<\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><\/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; Arduino with MPU-6050<\/h3>\n\n\n\n<p>Wire the Arduino to the MPU-6050 sensor as shown in the following schematic diagram: connect the SCL pin to <span class=\"rnthl rntcyellow\">A5<\/span> and the SDA pin to <span class=\"rnthl rntclgray\">A4<\/span>.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050_Fritzing_Wiring_Diagram.png?resize=596%2C712&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino MPU-6050 Acclerometer Gyroscope Wiring Diagram Fritzing\" class=\"wp-image-101586\" width=\"596\" height=\"712\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050_Fritzing_Wiring_Diagram.png?w=795&amp;quality=100&amp;strip=all&amp;ssl=1 795w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050_Fritzing_Wiring_Diagram.png?resize=251%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 251w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050_Fritzing_Wiring_Diagram.png?resize=768%2C917&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/figure><\/div>\n\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 look at a basic example that prints the Serial Monitor&#8217;s sensor readings.<\/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\"><li><span class=\"rnthl rntliteral\">a.acceleration.x<\/span>: gets acceleration on the x axis;<\/li><li><span class=\"rnthl rntliteral\">a.acceleration.y<\/span>: gets acceleration on the y axis;<\/li><li><span class=\"rnthl rntliteral\">a.acceleration.z<\/span>: gets acceleration on the z axis.<\/li><\/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\"><li><span class=\"rnthl rntliteral\">g.gyro.x<\/span>: gets angular velocity on the x axis;<\/li><li><span class=\"rnthl rntliteral\">g.gyro.y<\/span>: gets angular velocity on the y axis;<\/li><li><span class=\"rnthl rntliteral\">g.gyro.z<\/span>: gets angular velocity on the z axis.<\/li><\/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 the Arduino board. <\/p>\n\n\n\n<p>Then, 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\n<div class=\"wp-block-image is-style-default\"><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\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\"><li>x: 0.06 rad\/s<\/li><li>y: -0.02 rad\/s<\/li><li>z: 0.00 rad\/s<\/li><\/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 Z-axis acceleration should be closer to the gravitational force (9,8 m\/s2), 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\"><li>x: 0.71 m\/s<sup>2<\/sup><\/li><li>y: 0.28 m\/s<sup>2<\/sup><\/li><li>z: 9.43 m\/s<sup>2<\/sup><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3\">Display MPU-6050 Readings on Serial Plotter<\/h2>\n\n\n\n<p>There is a similar example, but with slight differences to display the readings on the Serial Plotter.<\/p>\n\n\n\n<p>Upload the following code to your board:<\/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\/\/ 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\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\n  mpu.setAccelerometerRange(MPU6050_RANGE_16_G);\n  mpu.setGyroRange(MPU6050_RANGE_250_DEG);\n  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);\n  Serial.println(&quot;&quot;);\n  delay(100);\n}\n\nvoid loop() {\n\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(a.acceleration.x);\n  Serial.print(&quot;,&quot;);\n  Serial.print(a.acceleration.y);\n  Serial.print(&quot;,&quot;);\n  Serial.print(a.acceleration.z);\n  Serial.print(&quot;, &quot;);\n  Serial.print(g.gyro.x);\n  Serial.print(&quot;,&quot;);\n  Serial.print(g.gyro.y);\n  Serial.print(&quot;,&quot;);\n  Serial.print(g.gyro.z);\n  Serial.println(&quot;&quot;);\n\n  delay(10);\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\/Arduino\/Arduino_MPU6050_Serial_Plotter.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>After uploading the code, in the Arduino IDE go to <strong>Tools <\/strong>&gt; <strong>Serial Plotter<\/strong>.<\/p>\n\n\n\n<p>The Serial Plotter displays the accelerometer and the gyroscope readings on the X, Y, and Z-axis over time. Move the sensor in different ways and see how the values change.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"798\" height=\"519\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-Serial-Plotter-Example.png?resize=798%2C519&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino MPU-6050 Accelerometer and Gyroscope Serial Plotter Example\" class=\"wp-image-101588\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-Serial-Plotter-Example.png?w=798&amp;quality=100&amp;strip=all&amp;ssl=1 798w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-Serial-Plotter-Example.png?resize=300%2C195&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-Serial-Plotter-Example.png?resize=768%2C499&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 798px) 100vw, 798px\" \/><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator 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\n<div class=\"wp-block-image is-style-default\"><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\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Module-Parts.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino Board MPU6050 Module Accelerometer Gyroscope Temperature Sensor Module Parts Required\" class=\"wp-image-101570\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Module-Parts.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Module-Parts.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\n<p>The Adafruit MPU6050 library provides an example that displays 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&#8217;s the parts required to complete this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-20d88f2a-6847-4e0b-a050-3bc5f026cbe4\"><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><li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino <\/a>(read <a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best Arduino Starter Kits<\/a>)<\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">0.96 inch I2C OLED Display SSD1306<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><\/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; Arduino 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 Arduino board).<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-OLED_Fritzing_Wiring_Diagram.jpg?resize=600%2C605&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino MPU-6050 Gyroscope Accelerometer Sensor with OLED Display Fritzing Wiring Diagram\" class=\"wp-image-101587\" width=\"600\" height=\"605\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-OLED_Fritzing_Wiring_Diagram.jpg?w=800&amp;quality=100&amp;strip=all&amp;ssl=1 800w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-OLED_Fritzing_Wiring_Diagram.jpg?resize=298%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 298w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-OLED_Fritzing_Wiring_Diagram.jpg?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-MPU-6050-OLED_Fritzing_Wiring_Diagram.jpg?resize=768%2C774&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about using the OLED display with the Arduino: <a href=\"https:\/\/randomnerdtutorials.com\/guide-for-oled-display-with-arduino\/\">Guide for I2C OLED Display with Arduino<\/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\n<div class=\"wp-block-image is-style-default\"><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\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 Arduino board.<\/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 the OLED display.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Demosntration.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino Board MPU6050 Module Accelerometer Gyroscope Temperature Sensor Demonstration\" class=\"wp-image-101569\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Demosntration.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor-Demosntration.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\n<p>Move the sensor and see the values changing.<\/p>\n\n\n\n<div class=\"wp-block-image is-style-default\"><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\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 module communicates via the I2C communication protocol. So, the wiring is straightforward. Just connect the sensor to the Arduino 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\"><li><a href=\"https:\/\/randomnerdtutorials.com\/bme680-sensor-arduino-gas-temperature-humidity-pressure\/\"><strong>BME680 <\/strong>Environmental Sensor with Arduino (Gas, Temperature, Humidity, Pressure)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/bme280-sensor-arduino-pressure-temperature-humidity\/\"><strong>BME280&nbsp;<\/strong>Sensor with Arduino (Pressure, Temperature, Humidity)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-ds18b20-temperature-sensor-with-arduino\/\"><strong>DS18B20&nbsp;<\/strong>Temperature Sensor with Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/\"><strong>DHT11\/DHT22<\/strong>&nbsp;Humidity and Temperature Sensor With Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-lm35-lm335-lm34-temperature-sensor\/\"><strong>LM35<\/strong>,&nbsp;<strong>LM335<\/strong>&nbsp;and&nbsp;<strong>LM34&nbsp;<\/strong>Temperature Sensors with Arduino<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/dht11-vs-dht22-vs-lm35-vs-ds18b20-vs-bme280-vs-bmp180\/\">DHT11 vs DHT22 vs LM35 vs DS18B20 vs BME280 vs BMP180<\/a><\/li><\/ul>\n\n\n\n<p>Learn more about the Arduino with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\">Arduino Step-by-step Projects (eBook)<\/a><\/strong><\/li><li><strong><a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\">More Arduino Projects and Tutorials\u2026<\/a><\/strong><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to use the MPU-6050 accelerometer and gyroscope module with the Arduino board. The MPU-6050 IMU (Inertial Measurement Unit) is a 3-axis accelerometer and 3-axis gyroscope sensor. The accelerometer &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Arduino Guide for MPU-6050 Accelerometer and Gyroscope Sensor\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/arduino-mpu-6050-accelerometer-gyroscope\/#more-101585\" aria-label=\"Read more about Arduino Guide for MPU-6050 Accelerometer and Gyroscope Sensor\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":101568,"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":[303,2,267,302,245,264],"tags":[],"class_list":["post-101585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino","category-arduino-project","category-0-guide","category-arduino-ide","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/01\/Arduino-Board-MPU6050-Module-Accelerometer-Gyroscope-Temperature-Sensor.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\/101585","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=101585"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/101585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/101568"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=101585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=101585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=101585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}