0% found this document useful (0 votes)
53 views2 pages

MPU6050 I2C Arduino Code Example

This code is initializing an MPU6050 accelerometer and gyroscope sensor module to read acceleration and gyroscope data from an Arduino. It sets up the I2C communication, initializes the sensor, verifies the connection, and then reads the sensor data in a loop. It outputs the sensor values either as readable text or binary data over the serial port. It also blinks an LED with each new sensor reading.

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views2 pages

MPU6050 I2C Arduino Code Example

This code is initializing an MPU6050 accelerometer and gyroscope sensor module to read acceleration and gyroscope data from an Arduino. It sets up the I2C communication, initializes the sensor, verifies the connection, and then reads the sensor data in a loop. It outputs the sensor values either as readable text or binary data over the serial port. It also blinks an LED with each new sensor reading.

Uploaded by

Aman Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include "I2Cdev.

h"
#include "MPU6050.h"

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation


// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif

// class default I2C address is 0x68


// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high

int16_t ax, ay, az;


int16_t gx, gy, gz;

// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated


// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
// not so easy to parse, and slow(er) over UART.
#define OUTPUT_READABLE_ACCELGYRO

// uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit


// binary, one right after the other. This is very fast (as fast as possible
// without compression or data loss), and easy to parse, but impossible to read
// for a human.
//#define OUTPUT_BINARY_ACCELGYRO

#define LED_PIN 13
bool blinkState = false;

void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
[Link]();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

// initialize serial communication


// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
[Link](38400);

// initialize device
[Link]("Initializing I2C devices...");
[Link]();

// verify connection
[Link]("Testing device connections...");
[Link]([Link]() ? "MPU6050 connection successful" :
"MPU6050 connection failed");

// use the code below to change accel/gyro offset values


/*
[Link]("Updating internal sensor offsets...");
// -76 -2359 1688 0 0 0
[Link]([Link]()); [Link]("\t"); // -76
[Link]([Link]()); [Link]("\t"); // -2359
[Link]([Link]()); [Link]("\t"); // 1688
[Link]([Link]()); [Link]("\t"); // 0
[Link]([Link]()); [Link]("\t"); // 0
[Link]([Link]()); [Link]("\t"); // 0
[Link]("\n");
[Link](220);
[Link](76);
[Link](-85);
[Link]([Link]()); [Link]("\t"); // -76
[Link]([Link]()); [Link]("\t"); // -2359
[Link]([Link]()); [Link]("\t"); // 1688
[Link]([Link]()); [Link]("\t"); // 0
[Link]([Link]()); [Link]("\t"); // 0
[Link]([Link]()); [Link]("\t"); // 0
[Link]("\n");
*/

// configure Arduino LED for


pinMode(LED_PIN, OUTPUT);
}

void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

// these methods (and a few others) are also available


//[Link](&ax, &ay, &az);
//[Link](&gx, &gy, &gz);

#ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y/z values
[Link]("a/g:\t");
[Link](ax); [Link]("\t");
[Link](ay); [Link]("\t");
[Link](az); [Link]("\t");
[Link](gx); [Link]("\t");
[Link](gy); [Link]("\t");
[Link](gz);
#endif

#ifdef OUTPUT_BINARY_ACCELGYRO
[Link]((uint8_t)(ax >> 8)); [Link]((uint8_t)(ax & 0xFF));
[Link]((uint8_t)(ay >> 8)); [Link]((uint8_t)(ay & 0xFF));
[Link]((uint8_t)(az >> 8)); [Link]((uint8_t)(az & 0xFF));
[Link]((uint8_t)(gx >> 8)); [Link]((uint8_t)(gx & 0xFF));
[Link]((uint8_t)(gy >> 8)); [Link]((uint8_t)(gy & 0xFF));
[Link]((uint8_t)(gz >> 8)); [Link]((uint8_t)(gz & 0xFF));
#endif

// blink LED to indicate activity


blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}

You might also like