Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

Arduino Interfacing with Gyro sensor diagram,working,code

This app note covers Arduino Interfacing with Gyro sensor diagram and its working. It describes Gyro sensor used and mentions arduino code.

Gyro sensor is widely used in wearable device. It is used to determine rotational motion and changes in orientation. Now-a-days gyro sensor comes with I2C interface which allows easy implementation with arduino board, microcontroller or microprocessor boards.

The typical applications of gyro sensor are angular velocity sensing, angle sensing and control mechanisms.

About Gyro sensor

The gyro sensor shown is model MPU-6050. It provides x-axis, y-axis and z-axis data together. The same will be parsed in the code to derive individual values.

Gyro sensor MPU 6050

I2C bus protocol uses two lines for communication between master and slave devices. The lines are SDA (Serial Data) and SCL (Serial Clock). Refer I2C vs SPI vs UART >> for more information on I2C interface.

About Arduino board

Arduino board

• Arduino Uno houses ATmega328 microcontroller from ATMEL. This microcontroller contains flash memory (32 KB), RAM (2 KB), 8 bit wide CPU and 1 KB EEPROM.
• It also supports 6 analog pins which reads voltage and not current. Inside, it converts analog measurement to digital for various purposes. It supports digital pins (0 to 13) which can function either as input or output.
• It has various interfaces viz. I2C, digital pins, analog pins, serial communication, USB etc.
• It also has reset pin, power port, crystal oscillator and Tx/Rx LEDs.
• This open source prototype board can be easily programmed using easy to use arduino IDE and USB interfaced between laptop PC and arduino board.
• IDE uses simplified C++ program.
• Board requires 5V DC which can be powered using AC/DC adapter or battery.

Arduino Interfacing with Gyro sensor diagram and its working

As shown in the figure-1, five pins of gyro sensor are interfaced with arduino board. The table below mentions connections between them.

Arduino Interfacing with Gyro sensor
Gyro sensor Arduino Uno Board
INT Pin 2

Arduino Gyro sensor interface code

Following is the arduino code compiled and uploaded to the arduino board using Arduino IDE. Following code reads data from MPU-6050 gyro sensor connected with arduino. For complete code and its description visit howtomechatronics.com >>.

const int MPU = 0x68; // MPU6050 I2C address
float AccX, AccY, AccZ;
float GyroX, GyroY, GyroZ;
float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
float roll, pitch, yaw;
float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
float elapsedTime, currentTime, previousTime;
int c = 0;
void setup()
Wire.begin(); // Initialize comunication
Wire.beginTransmission(MPU); // Start communication with MPU6050 // MPU=0x68
Wire.write(0x6B); // Talk to the register 6B
Wire.write(0x00); // Make reset - place a 0 into the 6B register
Wire.endTransmission(true); //end the transmission
void loop() {
// === Read acceleromter data === //
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.requestFrom(MPU, 6, true);
// Read 6 registers total, each axis value is stored in 2 registers
//For a range of +-2g, we need to divide the raw values by 16384, according to the datasheet
AccX = (Wire.read() << 8 | Wire.read()) / 16384.0; // X-axis-value
AccY = (Wire.read() << 8 | Wire.read()) / 16384.0; // Y-axis-value
AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0; // Z-axis-value
// Calculating Roll and Pitch from the accelerometer data
accAngleX = (atan(AccY / sqrt(pow(AccX, 2) + pow(AccZ, 2))) * 180 / PI) - 0.58;
// AccErrorX ~(0.58) See the calculate_IMU_error()custom function for more details
accAngleY = (atan(-1 * AccX / sqrt(pow(AccY, 2) + pow(AccZ, 2))) * 180 / PI) + 1.58;
// AccErrorY ~(-1.58)
// === Read gyroscope data === //
previousTime = currentTime; // Previous time is stored before the actual time read
currentTime = millis(); // Current time actual time read
elapsedTime = (currentTime - previousTime) / 1000; // Divide by 1000 to get seconds
Wire.write(0x43); // Gyro data first register address 0x43
Wire.requestFrom(MPU, 6, true); // Read 4 registers total, each axis value is stored in 2 registers
GyroX = (Wire.read() << 8 | Wire.read()) / 131.0;
// In 250deg/s range we have to divide first the raw value by 131.0, according to the datasheet
GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;
// Correct the outputs with the calculated error values
GyroX = GyroX + 0.56; // GyroErrorX ~(-0.56)
GyroY = GyroY - 2; // GyroErrorY ~(2)
GyroZ = GyroZ + 0.79; // GyroErrorZ ~ (-0.8)
// Currently the raw values are in degrees per seconds, deg/s, so we need to multiply
//by sendonds (s) to get the angle in degrees
gyroAngleX = gyroAngleX + GyroX * elapsedTime; // deg/s * s = deg
gyroAngleY = gyroAngleY + GyroY * elapsedTime;
yaw = yaw + GyroZ * elapsedTime;
// Complementary filter - combine acceleromter and gyro angle values
roll = 0.96 * gyroAngleX + 0.04 * accAngleX;
pitch = 0.96 * gyroAngleY + 0.04 * accAngleY;

// Print the values on the serial monitor

Conclusion: In this application note we have seen Gyro sensor interfacing with Arduino Uno board. Arduino board is also used for interfacing different types of sensors for applications. The typical sensors interfaced with arduino are sound sensor, heartbeat sensor, LDR sensor, GPS sensor, color sensor, pH sensor etc. Refer difference between accelerometer and gyroscope >> and advantages and disadvantages of gyroscope >> for more information.

IoT system on chip tutorial Related Links

This tutorial section on IoT (Internet of Things) covers following sub topics:
Main tutorial  IoT section  IoT article  Cellular IoT  Components  Sensors  INDIAN companies  Antenna Types  Wireless technologies  IoT transceiver  SoC 

RF and Wireless Terminologies

Share this page

Translate this page