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

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

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

Circuit Documentation

Summary

This circuit involves an Arduino Nano microcontroller interfaced with an MPU-6050 sensor and an OLED display. The Arduino Nano reads angle data from the MPU-6050 sensor and displays the angle on the OLED display. The angle range is from -60.0 to 60.0 degrees.

Component List

Arduino Nano

OLED Display 2.42"

MPU-6050

Wiring Details

Arduino Nano

OLED Display 2.42"

MPU-6050

Code Documentation

/*
 * 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 initializes the MPU-6050 sensor and the OLED display, reads the angle data from the sensor, and displays the angle on the OLED display. The angle is constrained to a range of -60.0 to 60.0 degrees.