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

MPU6050 Module
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 3-axis magnetometer, a pressure sensor using its Auxiliary I2C bus.
- If external 3-axis magnetometer is connected, it can provide complete 9-axis Motion Fusion output.
- A microcontroller can communicate with this module using I2C communication protocol.
- Gyroscope and accelerometer reading along X, Y and Z axes are available in 2’s complement form. Temperature reading is available in 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 MPU6050 Sensor Module and how to use it, refer 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 I2C communication interface, we are connecting it with I2C of PIC18F4550.
- The module requires +5V DC power supply, so connect it to VCC pin of a module.
- Connect ground to GND pin of a module.
- Here we have used FTDI serial to USB converter to send values serially to the computer terminal.
Interfacing MPU6050 with PIC18F4550

PIC18F4550 Interface with MPU6050
Program
/*
* 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
.png)
No comments:
Post a Comment