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

Ard

Uploaded by

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

Ard

Uploaded by

Harshit's Lab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

The code has been copied from the software

Contributed By Harshit Malik

#include "Wire.h" .

const int MPU_ADDR = 0x68;

int16_t accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data

int16_t gyro_x, gyro_y, gyro_z; // variables for gyro raw data

int16_t temperature; // variables for temperature data

char tmp_str[7];

char* convert_int16_to_str(int16_t i) {

sprintf(tmp_str, "%6d", i);

return tmp_str;

void setup() {

Serial.begin(9600);

Wire.begin();

Wire.beginTransmission(MPU_ADDR);

Wire.write(0x6B);

Wire.write(0);

Wire.endTransmission(true);

void loop() {

Wire.beginTransmission(MPU_ADDR);

Wire.write(0x3B);

Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a
result, the connection is kept active.

Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers


// "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable

accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and


0x3C (ACCEL_XOUT_L)

accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and


0x3E (ACCEL_YOUT_L)

accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and


0x40 (ACCEL_ZOUT_L)

temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42


(TEMP_OUT_L)

gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44


(GYRO_XOUT_L)

gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46


(GYRO_YOUT_L)

gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48


(GYRO_ZOUT_L)

Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));

Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));

Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));

Serial.print(" | tmp = "); Serial.print(temperature/340.00+36.53);

Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));

Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));

Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));

Serial.println();

delay(1000);

You might also like