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

Arduino Nano Based Angle Measurement with MPU-6050 and OLED Display

View and Edit Project In Cirkit DesignerImage of Arduino Nano Based Angle Measurement with MPU-6050 and OLED Display

Circuit Documentation

Summary

This circuit integrates an Arduino Nano microcontroller with an MPU-6050 accelerometer/gyroscope module and an OLED display. The purpose of the circuit is to read angular data from the MPU-6050 sensor and display the angle on the OLED screen. The angle is displayed within a range of -60.0 to 60.0 degrees. The Arduino Nano serves as the central processing unit, interfacing with the MPU-6050 sensor over I2C and controlling the OLED display through SPI communication.

Component List

Arduino Nano

OLED Display 2.42"

MPU-6050

Wiring Details

Arduino Nano

OLED Display 2.42"

MPU-6050

Documented Code

/*
 * This Arduino sketch reads the angle data from an MPU-6050 sensor and displays
 * the angle on an OLED display. The angle range is from -60.0 to 60.0 degrees.
 */

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <MPU6050.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET 3
#define OLED_DC 4
#define OLED_CS 10

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
MPU6050 mpu;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize I2C communication
  Wire.begin();

  // Initialize MPU-6050
  mpu.initialize();
  if (!mpu.testConnection()) {
    Serial.println("MPU6050 connection failed");
    while (1);
  }

  // Initialize OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("SSD1306 allocation failed");
    while (1);
  }
  display.display();
  delay(2000);
  display.clearDisplay();
}

void loop() {
  // Read angle from MPU-6050
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  float angle = atan2(ay, az) * 180 / PI;

  // Constrain angle to -60.0 to 60.0 degrees
  angle = constrain(angle, -60.0, 60.0);

  // Display angle on OLED
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 10);
  display.print("Angle: ");
  display.print(angle);
  display.print(" deg");
  display.display();

  delay(500);
}

This code is responsible for initializing the MPU-6050 sensor and the OLED display, reading the angle from the sensor, and displaying it on the screen. The setup() function is called once when the program starts and is used to initialize the components. The loop() function runs continuously, updating the angle display every 500 milliseconds.