I was struggling to communicate with my MPU6050 sensor through I2C. It turns out the solution was to add a small delay after setting some of the initial registers:
Now I am wondering, what exactly is the delay needed for? Through some trial and error, I discovered that it consistently needs the same, specific amount of time between starting up and reading in the first values properly.
Thanks for any help you can provide for this question.
(1) There are following sections in an Arduino Sketch:
Global Space
Setup Function Space
Loop Function Space
Users Function Space
(2) Please, put your codes at the appropriate sections of the following 'Blank Arduino Sketch'. This will help us to study your codes and to think over your question.
//---------Global Space-----------
//---end of Global Space
void setup()
{
}
void loop()
{
}
//-----Users Function Space
//----end of user Function Space
(3) Do you agree with the following arrangement of your codes? If not, please do your arrangement to comply with my above request.
BillMahooney:
It turns out the solution was to add a small delay after setting some of the initial registers:
delay(1000);
I can't answer your question about why a delay() is needed, but I'd hardly think a 1000ms is "small". In computer terms that's for ever. (I'm pretty sure I saw a fix somewhere just using delay(5).)
If I were you I'd experiment with smaller values and establish the smallest that works reliably.
Now I am wondering, what exactly is the delay needed for? Through some trial and error, I discovered that it consistently needs the same, specific amount of time between starting up and reading in the first values properly.
Your subject is misleading. If this delay is really needed, your MPU is needing it, not I2C as the interface between them. A lot of sensors need some time to startup and you have to wait before you can read actual values from them.