ldab / KXTJ3-1057

KXTJ3-1057 Tri-axis Digital Accelerometer - Arduino Library
MIT License
14 stars 9 forks source link

"Failed to Initialize IMU" #15

Open anthonys225 opened 1 year ago

anthonys225 commented 1 year ago

I am having issues with the IMU not passing the setup. I isolated the issue to this portion. as shown below. I created the error IMU_HW_ERROR6 as this is where my code stops. I'm not sure what portion of I2C is failing. Does anyone have any tips?

image

nomakewan commented 1 year ago

If you got to that part of the code, it means the IMU never responded to any communication. The only way to get there is if the very first attempt to write fails, and then the attempt to write to the bit-flipped address also fails. This does not appear to be a code issue, but rather a communication issue.

First, note that this library uses the default Wire implementation. If your specific chip uses multiple I2C ports, you will need to modify the code to specify that (for example, the AVR DB allows swapping I2C ports).

Second, note that the KXJ3-1057 is a maximum 3.3V IMU. If you're feeding it 5V, it is out of spec. Similarly if your I2C ports are expecting 5V logic, you will be out of spec. Make sure everything is 1.8V~3.3V.

Third, check the wiring between your Arduino and the sensor. Make sure not only that you have the wires connected to the correct pins on both the IMU and Arduino side, but also that you have the proper pull-up resistors on the I2C lines (Kionix uses 2.1k resistors on their demo board; that value is generally fine, as would be 4.7k).

anthonys225 commented 1 year ago

If you got to that part of the code, it means the IMU never responded to any communication. The only way to get there is if the very first attempt to write fails, and then the attempt to write to the bit-flipped address also fails. This does not appear to be a code issue, but rather a communication issue.

First, note that this library uses the default Wire implementation. If your specific chip uses multiple I2C ports, you will need to modify the code to specify that (for example, the AVR DB allows swapping I2C ports).

Second, note that the KXJ3-1057 is a maximum 3.3V IMU. If you're feeding it 5V, it is out of spec. Similarly if your I2C ports are expecting 5V logic, you will be out of spec. Make sure everything is 1.8V~3.3V.

Third, check the wiring between your Arduino and the sensor. Make sure not only that you have the wires connected to the correct pins on both the IMU and Arduino side, but also that you have the proper pull-up resistors on the I2C lines (Kionix uses 2.1k resistors on their demo board; that value is generally fine, as would be 4.7k).

Hey Nomakewan,

  1. I understand that and I am using a RP 2040 MCU. I am currently using SDA GPIO4 (Pin 6) and SCL GPIO5 (Pin 7). I am unsure on how to swap the pin allocation for I2C SDA AND SCL to the appropriate pins. I looked up a video and thought I was able to change it by changing the pins_arduino.h file within "C:\Users----\AppData\Local\Arduino15\packages\arduino\hardware\mbed_nano\4.0.4\variants\NANO_RP2040_CONNECT as shown below. image

If this is not the correct way to do it with arduino, would you happen to know the correct way via IDE?

  1. 3.3v is the supply voltage being used.

  2. I am using 4.7K pullups as well.

Thank you!

2.

nomakewan commented 1 year ago

Are you actually using the Nano RP2040 Connect? Or are you using a custom board featuring the RP2040 processor by itself?

I ask because the Nano RP2040 Connect has its own onboard IMU with its own library which is already hardwired to its I2C bus: https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-imu-basics

I would never alter the pins_arduino.h file unless I was absolutely positively certain that whoever authored that library got something wrong.

anthonys225 commented 1 year ago

rp2040-datasheet.pdf

I am using a custom board featuring the RP2040. It is capable of remapping its I2C pins, but the documentation is weird for me personally. I have used PIC processors in the past, which had a more straight forward structure for remapping pins.

Is there a way I can revert the pins_arduino.h file I guess? It was the only way I could visually see to remap the I2C pins. Let me know if you need additional information.

nomakewan commented 1 year ago

Okay, so then that is (likely) your issue. You are using a library for a board that your board is not. That library was designed specifically for the Nano RP2040 Connect, which is configured very differently from a raw RP2040 chip. So my guess here is that that library is expecting an environment that doesn't match your actual environment.

If you don't know what the previous values were, you can always uninstall and reinstall the board from the Boards Manager in the IDE. That will give you a fresh copy. That said, I don't think it will actually help since you aren't actually using a Nano RP2040 Connect.

EDIT: Here is the default file for the Nano RP2040 Connect: https://github.com/arduino/ArduinoCore-mbed/blob/main/variants/NANO_RP2040_CONNECT/pins_arduino.h

I did look at the datasheet for the raw chip and it appears that I2C is supported on any of the 20 GPIO pins. Whereas the Nano RP2040 Connect is configured to only use I2C on A4/A5 (D18/D19).

My thought is that you'll have to do some direct register addressing of your MCU to configure its GPIO registers to set the I2C pins to the same ones as the Nano RP2040 Connect if you want to have any hope whatsoever of making that library work.

anthonys225 commented 1 year ago

I understand exactly what you're saying. Though I'd imagine some of the configurations should be identical no? The code compiles and uploads onto the raw RP2040 chip. It seems to me the mapping of the I2C pins is what is stopping the code.

Or are you saying there are a lot of other settings and configurations within the libraries that are causing issues with the code to run on the custom board? There should be a way to remap the pin allocation and have the same code work no?

nomakewan commented 1 year ago

The library you are using expects an RP2040 that already has a firmware flashed to it (bootloader?) that's designed to operate as the "Nano RP2040 Connect." This includes already-set registers to configure the I2C pins, SPI pins, etc etc etc etc. Since the library does not expose any user-configurable settings that means you cannot use the library itself to swap pins around. You would have to do direct register manipulation (or write your own interface library).

For giggles you could swap your wires over to D18/D19 and see if that works. But there's no way that you will be able to get I2C up on pins D4/D5 if you're using the Nano RP2040 Connect library.

anthonys225 commented 1 year ago

I understand so the code will have to essentially be written from scratch? Also on the board manager I don't have a choice to choose RP2040 non Connect board. All that is available is RP2040 Connect.

This is very hard since they're hard wired on a PCB as of now. So theres absolutely no way to remap the pins using these libraries?

nomakewan commented 1 year ago

Correct on all counts.