Skip to content

Instantly share code, notes, and snippets.

@fxprime
Created March 7, 2025 07:12
Show Gist options
  • Save fxprime/595eb5517c34a0151dca76e55bbb4522 to your computer and use it in GitHub Desktop.
Save fxprime/595eb5517c34a0151dca76e55bbb4522 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
/* Get all possible data from MPU6050
* Accelerometer values are given as multiple of the gravity [1g = 9.81 m/s²]
* Gyro values are given in deg/s
* Angles are given in degrees
* Note that X and Y are tilt angles and not pitch/roll.
*
* License: MIT
*/
/*
rfetick/MPU6050_light @ ^1.1.0
*/
#include "Wire.h"
#include <MPU6050_light.h>
MPU6050 mpu(Wire);
long timer = 0;
void setup() {
Serial.begin(115200);
Wire.begin(21,22,400000); // SDA, SCL, 400kHz
byte status = mpu.begin();
Serial.print(F("MPU6050 status: "));
Serial.println(status);
while(status!=0){ } // stop everything if could not connect to MPU6050
Serial.println(F("Calculating offsets, do not move MPU6050"));
delay(1000);
mpu.calcOffsets(true,true); // gyro and accelero
Serial.println("Done!\n");
}
void loop() {
mpu.update();
if(millis() - timer > 100){ // print data every second
Serial.print(F("TEMPERATURE: "));Serial.println(mpu.getTemp());
Serial.print(F("ACCELERO X: "));Serial.print(mpu.getAccX());
Serial.print("\tY: ");Serial.print(mpu.getAccY());
Serial.print("\tZ: ");Serial.println(mpu.getAccZ());
Serial.print(F("GYRO X: "));Serial.print(mpu.getGyroX());
Serial.print("\tY: ");Serial.print(mpu.getGyroY());
Serial.print("\tZ: ");Serial.println(mpu.getGyroZ());
Serial.print(F("ACC ANGLE X: "));Serial.print(mpu.getAccAngleX());
Serial.print("\tY: ");Serial.println(mpu.getAccAngleY());
Serial.print(F("ANGLE X: "));Serial.print(mpu.getAngleX());
Serial.print("\tY: ");Serial.print(mpu.getAngleY());
Serial.print("\tZ: ");Serial.println(mpu.getAngleZ());
Serial.println(F("=====================================================\n"));
timer = millis();
}
}
/*
TEMPERATURE: 33.40
ACCELERO X: -0.01 Y: -0.00 Z: 1.01
GYRO X: -0.21 Y: -0.10 Z: 0.19
ACC ANGLE X: -0.14 Y: 0.77
ANGLE X: -0.33 Y: 0.47 Z: -1.83
=====================================================
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment