Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

ELEGOO MEGA · LESSON 16

IMU Module: MPU-6050 / QMI8658C

Print raw acceleration, angular-rate and temperature readings from the identified IMU.

Choose the correct sensor address, register order and driver for the actual IMU.

MPU-6050 / GY-521

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • The GY-521 carries an MPU-6050 six-axis IMU that digitizes three acceleration axes and three angular-rate axes over I²C. Compact GY-521 board with a small square IC, mounting holes and labeled power/I²C/interrupt pins. Confirm the MPU-6050 marking and axis arrows rather than assuming every blue board is the same IMU.
  • An 830-contact solderless breadboard joins inserted leads through hidden spring strips so circuits can be rebuilt without solder. Full-size rectangular board, center channel, numbered five-hole terminal groups and side power rails. Counted contact holes are not component units.
  • Male-to-male jumper leads make removable point-to-point connections for low-current prototype signals. Pins at both ends; count individual leads rather than ribbon groups. Check intact insulation and snug contacts before use.
  • Male-to-female jumper leads make removable point-to-point connections for low-current prototype signals. Pin at one end and socket at the other. Check intact insulation and snug contacts before use.
  • ?
    1 ×Adafruit 757 four-channel BSS138 I2C level-shifter module, with headers fitted
    Unconfirmed · Availability and exact identity unconfirmed.

Assembly

  1. Unplug all power and identify a regulated GY-521/MPU-6050, not QMI8658C.
  2. Place the Adafruit 757 level-shifter with headers fitted on the breadboard; connect its HV/LV and common ground before the two signal-channel pairs.
  3. Leave INT, XDA, XCL and AD0 unconnected as in the source experiment; AD0 must be low/default for 0x68.
  4. Reconnect USB after checking both translator channels.

The original omits I2C level translation. Use the corrected Adafruit 757 circuit on D20/D21; the GY-521 regulator does not establish 5 V logic tolerance.

Reviewed pin-to-pin connections
FromToPurpose / notes
Mega: 5VGY-521 regulated module: VCC input
Mega: GNDGY-521 regulated module: GND
Mega: 5VI2C translator: HV
Mega: 3.3VI2C translator: LV
Mega: GNDI2C translator: GND
Mega: D20 / SDAI2C translator: B1 (HV1)
I2C translator: A1 (LV1)GY-521 regulated module: SDA
Mega: D21 / SCLI2C translator: B2 (HV2)
I2C translator: A2 (LV2)GY-521 regulated module: SCL
Open vector connection diagram ↗

Code

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600

Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.

main.cpp
#include <Arduino.h>
// Wire.h gives us I2C communication, used to talk to the motion sensor.
#include <Wire.h>

// 0x68 is the sensor I2C address for the reviewed MPU-6050 setup.
const uint8_t MPU_ADDRESS = 0x68;
// Use a true/false flag to remember whether initialization worked.
bool sensorReady = false;

// Open Serial, start I2C, and wake the sensor from sleep mode.
void setup() {
  Serial.begin(9600);
  Wire.begin();
  // Use a 100 kHz I2C clock for communication.
  Wire.setClock(100000);
  // Begin writing to the sensor at its I2C address.
  Wire.beginTransmission(MPU_ADDRESS);
  Wire.write(0x6B);  // PWR_MGMT_1: leave sleep mode.
  Wire.write(0);
  // A zero result means the I2C write was acknowledged.
  sensorReady = Wire.endTransmission() == 0;
}

// If the sensor responded, request a group of accelerometer and gyro values.
void loop() {
  // If initialization failed, report it and skip this reading.
  if (!sensorReady) {
    Serial.println("MPU-6050 not detected at 0x68");
    delay(1000);
    return;
  }
  Wire.beginTransmission(MPU_ADDRESS);
  // 0x3B points to the first measurement register; the next 14 bytes hold seven values.
  Wire.write(0x3B);  // ACCEL_XOUT_H; seven big-endian 16-bit values follow.
  // If the sensor does not respond or returns fewer bytes than expected, report an error.
  if (Wire.endTransmission(false) != 0 ||
      Wire.requestFrom(MPU_ADDRESS, uint8_t(14), uint8_t(true)) != 14) {
    Serial.println("Incomplete MPU-6050 read");
    delay(333);
    return;
  }
  // Store seven signed 16-bit readings: acceleration XYZ, temperature, and gyro XYZ.
  int16_t value[7];
  // Each reading is two bytes; combine high byte first because this sensor is big-endian.
  for (uint8_t i = 0; i < 7; ++i) {
    const uint16_t high = uint8_t(Wire.read());
    const uint16_t low = uint8_t(Wire.read());
    // << shifts the high byte left; | joins it with the low byte.
    value[i] = int16_t((high << 8) | low);
  }
  Serial.print("AcX="); Serial.print(value[0]);
  Serial.print(" | AcY="); Serial.print(value[1]);
  Serial.print(" | AcZ="); Serial.print(value[2]);
  // Convert the raw temperature value using the MPU-6050 formula.
  Serial.print(" | Tmp="); Serial.print(value[3] / 340.0 + 36.53);
  Serial.print(" | GyX="); Serial.print(value[4]);
  Serial.print(" | GyY="); Serial.print(value[5]);
  Serial.print(" | GyZ="); Serial.println(value[6]);
  delay(333);
}

