I2C magnetic sensor - multiple I2C busses support

I’m also pretty attached to our esp32 but I am also trying some foc on the Blackpill. For me it would be a cheap solution as it has up to 3 seperate i2c lines (pins)which means you could control 3 of the as5600s at 400khz. Only reason I have not used it is that Arduino environment doesn’t support 3 seperate i2c lines.

Hey @Adam_Donovan,

is this inherent problem of the Arduino IDE or is it the issue of the library?

I found this online:

We could maybe support this feature as well since most of the remaining configuration parameters have been nicely implemented by @Owen_Williams recently.

Even ESP32 seems to be able to have more than one I2C busses:

Yes - it’s probably a limitation in the library. In dev branch - you can swap over to another bus (by changing sda, scl pins) but I don’t believe you can both buses at the same time as it’s using Wire directly. Adding this would be a sensible enhancement.

The SPI situation is a little bit worse as you can’t even switch over to SPI2 - e.g. @blackblue007 had to edit MagneticSensorSPI.cpp to use an alternative SPI bus. This would be another enhancement.

If anyone wants a crack at fixing these issues - they’d make a great first Pull Request.

Its the wire library, I did some investigating to try and get around this limitatiom while considering the blackpill aka a stm32f11 which has 3 i2cs. Even thought Todo bit banging but I think we of course run into speed problems.

I also have a 3 wheel foc prototype robot running with an i2c multiplexer but of course it runs slower as the i2c max speed of the TCA9548 multiplexer is 400khz and divide/3 robot wheels is more like 133khz…it does however work really well just too slow for something dynamic.

Scale and cost right, the as5600 is cheap and if only we could pair it with say a stm32 that’s fast low power with just enough pins for the i2c. 3 phases and SPI then make it all on one board. Better yet if it was a standard part from jlcpcb.

To be viable it would have to cost less than one of the other mag encoders that has SPI.
Apart from this I think The stm32f11 is a pretty good choice for something low cost and dedicated to run 3 motors and also able to communicate with the esp32.

FYI ——> https://github.com/miwagner/ESP32-Arduino-CAN

@Adam_Donovan - I’ve just done a test - it is possible at least on the ESP32. I’ve just hacked the MagneticSensorI2C.cpp and h files and got two AS5600 working on two buses.

It’s in no fit state to merge, but you can see what changes I made here:

And my main file looks a bit like this:


MagneticSensorI2C sensor1 = MagneticSensorI2C(AS5600_I2C);
MagneticSensorI2C sensor2 = MagneticSensorI2C(AS5600_I2C);

void setup() {


// use monitoring with serial 
  Serial.begin(115200);
  // comment out if not needed
  motor.useMonitoring(Serial);
  delay(750);

  sensor1.scl_pin = 23;
  sensor1.sda_pin = 19;
  sensor1.i2c_bus = 1;
  // initialise magnetic sensor1 hardware
  sensor1.init();
  sensor2.init();

I’d need to give it some more thought before trying to put a PR together and I don’t have STM32 with two i2C buses to test. In the above code sensor2 is using the default bus (0) with default SDA SCL pins and I’ve overriden sensor1 to use bus (1) and the pins for second bus.

1 Like

@Owen_Williams Nice I will try it out for sure:) BTW I love your FOC tones, I was thinking of doing something similar for our swarm a while back…can you imagine each robot has 3 motors and we have 50 robots running…could end up making quite a complex audio scape just with motors. I wan to ty it out next week:)

Swarms, wait what? That sounds awesome! You have 50x 3 wheel robots?

Pictures please!

@Owen_Williams https://hochschuh-donovan.com/portfolio/empathy-swarm/

3 Likes

You are doing some great work! I haven’t thought about robotics from an art and human interaction perspective. Really inspiring!

1 Like

It is a sort of copper craftmanship

@Owen_Williams What would be next steps to get it to work on the stm32f11? My partner Katrin could try and implement something if I have a clue:)

Support for dual i2c buses might land in the next version (1.7), if katrin is super keen she could look at the pull request below but there is a fair chance that it will change (it hasn’t even made it on to dev branch yet):

There is a new ‘dual bus’ example in the PR.

I did my testing on esp32 and disco_stm32407g board and for my stm32 board i had to also patch the core because the Wire pinmaps for second i2c bus weren’t setup properly. I wouldn’t be surprised if you faced a similar issue with you stm32f11 core. My core also went into an infinite loop when i tried to change clockSpeed (looked like the core didn’t have HAL implementation for disabling interrupts)

I’ve had other issues with as5600 sensor. I’m occasionally seeing it locking up the bus when mcu is reset (toggling scl seems to release it, as does power off/on) and both version (green circular or white square) are hard to solder. The ma730 and as5047 cost 3 times as much but are better imho.

@Owen_Williams I agree that the ma730 and as5047d are better sensors. The problem I have with trying to make this work for our needs is not having enough pins on the MCU Todo spi.

3 separate i2c is 6 pins and 3 spis sharing sclk, miso, mosi is also 6 pins. Granted they might not be available.

Did you know there is a new as5600l which you can program to use a different chip address? I.e. you could have 3 as5600l’s on the same bus?

Sorry Owen I think I missed that as I thought somehow you see referring to the 5200 which has 2 possible i2c addresses. If that’s the case I will re tests the speeds. I wonder though if the problems you had with hanging on reset were with the new 5600l or the older? I still think that having a dedicated MCU thats fast enough and just the right amount of pins might be the way to go in terms of chaining many motors.

The hanging was on the older as5600 but i suspect it is a combination of stm32 and the sensor, I didn’t notice it in esp32. I’ve written a checkBus() function on a recent pull request that seems to fix the issue (it basically sends 16 clocks to clear the sensor).

I agree that whatever seems the most straightforward for your design is the best option!

You know @Owen_Williams and @Antun_Skuric how about using a super cheap MCU like the STM32F030F4P6 which we can get for 1.40$ a board and then use one per motor with SPI. I’d couple it with the ma730 and try fit it all on one board. STM uses a stm32 f0 for their robot eval 01 (packaged inside the STSPIN32F0A). This way projects needing more motors Can managed by whatever flavour MCU we might prefer.

Depending on if we need 2 SPI then perhaps the STM32F071C8