Skip to content

Arduino Nano Every MPU6050: Read I2C Sensor Values | ShillehTek

October 23, 2023

Video Tutorial

Watch first if you want to see the Arduino Nano Every wired to the MPU6050 and the sensor readings displayed in the Serial Monitor.

Project Overview

In this tutorial, you will connect an Arduino Nano Every to an MPU6050 6DOF accelerometer and gyroscope and read acceleration and gyro values in the Arduino IDE Serial Monitor using I2C.

The Nano Every is Arduino's smallest board with dimensions of only 45x18mm and a weight under 5g. It is based on the ATMega4809 AVR processor and has 20 MHz clock speed, 48 KB of flash memory, 6 KB of SRAM, and 256 bytes of EEPROM. It also has two 15-pin connectors (one on each side) that are pin-to-pin compatible with the original Arduino Nano.

The Nano Every is a great choice for projects that require a small and powerful microcontroller board. It is also a good option for beginners who want to learn about Arduino programming, as it is very similar to the Arduino Uno.

  • Time: 15 to 30 minutes
  • Skill level: Beginner
  • What you will build: A working I2C MPU6050 setup on the Nano Every that streams accelerometer and gyroscope readings over Serial

Parts List

From ShillehTek

  • None listed in the original build

External

  • Arduino Nano Every
  • MPU6050 Pre Soldered
  • 4 jumper wires (for I2C + power)
  • USB cable (to power and program the board)
  • Arduino IDE (with Boards Manager and Library Manager)

Note: This tutorial assumes your MPU6050 module has soldered header pins so you can connect it with jumper wires.

Step-by-Step Guide

Step 1 - Connect the Nano Every to the MPU6050 and power it

Goal: Make the physical I2C and power connections so the Nano Every can communicate with the MPU6050.

What to do: Use 4 jumper wires to connect the MPU6050 to the Nano Every for I2C (SDA/SCL) plus power (VCC/GND). Then plug the Nano Every into your computer via USB to power it.

Arduino Nano Every wired to an MPU6050 accelerometer and gyroscope module using jumper wires
Example wiring setup: Nano Every connected to an MPU6050 module with jumper wires.

Expected result: The board powers on over USB and the MPU6050 is connected with 4 wires (power + I2C).

Step 2 - Install Arduino megaAVR Boards in the Arduino IDE

Goal: Make sure the Arduino IDE can compile and upload code to the Nano Every.

What to do: Open Arduino IDE and open the Boards Manager. Install Arduino megaAVR Boards so that you can connect to the Arduino Every. When finished, select the correct board and port.

Expected result: The Nano Every appears as a selectable board, and you can select its serial port.

Step 3 - Install the Adafruit MPU6050 library

Goal: Add the library needed to communicate with the MPU6050.

What to do: Open the Library Manager, search for MPU6050, and install the Adafruit MPU6050 library.

Expected result: The Adafruit MPU6050 library is installed and example sketches become available.

Step 4 - Open and upload the basic_readings example

Goal: Upload a known working example and view accelerometer and gyroscope readings.

What to do: In the Arduino IDE, go to File > Examples. Under the external library examples, select the basic_readings example from the Adafruit MPU6050 library. Upload it to the Nano Every.

Open the Serial Monitor and make sure the baud rate matches the sketch.

Code:

// Basic demo for accelerometer readings from Adafruit MPU6050

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }

  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }

  Serial.println("");
  delay(100);
}

void loop() {

  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  /* Print out the values */
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");

  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");

  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");

  Serial.println("");
  delay(500);
}

Expected result: The Serial Monitor shows acceleration, rotation, and temperature values updating continuously (with the correct baud rate selected).

Conclusion

You connected an Arduino Nano Every to an MPU6050 over I2C and used the Adafruit library example to print accelerometer and gyroscope readings in the Arduino IDE Serial Monitor. From here, you can customize the example and add your own logic as needed.

Want the parts used in this build? Grab what you need from ShillehTek.com. If you want help customizing this project or building something similar for your product, check out our IoT consulting services.