• Howdy! we're looking for donations to finish custom knowledgebase software for this forum. Please see our Funding drive thread

Bafang m820 re braining

Oct 11, 2023
After years of pulling info from forums, I finally have something to hopefully contribute.
Inspired by casainho and his m500 esp/vesc transplant.
Build spec:
M820 motor
Esp32-s3 (circuitpython)
Sw102 display (unmodified)
Grin baserunner controller.

Crack open the casing, there are no hall sensors, just a mr30 plug for the motor and the 6 pin torque sensor plug.

First port of call, getting outputs from the torque sensor:
Surprisingly, it is not a CAN sensor. Don't waste days trying to figure out why you can't receive messages from it, there aren't any to receive.
It has 5v and ground as per the m500 but:
2 outputs for a (possibly 22 pole) rotary encoder (quadrature cadence sensor) at 3.8v logic (level shift to 3.3v for esp)
1 analogue output for 0-120nm torque at ~0.8 - 4.2v. (shift to max 3.1 for esp adc)
The adafruit rotary encoder example reads the rotation direction and counts transitions Rotary Encoder in CircuitPython
Standard analogue inputs picks up the torque channel.
Last edited:
Interested in seeing how this goes. Too few have experimented with external controllers on drives like these. One unmentioned benefit is that you get free thermal headroom in the case by removing the heat of the controller.
There might be a hall sensor on the controller board that picks up an encoder magnet on the end of the shaft. That's how it works on the M620.
View attachment 354786
Nice one, I will have a look next time I crack open the case. Might be able to replace it with a hall sensors straight to the controller (not using the internal board at all)

Does it pick up 1 pulse per revolution if it's shaft based, I think the grin FOC controllers rely on motor poles passing 3 halls to use the sensor start up.

They also have a feature that keeps the motor idling at low power so you would only need it to find the motor position when starting from a compete stop if you need a faster engagement when riding.
There is indeed a magnet with a hall at the end of the motor shaft, as per the m620. From what I could find, it generates a simulated hall signal but I couldn't find anyone who has managed to tap into it or use it for the ASI/grin controllers so I will leave it on the backburner and revisit it if the response from the motor is too sluggish (I suspect it will require a 3 hall retrofit).

Back on to the project:

I have managed to get the feeds from the torque sensor into the ESP32
I have used the encoder signal as a trigger for motor output: when position changes 2 pulses forwards with 0.2s torque sensing becomes live.
The torque sensing is smoothed and converted to Nm
I have deviated from casainho's power ramping method and gone for direct torque scaling, saves a lot of calculation and eases my brain. After all power is just a function of torque and rotational speed. I believe FOC controllers typically map throttle to torque output.
I have managed to tap into the TX line of the display to pickup the assist levels and set my own scaling factors which are editable over wifi for fine tuning (along with anything else in the code). A logic level shifter shifts the 5v display logic to the 3.3 for the esp.
the torque input is then multiplied by the scaling factor and that if mapped to the range for an MCP4725 DAC (the esp32-s3 does not have any DAC, I tried PWM with filters but it wasn't working well and introduced more delay between input from the pedals and output from the motor).

The baserunner is only connected to the motor phases of the M820, I have added an NTC and a speed sensor though the wiring is wrong, I think I need a 5k resistor as R2 in the divider circuit to get the output voltages the baserunner expects.

The motor runs, and responds to the pedals turning whilst a dummy torque signal is applied.
The idling feature also works nicely and makes the motor very responsive.
I should probably add that the esp32 is completely unnecessary (it is only needed for being pedantic over the torque scaling).

The analogue output can be directly connected to the Baserunner's PAS throttle and the throttle map adjusted in the Phase runner suite, the assist levels from the display can be used by the baserunner and power output capped accordingly.

I unfortunately really don't like this method of assist scaling (the unmodified M820 does the same thing). If you have a torque sensor, why not use it. After all, we naturally control the speed of our bikes with torque input so surely adding a motor with a controller that responds directly to this makes sense.