Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

šŸ‘‰ Ready to explore and create with us? Click [here] and join today!

Lesson 05: Gyroscope & Accelerometer Module (MPU6050)

In this lesson, you will learn how to use the MPU6050 sensor with an Arduino to measure acceleration, rotation, and temperature. We’ll explore initializing the sensor, setting its ranges, and reading data to display on the serial monitor. This project offers a hands-on approach to working with motion sensors and integrating them with Arduino, perfect for those looking to dive into the world of electronics and sensor data handling.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

Gyroscope & Accelerometer Module (MPU6050)

BUY

Wiring

../_images/Lesson_05_mpu6050_circuit_uno_bb.png

Code

Note

To install the library, use the Arduino Library Manager and search for ā€œAdafruit MPU6050ā€ and install it.

Code Analysis

  1. The code starts by including the necessary libraries and creating an object for the MPU6050 sensor. This code uses the Adafruit_MPU6050 library, Adafruit_Sensor library, and Wire library. The Adafruit_MPU6050 library is used to interact with the MPU6050 sensor and retrieve acceleration, rotation, and temperature data. The Adafruit_Sensor library provides a common interface for various types of sensors. The Wire library is used for I2C communication, which is necessary to communicate with the MPU6050 sensor.

    Note

    To install the library, use the Arduino Library Manager and search for ā€œAdafruit MPU6050ā€ and install it.

    #include <Adafruit_MPU6050.h>
    #include <Adafruit_Sensor.h>
    #include <Wire.h>
    Adafruit_MPU6050 mpu;
    
  2. The setup() function initializes the serial communication and checks if the sensor is detected. If the sensor is not found, the Arduino enters an infinite loop with a ā€œFailed to find MPU6050 chipā€ message. If found, the accelerometer range, gyro range, and filter bandwidth are set, and a delay is added for stability.

    void setup(void) {
      // Initialize the serial communication
      Serial.begin(9600);
    
      // Check if the MPU6050 sensor is detected
      if (!mpu.begin()) {
        Serial.println("Failed to find MPU6050 chip");
        while (1) {
          delay(10);
        }
      }
      Serial.println("MPU6050 Found!");
    
      // set accelerometer range to +-8G
      mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
    
      // set gyro range to +- 500 deg/s
      mpu.setGyroRange(MPU6050_RANGE_500_DEG);
    
      // set filter bandwidth to 21 Hz
      mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
    
      // Add a delay for stability
      delay(100);
    }
    
  3. In the loop() function, the program creates events to store the sensor readings and then retrieves the readings. The acceleration, rotation, and temperature values are then printed to the serial monitor.

    void loop() {
      // Get new sensor events with the readings
      sensors_event_t a, g, temp;
      mpu.getEvent(&a, &g, &temp);
    
      // Print out the acceleration, rotation, and temperature readings
      // ...
    
      // Add a delay to avoid flooding the serial monitor
      delay(1000);
    }