Choice of microcontroller
First and foremost the decision of the microcontroller had to be made. While there are dedicated chips available, I like to code embedded systems and thus the whole control should be done in firmware. The following GPIOs are needed:
- 6 digital ports with hardware PWM for the 6 MOSFETs
- 3 digital ports for hall sensors in the motor for position sensing
- 3 analog ports for phase sensing for sensorless operation
- 3 digital port for brake switch, pedal sensor and speed sensor
- 3 analog ports for throttle, assist mode and thermal motor sensor
- 2 digital ports for light switching
- 2 analog ports for sensing battery voltage and current
It so happens that this fits onto an Atmega328PB. The 328PB has the advantage of having 5 timers versus 3 timers on the 328P on the Arduino boards. This is a nice convenience as 3 timers can be used for the PWM generation, while still having two more timers left for general timing purposes.
Why 8-bit and not 32-bit ARM core? The idea behind this choice is a bit hacker ethics - you can still fully understand what is happening under the hood of an 8-bit chip and the datasheet is only 400 pages. In order to program the 32 bit chips you wont easily do it without using boiler plate code produced automatically by Atmel Studio. So it is a minimalist approach. Or as Antoine de Saint-Exupery put it: “Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.”
What else is needed?
The input voltage needs to cover up to 13s lipo batteries. 13s means 13 cells in series which adds up to 54.6 Volt when fully charged. These battery packs are usually sold as 48V packs and are a common standard. The controller itself needs 10-12V for driving the MOSFETs and 5V for the microcontroller.
Coming down from 55V to 12V is best done with a switching regulator. This gives also enough output power to connect a normal bicycle light. The second stage 12V to 5V is done with a linear regulator as the microcontroller and the bluetooth only need a few dozen mW of power.
The MOSFETs gates cannot be powered directly from the uC ports for two reasons: the gates need at least 10V to fully switch on and you need to switch them on and off very fast to minimize switching losses. Very fast means in the order of 100ns. There are plenty of dedicated chips out there and I choose the International Rectifier IR2104 half bridge driver for this purpose. The chip offers shoot-through protection, i.e. high and low side MOSFET will never both be switched on at the same time. This is nice to have because "shoot through" means a battery short circuit and therefore the release of magic smoke with a loud bang.
The chip also has a bootstrap circuitry thus N-channel MOSFETs can also be used for the high side.
That's it, those are the main components of the BLDC controller.
Next up: board layout overview, coming tomorrow.