i have rigged up this sensor to my atmega 8 microcontroller :
here is the image of sensor:Arduino Playground - HomePage
i have given 3v supply to the board. And i am using pleury's header file for i2c communication b/w my sensor & atmega. so below i have written a code to see just whether the device is accessible or not
pin connections to mpu:
vcc-3v
scl,sda pins of mpu: i have connected 4.7k pulls ups through 3v supply as per the dig here; http://blog.codeblack.nl/image.axd?picture=2011%2F7%2FNetduino.I2C.png and have given it to scl (pc5) &sda (pc4) pins of atmega.
xcl,xda pins are left open.
ad0 pin is given to 0v.
int pin open.
as per datasheet if ad0=0 then address of the device =0X68.
i am using avr studio 4.
here is my code:
#include<avr/io.h> //This is the header for AVR Microcontroller.
#define F_CPU 12000000UL
#include "i2cmaster.h"
#include<util/delay.h>
#include"lcd118010.h" //lcd
#define mpu 0X68 //address of the i2c device
int main(void)
{
unsigned char ret=0;
PORTC=0X00; //disabling the internal pull up for sda,scl
lcd_init();
i2c_init(); //initialise i2c communication
while(1)
{
ret=i2c_rep_start(mpu); //send adress to recognize slave device and if ret=0 device is accesible ret=1 device is not accessible
lcd_showvalue(ret); //print that value on lcd
_delay_ms(1900);
lcd_clear(); //clear lcd & continue
}
}
my observations :so ret should either print 0 or 1. but lcd remains blank (there is no print at all). i have tested the lcd (it's working all fine with other programs). I am unable to figure out what is the exact problem :0
Why do you use a different I2C libary, you can use the Arduino Wire library.
Please the newest Arduino Uno.
You don't need pullup resistors, they are already on that MPU-6050 module.
i had to tell it before itself i searched for help on most of the sites i did not get that is why i joined here though i donot use aurdino i use avr atmega 8-8l .
When you know the settings for the fuses and so, you can use the Arduino IDE to write a sketch. That way you can test the I2C. But you do need to connect RX and TX to serial port (for example with a usb-ttl-serial converter).
When you write the sketch without bootloader, the Arduino IDE also sets the fuses. You have to set those to your own values after you continue with your own code.
Perhaps you think that your code is smaller than the Arduino code ? That would be a nice challenge You could try your test with Arduino functions, I think you will be surprised.
To able to pinpoint the problem, we need to know all the libraries you use, how the hardware wiring is.
The MPU-6050 should work with 3V. You did connect the GND to the sensor ?
I think that the ATmega8 won't work at 3V, but the ATmega8A and ATmega8L do.
The I2C could be wrong, when the MPU-6050 is not powered, or the SDA and SCL are shortcut to each other.
Do you at least have an Arduino board ? You can use that to test the MPU-6050 module.
The site http://www.avrfreaks.net/ is for ATmega projects without Arduino. Sometimes I use the code in the Projects section. I don't know if they can be more of help than we. You problem seems to be to get the I2C-bus running or the I2C library working.
When you know the settings for the fuses and so, you can use the Arduino IDE to write a sketch. That way you can test the I2C. But you do need to connect RX and TX to serial port (for example with a usb-ttl-serial converter).
When you write the sketch without bootloader, the Arduino IDE also sets the fuses. You have to set those to your own values after you continue with your own code.
Perhaps you think that your code is smaller than the Arduino code ? That would be a nice challenge You could try your test with Arduino functions, I think you will be surprised.
well these i can do only when i will have my aurdino board.
To able to pinpoint the problem, we need to know all the libraries you use,
The I2C could be wrong, when the MPU-6050 is not powered, or the SDA and SCL are shortcut to each other.
i didnot get what do you mean by this..
Do you at least have an Arduino board ? You can use that to test the MPU-6050 module.
no i donot have aurdino board.
The site http://www.avrfreaks.net/ is for ATmega projects without Arduino. Sometimes I use the code in the Projects section. I don't know if they can be more of help than we. You problem seems to be to get the I2C-bus running or the I2C library working.
The I2C could be wrong, when the MPU-6050 is not powered, or the SDA and SCL are shortcut to each other.
i didnot get what do you mean by this..
When the sensor board would be okay, but the I2C is not working, it can be cause by SDA or SCL being pulled low (for example when the MPU-6050 is not powered with 3V or 5V), or the SDA is shortcut to SCL.
I did take a look at the I2C library by Peter Fleury, but I'm not sure how to use it.
Your test with "i2c_rep_start()" seems okay, but I don't know what "lcd_showvalue()" does with the 'ret' value. It should print a value, but if it prints character 0x00 or character 0x01, you probably won't see a thing. You can make 'ret' an integer to avoid that.
Can you measure the I2C bus with a logical analyzer or scope ?
In the library is something different:
ret = i2c_start( address ); // set device address
if ( ret == 0 ) {
/* failed to issue start condition, possibly no device found */
i2c_stop();
}
Peter_n:
When the sensor board would be okay, but the I2C is not working, it can be cause by SDA or SCL being pulled low (for example when the MPU-6050 is not powered with 3V or 5V), or the SDA is shortcut to SCL.
no that fault should not exist as i have indeed powered the controller board with 5v & sensor board with 3v. Am i right?
I did take a look at the I2C library by Peter Fleury, but I'm not sure how to use it.
Your test with "i2c_rep_start()" seems okay, but I don't know what "lcd_showvalue()" does with the 'ret' value. It should print a value, but if it prints character 0x00 or character 0x01, you probably won't see a thing. You can make 'ret' an integer to avoid that.
ok in the code my post #1 as you can see what my observations are.
i2c_rep_start(); in the zip provided by fleury here is what they explain about the kunction i am using in my code #1:
/*************************************************************************
Issues a repeated start condition and sends address and transfer direction
Input: address and transfer direction of I2C device
Return: 0 device accessible
1 failed to access device
*************************************************************************/
unsigned char i2c_rep_start(unsigned char address)
{
return i2c_start( address );
}/* i2c_rep_start */
so in my case ret is a unsigned char value.
And the lcd header file which i had got when i attended some workshop on robotics the function is:
void lcd_showvalue(unsigned char num) //prints the decimal 3digit value of num
{
unsigned char H=0,T=0,O=0;
H=num/100;
T=(num - (H*100))/10;
O=(num - (H*100) - (T*10));
lcd_char(H+48);
lcd_char(T+48);
lcd_char(O+48);
}
so the ret value is unsigned char & i am making it print on lcd using above function. I have already used these functions & my lcd is ok..
The problem is when i making "ret " value print the lcd screen is blank everything before that i write say if i want to blink some port leds or i want to write :
some message on lcd gets printed(that is what i have told in post #1) if and only if i am writing these lines before this function: unsigned char i2c_rep_start(unsigned char address) . or any other i2c functions.
This problem may sound wierd .. but,.. :
whether my sensor is ok or not?
So far everything seems allright.
You should test your ATmega with another I2C device, or your MPU-6050 with another ATmega or Arduino board.
I think the MPU-6050 costs only 3 dollars on Ebay.
Do you have a multimeter ?
You could measure the shortcut current of SDA and SCL to GND. The shortcut current is the current through the pullup resistors.
You could test if SDA is shortcut to SCL.
You could test the voltage. It should be 5V or 3V and a little lower when you continuesly do something on the I2C bus.
The Arduino Uno is the easiest board to start with, but it is not the cheapest board.
You can attach normal shields to it, and simply stick hard-core wires into the black connectors.
The Arduino Leonardo might be a little cheaper and you can still use standard shields. It is a special board, since it can emulate a mouse or keyboard.
The cheapest board with a usb connector (which is still a 'normal' board) is the Arduino Nano clone on Ebay with CH340. It costs 3.60 dollars. You need the Nano, not the Pro Mini, because the Nano has a usb connector. But you have to solder or clip wires to it, it doesn't have to black rows of connector pin. The CH340 requires a special driver in Windows from wch-ic.com
EDIT: "You can attach" instead of "You can attack".
i observed the voltages & currents at sda & scl they are:
supply to mpu boad=3v
these observations are when i2c communication is initiated.
1)voltage across scl pin of mpu & gnd= 2 . 08v
2)'' '' sda pin & gnd =====2.99v
current though scl pin:....1.08mA
'' '' sda pin:...5ua actually almost zero.
Peter_n:
You could measure the shortcut current of SDA and SCL to GND. The shortcut current is the current through the pullup resistors.
You could test if SDA is shortcut to SCL.
You could test the voltage. It should be 5V or 3V and a little lower when you continuesly do something on the I2C bus.
i observed the voltages & currents at sda & scl they are:
supply to mpu boad=3v
these observations are when i2c communication is initiated.
1)voltage across scl pin of mpu & gnd= 2 . 08v
2)'' '' sda pin & gnd =====2.99v
current though scl pin:....1.08mA
'' '' sda pin:...5ua actually almost zero.
Very strange. There should be no difference. They should have the same pullup value so the current and voltage should be the same.
Perhaps the MPU-6050 is damaged, or something else is wrong with SDA.
Can you measure the same, with the MPU-6050 disconnected ?
There could be a little difference before and after the "Wire.begin()".
After that, the internal pullup resistors are enabled, that is about 50k, for an extra current of 60uA at 3V.
ok, i had to tell this in beginning itself but, ..
i have a atmega 8-8l board interfaced with buzzer, leds, lcd slot made available, etc...etc...
So the buzzer is connected to port c-5 i.e scl line of the atmega8-8l.
So as soon as i connect the scl line of the controller to the sensor's scl line the buzzer turn's on.
Hence, may be due to this voltage between scl line & gnd is around: 2.08v & sda=2.99v.
whereas the current @ scl line is 1 . 08mA & almost nil current through sda line.
may be this is the reason...
what do u have to say?
If the buzzer is inductive (with a coil) the MPU-6050 could be damaged.
It is always handy to have a few I2C devices to be able to test the I2C-bus. For example an I2C EEPROM, or a spare MPU-6050.
sorry.. for contacting so late..
i tried removing the buzzer & then i dumped the same program(above) into my controller board interfaced to mpu6050..
same problem as i mentioned above..
lcd just doesnot display the mpu values.
the code is here:
//
// INITIALIZING, ADDING #INCLUDE ETC..ETC..
//
int main(void)
{
unsigned char ret;
DDRC=0X00;
PORTC=0X00;
lcd_init();
i2c_init(); //initialise i2c communication
while(1)
{
lcd_string("hi");
ret=i2c_start(Dev24C02); // set device address
lcd_showvalue(ret); //if ret=0 then device is accessible, if ret=1 device is not accessible..
_delay_ms(1000);
lcd_clear();
}
}
so observations are: lcd display "hi" but after that nothing happens lcd neither clears though that is my last statement of the code.
what do u think? is there a problem with sensor & should i just get another one or should i get aurdino?..
I'm sorry, I don't know how to help.
Using an ATmega8 is not the problem, I use a few for small projects, some with Arduino and one non-Arduino.
But something could be damaged, there could be something with the voltages or the wires, or ground currents. The library could be wrong or only wrong for the ATmega8, and so on. I really don't have a clue what you can do.
Buy an Arduino Uno and a new MPU-6050 module, and it will work. Once you have that working, you could try to exchange parts and try to make the ATmega8 work with the MPU-6050.
Peter_n:
But something could be damaged, there could be something with the voltages or the wires, or ground currents. The library could be wrong or only wrong for the ATmega8, and so on. I really don't have a clue what you can do.
the library could be wrong? ok.. then, can we test the pleury libraries(which i am using for mpu 6050) on aurdino?
if yes then, do you have aurdino? obviously.. can you please test it on your aurdino? i shall give you link for the libraries
below i have attached the zipped file..
in that twimaster.c & i2cmaster.h are the important files that have to be linked to the compiler..