Communicate easily with MPU6050 and get readings from it.

The MPU6050 is a sensor that measures both acceleration and angular velocity. It consists of two main parts: an accelerometer and a gyroscope. The accelerometer is used to measure linear acceleration, while the gyroscope is used to measure angular velocity.

Overview


The MPU6050 sensor is a 6-axis device that can measure acceleration in 3-dimensional space and angular velocity about 3 axes. It has a wide range of applications in drones, robots, cell phones, video game controllers, and many other devices.


In this tutorial we will communicate with the sensor via I2C protocol and get readings from it using a simple library.


Getting the items


For this project you will need the following components: 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR

Wiring it up


To obtain the correct readings from the sensor, follow the instructions below as well as the image that demonstrates how to connect the sensor to the Arduino.



Connections from the Arduino to the MPU6050:

• Arduino VCC pin → MPU6050 VCC pin (+ pin)

• Arduino GND pin → MPU6050 GND pin (- pin)

• Arduino pin 5 → MPU6050 SCL pin

• Arduino pin 4 → MPU6050 SDA pin


Coding


The purpose of this sketch is to get readings from the MPU6050 sensor and show them through the Arduino communication with the computer via the serial monitor.


You will need to include wire.h library for this sketch to work properly it allows the communication between the Arduino and the sensor through the I2C protocol.


We will also use a basic and easy library for the MPU6050 sensor, which will make the code extremely simple and straightforward to use; this code may be used to control more complex and advanced projects.


Libraries are files that may be included with the Arduino IDE that allow you to use various sensors and components in a simple and feature-rich manner without having to dig into the underlying structure of the components.


Let's put the code to the test together; you'll be able to comprehend it quickly by following the comments and instructions throughout the code.


  1. /*
  2.   Voltaat learn (https://www.voltaat.com)
  3.   Link for full tutorial: https://www.voltaat.com/blogs/tutorials/communicate-easily-with-mpu6050-and-get-readings-from-it
  4.   Link for libraries:
  5.   Wire library: https://cdn.shopify.com/s/files/1/2822/2674/files/AceWire-develop.zip?v=1661086928
  6.   MPU6050 library: https://cdn.shopify.com/s/files/1/2822/2674/files/MPU6050_tockn-master.zip?v=1662629499
  7.  
  8.  
  9.  
  10.   Connections from the Arduino to the MPU6050:
  11.   • Arduino VCC pin → MPU6050 VCC pin (+ pin)
  12.   • Arduino GND pin → MPU6050 GND pin (- pin)
  13.   • Arduino pin 5 → MPU6050 SCL pin
  14.   • Arduino pin 4 → MPU6050 SDA pin
  15.  
  16. */
  17.  
  18. //Include MPU6050 Library
  19. #include MPU6050_tockn.h
  20. //This library allows you to connect with I2C/TWI devices
  21. #include Wire.h
  22.  
  23. MPU6050 mpu6050(Wire);
  24.  
  25. //Define variable
  26. long timer =0;
  27.  
  28. //Commands inside void setup run once
  29. void setup(){
  30. //Start the serial monitor at 19200 baud rate (19200 bits per second)
  31. Serial.begin(9600);
  32. //This function initializes the Wire library and join the I2C bus as a controller or a peripheral
  33. Wire.begin();
  34. //This function initializes the MPU6050 library
  35. mpu6050.begin();
  36. //See state of calculating calibration in serial monitor
  37. mpu6050.calcGyroOffsets(true);
  38. }
  39.  
  40. //Commands inside void loop run forever
  41. void loop(){
  42. //Get all data of MPU6050
  43. mpu6050.update();
  44.  
  45. if(millis()- timer >1000){
  46. //Print data to serial monitor
  47. Serial.println("=======================================================");
  48. Serial.print("temp : "); Serial.println(mpu6050.getTemp());
  49. Serial.print("accX : "); Serial.print(mpu6050.getAccX());
  50. Serial.print("\taccY : "); Serial.print(mpu6050.getAccY());
  51. Serial.print("\taccZ : "); Serial.println(mpu6050.getAccZ());
  52.  
  53. Serial.print("gyroX : "); Serial.print(mpu6050.getGyroX());
  54. Serial.print("\tgyroY : "); Serial.print(mpu6050.getGyroY());
  55. Serial.print("\tgyroZ : "); Serial.println(mpu6050.getGyroZ());
  56.  
  57. Serial.print("accAngleX : "); Serial.print(mpu6050.getAccAngleX());
  58. Serial.print("\taccAngleY : "); Serial.println(mpu6050.getAccAngleY());
  59.  
  60. Serial.print("gyroAngleX : "); Serial.print(mpu6050.getGyroAngleX());
  61. Serial.print("\tgyroAngleY : "); Serial.print(mpu6050.getGyroAngleY());
  62. Serial.print("\tgyroAngleZ : "); Serial.println(mpu6050.getGyroAngleZ());
  63.  
  64. Serial.print("angleX : "); Serial.print(mpu6050.getAngleX());
  65. Serial.print("\tangleY : "); Serial.print(mpu6050.getAngleY());
  66. Serial.print("\tangleZ : "); Serial.println(mpu6050.getAngleZ());
  67. Serial.println("=======================================================\n");
  68. timer = millis();
  69.  
  70. }
  71.  
  72. }

Testing it out


You may now access the serial monitor on your Arduino IDE by clicking on it.


The serial monitor is great way that’s enables you to make a communication between your computer and Arduino by sending and receiving different commands and information using the USB cable.


Now as we see in the following image the serial monitor displays the reading values from the MPU6050 sensor.

you should also make sure you have chosen the right baud rate (9600) as specified in the code.


Resources 


Arduino Code

wire Library
MPU6050 Library

Fritzing Wiring file

Related Tutorials


How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

Liquid error: Could not find asset snippets/zend_pre_order_theme_snippet.liquid
Liquid error: Could not find asset snippets/uploadkit.liquid