Stepdir SimpleFOC for STM32, PMSM motor, ABI encorder.?

hello everyone.
I want to build a simple FOC on STM32 to drive a PMSM motor (servo with ABI encoder 2000ppr). because I will control using step/dir. and coincidentally simple foc supports, previously I had tried with another foc it was successful only there was an error (and the person who made it was no longer active) now I tried on simple foc.

For now I have all the hardware. just change the pin used. I will use 6pwm to control the mosfet (igbt chip) is it permissible to use 1 shunt low side resistor with a value of 0.22ohm??

for a 2000ppr encoder is it safe to use quadature on stm32f103c8t6???
for now i will use 40v only for motor control.

And to upload code from Arduino, any idea whether to use Stlink or via USB (by installing the Stm32duino bootloader)?

And does Stm32 support the Simplefoc utility with Anaconda Python?? Because I tried to install it, I got a problem like the one below.
@runger



You need at lease 2 or 3 low side shunt resistors. The value used has to match the expected amps, the watt rating of the resistor and the gain of the current sense amplifier.

It will be a high interrupt load if you use the normal driver. If the F103 has an encoder mode in its timers, and you use the right pins, then you could use the hardware encoder driver from our drivers repository. This would work, even at higher RPMs.

You can use either one. If you have an ST-Link, you don’t have to bother with the bootloader and gain a few KB extra flash space :slight_smile:

Yes, I think you need a newer version of python. unfortunately I can’t help well with this, I don’t use anaconda.
@nanoparticle has recently re-packaged the SimpleFOCStudio: Releases · nekosystems/SimpleFOCStudio · GitHub
Perhaps this will work better for you?

Owh oke sir, so this mean it need the apli sir?? No chance without ampli? Only shunt resistor and some resistor sir?? Like this, from the lowside, with resistor 0.22ohm to gnd. Betwenlowside to resitor(0.22ohm) i connect with 47kohm resistor and come to adc pin stm32.???
Amd what is the best resistor sir??
I have shunt 0M50(2W), R005(2W), (R0.22, 0.47,0.5ohm(5W)) wichone is the best??

I get this recomendation pin for stm32f103 from st enginering.
++++++++++++++++++++++++

  • STM32F103CxTn PINOUT +
    ++++++++++++++++++++++++

FUNC NEW_PINOUT

ENABLE ADC12_IN6/PA6 (tim1 break pin, max 3.3V!!!)
STEP SPI1_MOSI/PB5 (5v tollerant)
DIR SPI1_MISO/PB4 (5v tollerant)
ENCODER_A SDA/PB7 (5v tollerant)
ENCODER_B SCL/PB6 (5v tollerant)
ENCODER_Z BOOT1/PB2 (Optional, 5v tollerant)
B_EMF ADC12_IN1/PA1 (Optional, need C_EMF)
C_EMF ADC12_IN0/PA0 (Optional, need B_EMF)
DC_LINK ADC12_IN2/PA2 (Optional for stabilization)
B_SENSE ADC12_IN3/PA3 (Single current sense)
C_SENSE ADC12_IN4/PA4 (Optional for FOC, need B_SENSE)
TIM1_CH1 TIM1_CH1/PA8
TIM1_CH1N TIM1_CH1N/PA7 (Better choice than PB13 due to minimal delay)
TIM1_CH2 TIM1_CH2/PA9
TIM1_CH2N TIM1_CH2N/PB0 (Better choice than PB14 due to minimal delay)
TIM1_CH3 TIM1_CH3/PA10
TIM1_CH3N TIM1_CH3N/PB1 (Better choice than PB15 due to minimal delay)
STATUS_LED JTDO/PB3 (PC13 should not be used for LED)
ERROR_LED JTDI/PA15 (PC14 should not be used for LED)
USB_DM USB_DM/PA11 (Obvious)
USB_DP USB_DP/PA12 (Obvios)
SW_DIO SWDIO/PA13 (USE SWD WHERE POSSIBLE)
SW_CLK SWCLK/PA14 (USE SWD WHERE POSSIBLE)

Owhh okee i will used stlink sir, i will try today with encorder first. Thank you.

Thank you sir​:pray::pray::pray:

Nope, sorry! We support current sensing with a minimum of 2 shunts, and especially for low side sensing, 3 shunts are recommended. One for each phase.

