How to wire two magnetic sensors to a stm32f446 nucleo board

Hi there,
First post on the forum and with simpleFOC in general.
I am in the process of building the simpleFOC balancer, as a way to learning motor control starting from a worked example

I have bought the same motors used in the simpleFOC YouTube video, but I picked the ones with integrated 5048A magnetic sensors (SPI) instead than using two encoders.

I am using two simpleFOC mini shields as motor drivers.

My question is about how best to connect the two magnetic sensors.

Should I connect them to the same SPI peripheral on the stm32 nucleo board (obviously using two separate CS pins), or should I use completely separate SPI peripherals?

I don’t know the simpleFOC library at all (yet).



Welcome to the forum.

I don’t think the library supports multiple SPI sensors on the same bus. You need to connect them to two separate busses.

@runger may provide more feedback perhaps.


Hey @rmeldo, welcome to SimpleFOC!

We do in fact support either configuration! You can use 2 (or more) SPI sensors on the same bus, with separate nCS pins. They can even be of different types / with different clock speeds and SPI modes. Or you can use separate SPI buses.

There is not really an advantage or disadvantage other than the number of MCU pins used. This is because Arduino framework does not support asynchronous reading of the SPI, so the sensors are read sequentially even if they are not on the same bus. It’s only on a multi-core MCU that you could theoretically benefit from 2 SPI buses.

1 Like

Great, thanks.
I’ll use the same bus then.

I’m working with something like this. Not Simple FOC but using two cores. It is REALLY hard to do and so far it’s not working. You think "I have two UARTS and two cores, I will let each core have its own UART (or SPI). The problem is the library code is not thread safe. Using one SPI per core works only if you write your own thread safe drivers and remember to lock any register that is shared.

I find it is easy if I redesign so that all the serial communications is done on one core and the other stuff on the other core. Push the data between threads using queues and locks

Using two cores does give true multitasking, and with it all the problems of true multitasking. You are best off if you design such that the two cores do not use the same libraries. Breaking this rule means you have nearly impossible to debug random glitches. As I said, REALLY hard to get right.

I absolutely agree that this is an advanced topic. And also that Arduino framework offers no support for this at all, so everything is coded in a way that assumes a single threaded model. Even just interrupts on a single MCU often cause problems.

So when approaching a project I think you have to seriously ask yourself whether Arduino framework and its libraries are the right choice for multi-threaded code.

But other frameworks offer a lot more support for this, and with an understanding of concurrent programming and above all a clear picture of your software architecture I think it is quite possible.

I have to add that my background is in computer science rather than electronics or engineering, so it’s also something I feel comfortable with. Motors on the other hand are still a bit mysterious to me :smiley: