# Microcontroller PWM output

I work on a project that need 36 PWM signal to drive 36 MOSFETs. Can I use a microcontroller with a limited number of PWM and configure all pins as PWM output or do I need to find a microcontroller with 36 PWM pins?

• What is the difference? – Jose May 17 '18 at 8:22
• Do you require that the 36 PMW can be controlled individually? – Bimpelrekkie May 17 '18 at 8:28
• With a fast enough microcontroller you could probably manage this with software PWM. Finding a micro with 36 hardware PWM channels might be a challenge. Alternatively, implement it in a small FPGA. – Colin May 17 '18 at 8:47
• If the speed requirement for the PWM isn't too high, you could use 3 PCA9685 LED controllers. They can also drive a mosfet. Each have 16 channels and a 12 bit resolution. – Peter Karlsen May 17 '18 at 9:25

First of all

So, can I use a microcontroller with a limited number of PWM and configure all pins as PWM output

you can not configure every MCU pin as PWM output. Physically controlled PWM output must be controlled by a timer. But you can set MCU as GPIO output pin and control it by software.

It may be hard to find MCU with 36 PWM outputs. So if PWM frequency isn't very high you can implement SOFTWARE PWM using one or more timers. Also as a solution, you may use MCU and shift registers to drive 36 PWM outputs. But in this case, driving speed of Shift Register must be high enough. I have used this solution for driving LEDs at low PWM speed just about 100 HZ.

You can select MCU from STM32 series with many timers. But this will bring extra expenses.

I don't know if your use case allows for it, but consider not using PWM at all, as hardware PWM with 36 individual outputs is a bit impractical.

You could try software PWM, but even then you would likely have problems toggling each pin at the right time in worst case scenarios (like multiple pins set to near identical values), due to the interrupt processing time.

Instead of PWM, you could output the most significant bit of all duty cycle values to the respective pins, simultaneously. Then you'd output the second-to-most-significant bits, and so on. Each set of bits in the sequence would be applied to the outputs for a time T proportional to 2N, where N is the ordinal number of the bits (e.g. the least significant bits for 10μs, the next ones for 20μs, then 40μs and so on). Although not strictly PWM, this scheme allows for modulating the average amplitude of dozens of outputs very cheaply.

Here's a simple example of four outputs controlled by 4-bit duty cycle values, but the concept scales very well for more outputs. The bits don't necessarily have to be output in any specific order, as long as they are applied to the outputs for the proper duration.

You'd probably have an array of individual duty cycle values for each output in your code (e.g. uint8_t dutyCycle[36]), which you need to transpose (make rows into columns and vice versa) into an array of output states (like uint64_t outputStates[8] or uint8_t outputStates[5][8]), so that you can output them to the I/O registers.
Here's the operation done on the previous example:

There are a few ways to do this bitwise transpose efficiently, probably better explained by hacker's delight

• If the OP goes the software PWM way, he should read up on 'vertical counters'. – Wouter van Ooijen May 17 '18 at 9:51

You can use several cheap microcontrollers with enough timers to solve your problem for high frequencies and connect them with i2c. Maybe it is possible to use a chip(s) with timers.