And the current measurement is a differential measurement across the shunt resistor. So you need 2 ADC channels to measure one phase, and 6 ADC channels (if used in a differential configuration) to measure all the phases.
Perhaps you could tie the GND-sides together and get by with 4 ADC channels.

You would have to centre the ADC signals using appropriate voltage dividers, and then, just in theory, you may be ok for low side sensing directly with the MCU. In practice, especially if you’re using 40V and IGBTs I think your common mode voltage will be outside the MCU’s range, and the voltage spikes that might be seen on these wires could damage your ADC or the whole MCU.

So I would strongly recommend to use some robust current sense amplifiers, to ensure the output is always within the range of what the MCU likes, and to be able to use a single ADC channel per phase.

Do u have recomenden apmlifiers sir?
It acs 712 recomend sir?? Because now iam in indonesia so hard to buy somthing from aliexpres ornother shop, only can buy inside in indonesia.

Yes, if the ACS712 is suitable for the currents you expect to measure, then using a Hall effect current sensor like this one is a bit easier than designing a shunt-based current sensing.

If you’re going to use hall-effect sensors, consider using inline current sensing instead of low side. It can be easier to use than low side sensing.

1 Like

Okee sir, but for the supplay i see its mean 5v, and adc stm32 is 3.3v. If i put resistor posible? Wich value is the best??.
And do u recomended 5,10,20 or30A sir? For now i have 1 acs712 30A

You can use a resistor divider, or a op-amp buffer to convert the analog voltage down to the correct range.

This depends on your expected currents. Use the lowest that you can, it will get you the best accuracy. But if you are expecting currents above 20A, then you need the 30A model.

Okee sir thank you, letter i will update again, and for encorder 2000ppr do u recomend tu on the quadature???

Hi sir, i see your library are suport for esp32 also.
I want to build step/dir for pmsm motor, do u recomend stm32 bluepill or esp32(ex NodeMCU) sirr.
Because i see with 240MHZ, and blue pill are 72MHZ sir. Or with stm32f407vet6( this is i have now, but i dont see this suport with your library)
For use 2000ppr encoder, and inline U, Vcurent sensing with acs712 sir.

Most BluePill boards have 64kB flash memory, that is not enough. But if you get one with 128kB, then it can work.

Of course F407 is much faster and better for SimpleFOC, STM32F4 series is well supported. Again, make sure you get one with 128kB flash or more.

ESP32 is also well supported, and would also be a fine choice.

If you want to buy a new one, I’d get the ESP32. Otherwise, use the F407 you already have. I’d avoid the BluePill, as it is less powerful than the other two MCUs.

1 Like

But i see esp32 lower cpu speed than stm32 right?? It will make problem? Like a reading encoder or something???.
Yess, but stm32f407 are expensive if i get new one (if working i want make more than one). I see stm32f401ccu6 with 64ram, 256rom and 84Mhz.
Did u recomend esp32 or stm32f401 sir??
Because the price almost same in my country sir.

No, usually ESP32 is 240Mhz, and only the very fast expensive STM32s run at this kind of clock rate (or faster).
Anyways, MHz is not everything. The ESP32 also has 2 cores and a RISC V architecture, it’s a very fast chip.

STM32F401 is also fine. It’s also a very fast chip. Just make sure you get one with 128kB flash or more.

A consideration is that for STM32 you will need an ST-Link programmer to use it best. For ESP32, if you take the ESP32 then normally all you need is a USB cable.

1 Like

Owhh okee sir. How about the pin?? Esp32 have ADC pin? For inline current phasa??.

Yes, ESP32 has ADCs, but the STM32’s ADC is much better.

But really, either one will work well with SimpleFOC.

If you have any other questions about these MCUs, I’d suggest you read their documentation, or consult some other resources online - there are posts and videos specifically dedicated to comparing the benefits of different MCU boards…

1 Like

Alright sir, thank u letter when i started i will post my progres​:pray: one more thank you for your info, and amazing library, i hope step dir will work nicely​:pray::pray:

Hi sir, for the step/dir it that open loop right sir??
How about if we make it closed loop? Because my pmsm motor have encorder with 2000ppr, it will be better than open loop right? I see on example it is open loo no pin for encorder. It posible to implement? And it will beter sir?? Do u have idea??