Interface ATMEGA 32 with IMU 9 v2

Hi guys,

i looked through this forum a lot and also looked up all the internet to get some advise for my problem. In case anyone finds the proper article, then i apologize that i opened a new thread.

So, I came across to program my own ATMEGA 32 off the scratch some time ago. I really like the arduino, play around with it a lot but very quickly reach its limits, especially when it comes to IDE.

Alright, my problem is know, that I do not succeed in communicating with the IMU (pololu.com/catalog/product/1268) by my I2C interface.
To ensure that the IMU is working I attached it to the arduino and saw the data coming properly, so I am pretty sure the IMU is able to work.

Now I use the I2C library that is widely spread from pete fleury (see attachment). Together with this library, I am trying to talk to the IMU (gyro in first place).

My sequence for programming looks like this (not a code extract):
i2c_init()
i2c_start(LGD320_WRITE) //means to set ID and write Bit
i2c_write(LGD320_CTRL_REG) // to turn on the IMU i need to set the ctrl register first
i2c_write(0x08) //setting the PD bit to one
i2c_stop()

//now i want to read the Who am i register
i2c_start(LGD320_WRITE)
i2c_write(LGD320_WHO_AM_I)
i2c_repstart(LGD320_READ) //as now I want to read data (see datasheet for sequence)
data = i2c_receive_byte() //that should give me the data?
i2c_nak() (to stop the i2c communication)

I would like to know if you think that at least this sequencing is correct? the arduino example doesnt give too much insight…

Then unfortunately immediately as I start the program it hangs at the start condition (i2c_start(Address) ) and the TWI Status Register is not getting an acknowledge:
if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) )
this is actually not succeeding…

Hardware wise I introduced the Pull Up Resistors properly, wires are properly connected, so I guess I am fine.

Has anyone already tried this with an ATMEGA32 in C only?
Any good suggestion what might be the issue.

I use an external crystal with 12MHz, SCL_Clock is set to 100kHz. Any limitation that I oversaw?

Thanks for your help in advance.
i2cmaster.zip (23 KB)

Hello.

I am sorry you are having problems communicating with your MinIMU-9. It looks like you are forgetting to enable the axes when setting CTRL_REG1. However, it seems like you have a lower-level problem that is causing your I2C communication to not work at all. You mentioned you added a pull-up resistor. Where did you add this? The MinIMU-9 v2 has pull-up resistors on the SCL and SDA lines already.

Could you post the full code of your program? If it is very long, please try to simplify it to something minimal that still shows the problem. Also, could you post a photo of your entire setup, including all the connections?

- Jeremy

Hi Jeremy,

thanks for the answer. I removed the extra pull up resistors again (+5VDC to SDA / SCL lines) and when measuring the resistance now, I saw 4,4kOhms of resistance to 5V supply. So the setup should be fine.
I voltmetered the connections all the connections again, the data and clock lines are connected properly to the controller, so far so good.

I played around with the communication again, I gave some outputs to get the reading of the TWSR (Status Register of the Controller) and it gave me always a “address transmitted, write mode not acknowledged”. So this means I do not get an answer of the IMU.
My address is set to: 0xD3 (READ) / 0xD2 (WRITE)

I have some general questions then:

  • As you stated, the IMU is off by default. How could it then send an “ACK” if I try to write into its register to activate it (writing into the Control Reg 1: 0x0F)?
  • Then I was insecure about the I2C speed: I set this in the registers for my controller (100kHz, 50kHz i tried, no change of my problem), the IMU is only reacting on my input and “knows” which speed to send with?
  • Even further basic: I have the module imu01b / 0J3865 / cpy 2012, and now dont know if its V2 or the older version. anyways from register / address settings it wont make a difference.

As I said, the unit is working with the Arduino, as per your code example. Which I2C speed is it using? (100kHz?)
As the Arduino is using a Atmega 328 I will go ahead and check if I can find some routines that might help me. One general thing seems to be to release the bus properly, send stop bits properly, etc.
If you have any hint, let me know.

Thanks

In my previous post, I asked if you could provide your code and a photo of your project. The full code and a photo will be really helpful for troubleshooting. Could you please provide them both?

Only the outputs are off by default, the chips will turn on when powered.

The I2C speed is going to be determined entirely by the Arduino. By default, it should be 100 kHz.

You have the MinIMU-9 v2 so you can refer to the current documentation for the address settings.

- Jeremy

Hi Jeremy,

sorry for the delay, i didnt find too much time lately.

But fortunately I found some time in the last two days and even more useful, an oscilloscope. I wrote some routines so I could command the mega to send data when I print some letters to it.

After a lot of time investigating, then going from the Gyro to the Magnetometer and Accelerometer, I found that I could talk to those ICs with the i2c interface. And finally I am now able to talk to the gyro.

Here are my findings:

  • the gyro addresses were different from the datasheet and the website:
    GyroReadaddress: 0xD6
    GyroWriteaddress: 0xD7

  • as we already saw, the gyro Ctrl Reg 1 needs to be set to normal power mode (write it with 0x0F)

  • my read routine for the I2C library needed some improvement.

I will post my libraries for the interface as soon as they are in a nice shape.
I will go on and try to get all the registry data properly, the address increment and so on, then I might come back with some more questions.

Thanks for your help…

Hello everyone,

please find attached my current code for the I2C interface of the ATMega32 and the interface to the IMU 9 v2 from Pololu.

I will have to work on the sequencing for the multiple register read, hopefully I will come back with it soon.

The port.c / port.h file has the I2c communication routines. in the uart.c the routines for reading and writing data are mentioned.

Hope this helps you guys in case someone is stuck talking to the IMU.

Most important information for me: The Gyro address was not as expected. I had to try several addresses until I got an acknowledge.

Greets
port.c (3.73 KB)
uart.c (3.84 KB)
port.h (481 Bytes)

I am glad to hear you got it working. Nice catch on the datasheet. It listed the binary addresses correctly (11010111, 11010110) but the hex conversions were incorrect (D3h, D2h). There is a new revision of the datasheet that fixes that error, and we are updating the version on our website. Thanks for letting us know.

- Jeremy

Hi Gentlemen,

I just wanted to leave a short notice, maybe that could be picked up into the “Short Protocol Hints” for the mini IMU:

For activating the Magnetometer, it is essential to write the MR_REG at least once with 0x00. Otherwise the data will not update.

This is not of too much info for the Arduino Sample code users, as it is done in the code that is provided by Pololu, but maybe one or the other comes across writing the code himself, then the data is not getting updated.

What is also interesting:
For the Gyro and the accelerometer the auto increment needs to be activated by setting a bit. The Magnetometer automatically auto increments its register addresses.

Okay, guys, in this case no questions, just some comments and maybe one or the other comment is useful for the website as well.

Greets from Germany.

Thanks for sharing. Most of the stuff you mentioned is covered under “Protocol Hints” section on the product page.

“The gyro, accelerometer, and magnetometer are all off by default. You have to turn them on by setting the correct configuration registers.”

“You can read or write multiple gyro or accelerometer registers in a single I²C command by asserting the most significant bit of the register address to enable address auto-increment.”

“The magnetometer will not update its data until all 6 data bytes have been read during a single I²C transfer. All the bytes can be read in the same transfer using the magnetometer’s automatic sub-address updating feature (this feature is enabled by default).”

- Jeremy