Richard-Gemmell / teensy4_i2c

An I2C library for the Teensy 4. Provides slave and master mode.
MIT License
92 stars 19 forks source link

two mpu6050 each on a separate i2c bus #32

Closed tiralonghipol closed 1 year ago

tiralonghipol commented 1 year ago

hi, thank you for this library! what would be the wiring required to achieve a parallel communication between x2 i2c bus - mpu6050? using the raw_find_slaves I can only detect address 104 (0x68) but the probelm is that afaik the two mpus have the same address any idea? thanks!

Richard-Gemmell commented 1 year ago

Hi, The MPU6050 can be configured to use one of 2 different addresses. This is described in section 9.2 I2C Interface of the datasheet.

I'm guessing that you're using a breakout board with an MPU6050 on it rather than building your own board. The manufacturer of the board may have given you a way to configure it. On this Adafruit board for example, it's controlled with the AD0 pin. It's possible that your particular board doesn't have this option.

The idea is that you leave one board as it is and change the other to use the alternate address which is 105. You can then read both of them with the same I2C port.

On an unrelated note, I suggest you checkout the INT pin if haven't already. You can get dodgy data from an IMU if you read it at the wrong moment!

Hope that helps, Richard

tiralonghipol commented 1 year ago

thank you for your fast reply! what I am trying to achieve, is actually parallel communication, this way: mpu1 -> bus1 mpu2 -> bus2

I already achieved the communication bus1 -> mpu1/mpu2 using the ad0 pin switch trick hope is more clear now

Richard-Gemmell commented 1 year ago

Ah I see.

This line of raw_find_slaves determines which I2C bus is being driven by the master. Change it to Master1 to use the second bus. (There's also a Master2 for the 3rd bus.) e.g. I2CMaster& master = Master1;

cheers, Richard

tiralonghipol commented 1 year ago

thank you, I will give it a try and let you know!

Richard-Gemmell commented 1 year ago

FWIW note that read_async starts a read by a master. You can start a read on one bus and then immediately start a read on the other one. They'll both run in parallel in the background.

master.finish tells you if a read (or write) has finished.

tiralonghipol commented 1 year ago

that's exactly what I need, thank you again

tiralonghipol commented 1 year ago

@Richard-Gemmell you are a genius! as you mentioned in your guide, this was the only change required to my code:

// #include <Wire.h>
#include <i2c_driver.h>
#include <i2c_driver_wire.h>

on a Teensy 4.0 (600Mhz) I am now able to read both mpu6050 at 1kHz! without changing any line of code

In case someone else ends up dealing with the same problem, here is the code (i'm not a software developer, so apologize in advance if I write bad code!)

Richard-Gemmell commented 1 year ago

Cool. Thanks for letting me know that it works and thanks for posting your code.

I'll close the issue now. Don't hesitate to re-open it if you need.

cheers, Richard