Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Based Pitch and Roll Display with MPU6050 and I2C LCD

View and Edit Project In Cirkit DesignerImage of Arduino-Based Pitch and Roll Display with MPU6050 and I2C LCD

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller interfacing with an I2C LCD 16x2 screen and an MPU-9250/6500/9255 sensor. The Arduino UNO serves as the central controller, managing the display and sensor data. The I2C protocol is used for communication between the Arduino, the LCD screen, and the MPU sensor.

Component List

Arduino UNO

I2C LCD 16x2 Screen

MPU-9250/6500/9255

Wiring Details

Arduino UNO

I2C LCD 16x2 Screen

MPU-9250/6500/9255

Code Documentation

Arduino Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <MPU6050.h>

// Initialize the MPU6050
MPU6050 mpu;

// Initialize the LCD, set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  // Initialize the I2C communication
  Wire.begin();
  
  // Initialize the LCD with 16 columns and 2 rows
  lcd.begin(16, 2);
  lcd.backlight();
  
  // Initialize the MPU6050
  mpu.initialize();
  
  // Check if the MPU6050 is connected
  if (!mpu.testConnection()) {
    lcd.print("MPU6050 error");
    while (1);
  }
}

void loop() {
  // Variables to store the orientation data
  int16_t ax, ay, az;
  int16_t gx, gy, gz;
  
  // Read the raw accelerometer and gyroscope data
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  // Calculate pitch and roll
  float pitch = atan2(ax, sqrt(ay * ay + az * az)) * 180.0 / PI;
  float roll = atan2(ay, az) * 180.0 / PI;
  
  // Display the pitch and roll on the LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Pitch: ");
  lcd.print(pitch);
  lcd.setCursor(0, 1);
  lcd.print("Roll: ");
  lcd.print(roll);
  
  // Delay for a while
  delay(500);
}

This code initializes the I2C communication, sets up the LCD display, and initializes the MPU6050 sensor. In the loop function, it reads the accelerometer and gyroscope data from the MPU6050, calculates the pitch and roll, and displays these values on the LCD screen.