Expected behavior

At 9600 baud, AcX/AcY/AcZ and GyX/GyY/GyZ change as the module moves; Tmp is the MPU’s internal die temperature, not a calibrated ambient thermometer.

Experiments & Challenges

  • Hold the board still, then rotate about one axis and compare the raw gyro channels.

Troubleshooting

  • Not detected at 0x68: verify sensor identity, AD0 state, translator power and SDA20/SCL21.

QMI8658C — not yet approved for assembly

Parts List

  • ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
  • A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
  • ?
    1 ×QMI8658C breakout, exact ELEGOO module revision and input-power rating unresolved
    Unconfirmed · Availability and exact identity unconfirmed.

Assembly blocked

  1. Do not assemble this variant yet: identify the exact QMI8658C breakout and establish its VIN, VDDIO and I2C level requirements.
  2. Do not copy the MPU-6050 power or pin order; the candidate project is supplied for software review/build only.

Candidate code · software review only

PlatformIO configuration
platformio.ini
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
main.cpp
#include <Arduino.h>
#include <Wire.h>

// IMPORTANT: This is code for study only. Do not assemble an unidentified QMI8658C breakout.
// QMI8658C only. Breakout power/level wiring remains blocked pending identification.
// 0x6B is the address expected by this QMI sensor candidate.
const uint8_t QMI_ADDRESS = 0x6B;
bool sensorReady = false;

// Helper function: write one configuration byte to a sensor register.
bool writeRegister(uint8_t reg, uint8_t value) {
  // Start an I2C message addressed to the QMI8658C.
  Wire.beginTransmission(QMI_ADDRESS);
  Wire.write(reg);
  Wire.write(value);
  // Return true if the write was acknowledged; otherwise false.
  return Wire.endTransmission() == 0;
}

// Helper function: read count bytes, placing them into the data array through a pointer.
bool readRegisters(uint8_t reg, uint8_t *data, uint8_t count) {
  Wire.beginTransmission(QMI_ADDRESS);
  Wire.write(reg);
  // If the register selection fails or the byte count is wrong, stop and return false.
  if (Wire.endTransmission(false) != 0 ||
      Wire.requestFrom(QMI_ADDRESS, count, uint8_t(true)) != count) return false;
  // Read each byte in sequence into the caller-provided array.
  for (uint8_t i = 0; i < count; ++i) data[i] = uint8_t(Wire.read());
  return true;
}

// Start Serial and I2C, then check the chip identity before configuring it.
void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.setClock(100000);
  delay(1000);
  uint8_t id = 0;
  // &id passes the address of id; only proceed if the expected chip ID is found.
  if (!readRegisters(0x00, &id, 1) || id != 0x05) return;
  // Chain several register writes with &&; all must succeed to set sensorReady true.
  sensorReady = writeRegister(0x02, 0x40) &&  // Address auto-increment.
                writeRegister(0x03, 0x04) &&  // Accelerometer: +/-2g, 500Hz.
                writeRegister(0x04, 0x64) &&  // Gyroscope: +/-2048dps, 500Hz.
                writeRegister(0x06, 0x11) &&  // Low-pass filters.
                writeRegister(0x08, 0x03);    // Enable accelerometer and gyro.
  delay(2000);
}

// Report failures or read fourteen sensor bytes and interpret them.
void loop() {
  if (!sensorReady) {
    Serial.println("QMI8658C not detected/configured at 0x6B (WHO_AM_I must be 0x05)");
    delay(1000);
    return;
  }
  // Store the raw fourteen bytes received from the sensor.
  uint8_t bytes[14];
  // sizeof(bytes) gets the number of bytes in this fixed array.
  if (!readRegisters(0x33, bytes, sizeof(bytes))) {
    Serial.println("Incomplete QMI8658C read");
    delay(50);
    return;
  }
  int16_t value[7];
  // Pair low and high bytes for each signed value; this sensor is little-endian.
  for (uint8_t i = 0; i < 7; ++i) {
    // Combine the two bytes into one signed 16-bit measurement.
    value[i] = int16_t((uint16_t(bytes[2 * i + 1]) << 8) | bytes[2 * i]);
  }
  // QMI8658C is little-endian; temperature is 256 LSB per degree C.
  Serial.print("AcX="); Serial.print(value[1]);
  Serial.print(" | AcY="); Serial.print(value[2]);
  Serial.print(" | AcZ="); Serial.print(value[3]);
  // Convert raw temperature using 256 counts per degree Celsius.
  Serial.print(" | Tmp="); Serial.print(value[0] / 256.0);
  Serial.print(" | GyX="); Serial.print(value[4]);
  Serial.print(" | GyY="); Serial.print(value[5]);
  Serial.print(" | GyZ="); Serial.println(value[6]);
  delay(50);
}

Expected behavior

After an approved breakout assembly, code expects WHO_AM_I0x05 at 0x6B and prints signed little-endian motion data plus die temperature. No physical result has been verified.

Troubleshooting

  • Detection failure is not evidence to install the MPU6050 driver; the addresses/register maps are different.