MPU6050 (Gyroscope + Accelerometer + Temperature) interface with PIC18F4550.

Introduction

MPU6050 Module
MPU6050 Module

 

The MPU6050 sensor module is an integrated 6-axis Motion tracking device.

  • It has a 3-axis Gyroscope, 3-axis Accelerometer, Digital Motion Processor, and a Temperature sensor, all in a single IC.
  • It can accept inputs from other sensors like a 3-axis magnetometer, a pressure sensor using its Auxiliary I2C bus.
  • If the external 3-axis magnetometer is connected, it can provide complete 9-axis Motion Fusion output.
  • A microcontroller can communicate with this module using the I2C communication protocol.
  • Gyroscope and accelerometer reading along X, Y, and Z axes are available in 2’s complement form. The temperature reading is available in a signed integer form.
  • Gyroscope readings are in the degrees per second (DPS) unit; Accelerometer readings are in g unit, and Temperature reading is in degrees Celsius.

For more information about the MPU6050 Sensor Module and how to use it, refer to the topic MPU6050 Sensor Module in the sensors and modules section.

 

Programming PIC18F4550 for MPU6050

Let's Interface and program PIC18F4550 with MPU6050 (Gyro meter + Accelerometer + Temperature) sensor module to read all sensor values and send all values on computer terminals over USART.

  • As MPU-6050 has an I2C communication interface, we are connecting it with I2C of PIC18F4550.
  • The module requires a +5V DC power supply, so connect it to the VCC pin of a module.
  • Connect ground to the GND pin of a module.
  • Here we have used FTDI serial to USB converter to send values serially to the computer terminal.

 

Connection Diagram of MPU6050 to PIC18F4550

MPU6050 Interface with PIC18F4550
MPU6050 Gyroscope with PIC18F4550

 

MPU6050 Code for PIC18F4550

/* 
 * PIC18F4550 Interface with MPU-6050
 * http://www.electronicwings.com
 */

#include <pic18f4550.h>
#include <stdio.h>
#include <stdlib.h>
#include "USART_Header_File.h"
#include "I2C_Master_File.h"
#include "MPU6050_res_define.h"
#include "Configuration_header_file.h"

void MPU6050_Init()		/* Gyro initialization function */
{
	MSdelay(150);		/* Power up time >100ms */
	I2C_Start_Wait(0xD0);	/* Start with device write address */
	I2C_Write(SMPLRT_DIV);	/* Write to sample rate register */
	I2C_Write(0x07);	/* 1KHz sample rate */
	I2C_Stop();

	I2C_Start_Wait(0xD0);
	I2C_Write(PWR_MGMT_1);	/* Write to power management register */
	I2C_Write(0x01);	/* X axis gyroscope reference frequency */
	I2C_Stop();

	I2C_Start_Wait(0xD0);
	I2C_Write(CONFIG);	/* Write to Configuration register */
	I2C_Write(0x00);	/* Fs = 8KHz */
	I2C_Stop();

	I2C_Start_Wait(0xD0);
	I2C_Write(GYRO_CONFIG);	/* Write to Gyro configuration register */
	I2C_Write(0x18);	/* Full scale range +/- 2000 degree/C */
	I2C_Stop();

	I2C_Start_Wait(0xD0);
	I2C_Write(INT_ENABLE);	/* Write to interrupt enable register */
	I2C_Write(0x01);
	I2C_Stop();
}

void MPU_Start_Loc()
{
	I2C_Start_Wait(0xD0);	/* I2C start with device write address */
	I2C_Write(ACCEL_XOUT_H);/* Write start location address to read */ 
	I2C_Repeated_Start(0xD1);/* I2C start with device read address */
}

void main()
{
	char buffer[20];
	int Ax,Ay,Az,T,Gx,Gy,Gz;
	float Xa,Ya,Za,t,Xg,Yg,Zg;
    	OSCCON = 0x72;
    	I2C_Init();		/* Initialize I2C */	
	MPU6050_Init();		/* Initialize Gyro */
	USART_Init(9600);	/* Initialize USART with 9600 baud rate */
	
	while(1)
	{
		MPU_Start_Loc();
		/* Read Gyro values continuously & send to terminal over UART */
		Ax = (((int)I2C_Read(0)<<8) | (int)I2C_Read(0));
		Ay = (((int)I2C_Read(0)<<8) | (int)I2C_Read(0));
		Az = (((int)I2C_Read(0)<<8) | (int)I2C_Read(0));
		T =  (((int)I2C_Read(0)<<8) | (int)I2C_Read(0));
		Gx = (((int)I2C_Read(0)<<8) | (int)I2C_Read(0));
		Gy = (((int)I2C_Read(0)<<8) | (int)I2C_Read(0));
		Gz = (((int)I2C_Read(0)<<8) | (int)I2C_Read(1));
		I2C_Stop();
        
		
		/* Divide raw value by sensitivity scale factor */
		Xa = (float)Ax/16384.0;	
		Ya = (float)Ay/16384.0;
		Za = (float)Az/16384.0;
		Xg = (float)Gx/131.0;
		Yg = (float)Gy/131.0;
		Zg = (float)Gz/131.0;
		t = ((float)T/340.00)+36.53;/* Convert temperature in °/c */


		/* Take values in buffer to send all parameters over USART */
		sprintf(buffer," Ax = %.2f g\t",Xa);
		USART_SendString(buffer);

		sprintf(buffer," Ay = %.2f g\t",Ya);
		USART_SendString(buffer);
		
		sprintf(buffer," Az = %.2f g\t",Za);
		USART_SendString(buffer);

		/* 0xF8 Ascii value of degree '°' on serial */
		sprintf(buffer," T = %.2f%cC\t",t,0xF8);
		USART_SendString(buffer);

		sprintf(buffer," Gx = %.2f%c/s\t",Xg,0xF8);
		USART_SendString(buffer);

		sprintf(buffer," Gy = %.2f%c/s\t",Yg,0xF8);
		USART_SendString(buffer);
		
		sprintf(buffer," Gz = %.2f%c/s\r\n",Zg,0xF8);
		USART_SendString(buffer);
	}
}

 

Output Window of Terminal

Output window will show all values mentioned below

Ax  = Accelerometer x axis data in g unit

Ay  = Accelerometer y axis data in g unit

Az  = Accelerometer z axis data in g unit

T = temperature in degree/celcius

Gx  = Gyro x axis data in degree/seconds unit

Gy  = Gyro y axis data in degree/seconds unit

Gz  = Gyro z axis data in degree/seconds unit

 

Output Terminal

Components Used

MPU6050 Gyroscope and Accelerometer
MPU6050 (Gyroscope + Accelerometer + Temperature) is a combination of 3-axis Gyroscope, 3-axis Accelerometer and Temperature sensor with on-chip Digital Motion Processor (DMP). It is used in mobile devices, motion enabled games, 3D mice, Gesture (motion command) technology etc
1
USB to Serial Converter CP2104
USB to Serial Converter CP2104
1
PICKit 4 MPLAB
PICKit 4 MPLAB
1
PIC18f4550
PIC18f4550
1

Downloads

MPU6050 DataSheet Download
RM MPU60xxA Download
MPU6050 Interface with PIC Project File Download
Ad