MPU6050 init Problem

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I am sorry for terrible look of the post, its my first time.

I'm quite fresh to AVR Microcontrollers (just started a few weeks ago) and recently, for the first time, I faced problem I couldn't overcame byt just googling solution and trying it out.

Despite having some experience with C and C++ I struggle with writing more complicated libraries. I wrote one for USART and its working perfectly but when it came to useage of i2c in context of reading values from gyroscope I totaly failed.

Coming to my Hardware: I use Atmega8A and MPU6050 connected on SCL/SDA and BLUETOOTH HC05 on RX/TX. As you can guess my goal is to send results from gyro to the pc via BT.

I have tried multiple external libs found the one internet for i2c and mpu6050 unfortunetely I never achieved even reading single byte... crying. As far as I could verify them with atmega DS, problem could be lying on MPU lib but these were quite complicated.

finally I found this project: http://www.electronicwings.com/a... and I copied libraries as they looked really promising ( I have checked if declaration of TWI is different however they don't vary in almost most Atmega's)

_________________________________________________________________________________________________________________________________________________________________________________________________________

#ifndef F_CPU
#define F_CPU 8000000UL // 1 MHz clock speed
#endif
#define BAUDRATE 51 //atmega datasheet, pages 184 and 207

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

#include "MPU6050_res_define.h"							/* Include MPU6050 register define file */
#include "I2C_Master_H_file.h"							/* Include I2C Master header file */

float Acc_x,Acc_y,Acc_z,Temperature,Gyro_x,Gyro_y,Gyro_z;

/////////////////////////////////////////////////////////////////////////////////////////////////////// RX/ TX COMM////////////////////////////////////////////////////////////////////////
#define RX_BUFFER_SIZE 32
#define TX_BUFFER_SIZE 32

volatile unsigned char rec_flag=0;
volatile unsigned char rec_char;

char tx_buffer[TX_BUFFER_SIZE] = "EXAMPLE_CODE1234EXAMPLE_CODE1234";
volatile unsigned int tx_buffer_ind;

char rx_buffer[RX_BUFFER_SIZE] = "EXAMPLE_CODE1234EXAMPLE_CODE1234";
volatile unsigned int rx_buffer_ind;
//-------------------------------------------------------------------------------------------------------

//HERE WAS DECLARATION OF USART TRANSMISSION WHICH IS COMPLETLY FINE AND NOT NECCESARY

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Gyro_Init()										/* Gyro initialization function */
{
	_delay_ms(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 from where to read */
	I2C_Repeated_Start(0xD1);							/* I2C start with device read address */
}

void Read_RawValue()
{
	MPU_Start_Loc();									/* Read Gyro values */
	Acc_x = ~((((int)I2C_Read_Ack()<<8) | (int)I2C_Read_Ack())-1);
	Acc_y = ~((((int)I2C_Read_Ack()<<8) | (int)I2C_Read_Ack())-1);
	Acc_z = ~((((int)I2C_Read_Ack()<<8) | (int)I2C_Read_Ack())-1);

	Gyro_x = ~((((int)I2C_Read_Ack()<<8) | (int)I2C_Read_Ack())-1);
	Gyro_y = ~((((int)I2C_Read_Ack()<<8) | (int)I2C_Read_Ack())-1);
	Gyro_z = ~((((int)I2C_Read_Ack()<<8) | (int)I2C_Read_Nack())-1);
	I2C_Stop();
}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{

	usart_Init();	//INITIALIZATION OF RX/TX COMMUNICATION
		_delay_ms(50);
	I2C_Init();											/* Initialize I2C */
		_delay_ms(50);
	Gyro_Init();										/* Initialize Gyro */
		_delay_ms(50);
	double gx=0, gy=0, gz=0;

	while(1){
			//Read_RawValue();
			_delay_ms(1000);
		gx = Gyro_x/16.4;
		gy = Gyro_y/16.4;
		gz = Gyro_z/16.4;
			transmittData("x ",0);
	}
}

____________________________________________________________________________________________________________________________________________________________________________________________________________

I have tested and USART functions are completly fine. Copied functions I am definetely not sure are: 

Gyro_Init, MPU_Start_Loc, Read_RawValue

I am wondering because it is not the first time as someone is declaring MPU6050 Adress for 0xD0 or 0xD1... I completly dont understand as in the MPU DS (https://www.invensense.com/wp-co...)

It's adress is declared as 0x68 if D0 PIN in Low or 0x69 if D0 pin is High. Obviously I tried exchanging it but it didnt help. Perhaps my Gyro is broken, However the LED on it is shining when pinned properly to power.

I'd gladly apprecieate any help. Perhaps somebody had similar problem or have written some libs for sucha project on atm8a ?

Chris

This topic has a solution.
Last Edited: Fri. Sep 22, 2017 - 02:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First welcome to the forum!

 

I2C (TWI) is a very friendly protocol when you use it properly, i.e. call functions and check return values!  It will tell you what is wrong if you do that.

Most here would use Peter Fluery's I2C driver.  http://homepage.hispeed.ch/peter...

Show us your schematic, I2C needs pull up resistors on the SCL/SDA lines, what did you use?

 

Jim

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you Jim for this quick reply.

I have pullup resistors 10k. As mpu board uses voltage 3.3 - 5V I connected it to ~ 4V (stabilized). as my General Supply for atmega and Hc-05 is a bit over 5V and I know that some sensors are extremly fragile in that case. GND is obviously bound together.

                               

 

Last Edited: Wed. Aug 30, 2017 - 08:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've downloaded Peters Fluery's I2C lib and found out that I doesnt work somehow. First of all #include <i2cmaster.h> in twimaster.c wasnt noticed "No such file or directory", however I fixed it by changing into #include "i2cmaster.h"

                                                          

meanwhile main.c just contains test code provided together with library, It stops working very early on first error...

#define Dev24C02  0xA2      // device address of EEPROM 24C02, see datasheet

int main(void)
{
    unsigned char ret;

    DDRB  = 0xff;                              // use all pins on port B for output
    PORTB = 0xff;                              // (active low LED's )

    i2c_init();                                // init I2C interface

    /* write 0x75 to eeprom address 0x05 (Byte Write) */
    ret = i2c_start(Dev24C02+I2C_WRITE);       // set device address and write mode

    if ( ret ) {
        /* failed to issue start condition, possibly no device found */
        i2c_stop();
        PORTB=0x00;                            // activate all 8 LED to show error */
    }else {
        /* issuing start condition ok, device accessible */

 

I suppose this I2C test should be performed with some device connected to SCL/SDA right ? Or I misunderstood something.. -,-'

Above tests were performed with my MPU6050 connected to SDA/SCL

 

 PORTB=0x00;                            // activate all 8 LED to show error */

 

^ According to the comment I face some error. No idea what kind of. All I was guessing was improper adress but this brought be to changing Dev24C02 def to 0xA0, and the address taken from my MPU6050 DS which is 0x68 (when PIN AD0 is L)

No luck though

 

Chris

Last Edited: Wed. Aug 30, 2017 - 10:20 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I write to anyone who accidentely approaches this topic. The problem lied in bad connection of the sensor (some wires were not connecting). After I made a new slot for the sensor the original code worked.

 

Chris