B-G431B-ESC1 With Hoverboard Motor


I have a B-G431B-ESC1 and AS5047p encoder with a 350w hoverboard motor. I need get some inputs to ESC from external arduino and As5047p. These are,

ESC A+ / PB6 → A Phase from Encoder (INPUT / Default)
ESC B+ / PB7 → B Phase from Encoder (INPUT / Default)
ESC Z+ / PB8 → Reserved for triggering external mosfet for braking resistor (OUTPUT / NOT DEFAULT)

ESC PWM / PA15 → PWM Input from arduino for setting required Torque value (INPUT / NOT DEFAULT)

I Think, these are ok, if not please warn me.

Now I need one more IO pin for sending direction signal from Arduino to ESC. It is Low / High only.
As I can see from pinout scheme, I have available some pins which are used for different purposes.


If there is more IO pins (solderable) exist, please warn me again.
I prefer to use CAN_RX or CAN_TX pins, not USART2 pins, because I may need them later / programing.

So the question is, can I use these free IO pins as a regular IO pins on a MCU , or special configuration is need ?

Also using

– ESC Z+ / PB8 pin as OUTPUN pin for triggering a mosfet
– ESC PWM / PA15 as PWM input for torque values from Arduino

any config changes or other things needed for ABOVE pins ?

I am using arduino ide + stm32duino pack and Simplefoc for Closed Loop Torque control.

Any help is appreciated.

Thank you.

Hey, I think your plan should work…

You should be able to use the STM32 hardware-based encoder from our drivers repository:

#include "SimpleFOCDrivers.h"
#include "encoders/stm32hwencoder/STM32HWEncoder.h"


STM32HWEncoder encoder = new STM32HWEncoder(ppr, PB6, PB7, PB8);

According to the schematics I can find for the B-G431-ESC1, and also its user-manual, the CAN_RX and CAN_TX are connected to a CAN transceiver, so I assume the solder pads are for CANH/CANL - not suitable for your purpose.
So I’d say using PB9/PA11 is a no-go, except for CAN.

The PWM on PA15 will work, but we don’t have a driver for this. You’ll have to write your own input routines, being careful not to busy-wait… ideally using the MCU’s timers to do it with little or no overhead.

Why not use this input for both speed and direction - just make 0 the middle point, and have a speed signal that can be negative or positive? Then you only need one input for speed and direction…

Thanks for quick reply.

  1. I can not find STM32HWEncoder class now. Can STM32HWEncoder class can take only 2 parameters ? I not using ESC Z+ / PB8

  2. ESC Z+ / PB8 pin as OUTPUT pin for triggering a mosfet. Is this require any config changes / pin mapping or just pinmode (Output) is ok?

  3. Can you clarify that how can I carry torque value and direction info in single pwm channel. I will write handling code async way by timers as you said.

Thanks you.

Hi @etempm ,

  1. You can find it in our drivers repository:
    Arduino-FOC-drivers/src/encoders/stm32hwencoder at dev · simplefoc/Arduino-FOC-drivers · GitHub
    At the moment it assumes you want to use Z+, but the code could be changed. AFAIK the STM32 encoder can also work with no I (Z) pin.

  2. Hmmm… from the point of view of the MCU, its just a matter of pinMode(output). But there are two issues:

  • First, you have to check if all the filtering components attached to Z+ are a problem for you (see schematic below) in output mode.
  • Second, the MCU can only drive logic level FETs. So if you really want a FET for braking energy, you will need a driver for it, and the MCU can control the driver. But it’s only 3.3V and 20mA, so it can’t directly drive a power FET.
  1. Well, if your PWM is 0-1024, then you can read the PWM value from your driver, and compute the direction/torque something like this:
int signed_pwm = pwm_value - 512
int torque = abs(signed_pwm)
if (signed_pwm>=0)
direction = 1; // forward
 direction = -1; // backward

Input schematic for A+/B+/Z+ on B-G431-ESC-1:

Firstly, thanks for valuable information.

I decided to try use Braking resistor with out mosfet. I am trying to design TVS diode based automatic braking circuit. So +Z will be free to use with encoder.

For torque and direction information, contacted with @Grizzly and he helped me about his CAN Bus implementation on B-G431B. ( Thank you @Grizzly )

So if possible, I will send torque, direction and more info to driver with Can Bus. I will try.

Do you think any problem with can bus comm. I mean speed, latency etc. ?

Thank you again.


For most applications, I think it will work very well! The latency will maybe be higher than with a PWM-based protocol, but as we know it is quite possible to control robots etc via BlueTooth or WiFi, so it will also work via CAN.

Let us know how it goes, and good luck with your build!

I found this mosfet which can be drivable by 3.3 here. So I need to check voltage is above 12v to trigger mosfet. How can I read voltage for activating braking circuit ? Sorry for my to much questions, I am new to simplefoc and I am reading and learning without stopping.

Well, you need to design a circuit, then we can give some advice here…

Generally, the way to do it is to use a FET to pass excess energy through a shunt resistor to GND, and a circuit to compare the voltage and activate/deactivate the FET. Either to protect the PSU, or prevent overcharging a battery.
But this circuit could be a little complicated, if you’re not using the MCU to control the whole process, then you’ll need to find a comparator, voltage reference, and design a circuit around these, also adjusting all the voltages to the levels the components can handle. I was looking through my docs for a reference, but couldn’t find one just now.

This reference design should give you some ideas on how to manage regenerated energy: Brake Chopper - Trinamic

Thanks for response. I saw that circuit, but it is 24v and only 500ma. I found exactly what I want is here.

He is sensing voltage and setting io high when treshold value exceeds. The io is connected to mosfet gate which drives power resistor to ground.

I am looking this code’s equavalent to simplefoc.

Any sample about this?

So I decided to use external adc and voltage divider to measure voltage from arduino. Then when some over voltage, I will use mosfet to drive power resistors. Also I will use tvs diode for analogue backup system at higher voltage.

I will try can and pwm for communication. But I have 2 questions.

  1. As I know pwm pin of esc is 5v tolerant. If I write pwm, say %50 duty cycle from arduino (5v), is esc will read that as %50 ? I think esc’s %50 is 1.65v (3.3v /2) and arduino’s is 2.5v(5v/2).

  2. Can I use usb of esc as regular uart for serial.println for debug messages?

Thank you.

yes, you can! The USB is connected to USART2 on the STM32G431…

I think it will work - but not in the way you describe - its not an analog signal, its a PWM signal, which changes rapidly between 0V and VCC - which is 5V for the Arduino. You can think of it like a “square wave” signal. So the question is will the STM32 read the ardino signal low (0V) as low - yes. And will it read the high (5V) as high state? - yes. In the other direction (STM32 sending PWM to Arduino) it is not so clear it will work, but in this direction is should be ok.