Monday 27 May 2019

Why change microcontroller's PWM frequency?


What are exactly the pros/cons of changing the PWM frequency?


I don't think I need to change any PWM frequency at this very moment - but since I don't know the benefits of doing it, how can I really know if it's useful for me?


I'd like a general answer (which I and other people reading this post can apply in different situations), but if someone wants some data to answer, here are those of the project I'm working on at the moment:



  • ATmega328 microcnotroller @16Mhz (it stands alone, it's not in an Arduino board)

  • 4 independent small brushed motors, each with a propeller (propeller's diameter: around 6 cm), on pins 3,5,6,11, controlled by a BUK9840-55 trasnsistor (datasheet)

  • a simple RF receiver is placed nearby and needs to receive a 433MHz signal (the RF comminication is quite slow (500bits/sec), and doesn't need to be very reliable).


EDIT: how does the PWM speed influence the RF communication?




Answer



PWM units, typically, work the following way, or equivalent¹.


Set up:



  1. an initial output value, ie. either high or low and

  2. the "counter reset value", to which the counter is reset after reaching

  3. the "counter maximum value", as well as

  4. the "counter flip value", where the output state is toggled


After that, you just let the counter run – for example, you might set




  • initial state high,

  • reset value 0,

  • max value 100,

  • flip threshold 25


Then your counter would start at 0, and increment once every clock cycle, and at 25, the output would be set to low, until the counter reaches 100 and is reset to 0. That way, the output would be high for 25 time units, and low for 75 – a duty cycle of \$\frac14=25\%\$.


Now, the PWM frequency is typically defined as the time between the reset and reaching the maximum.


So, this inherently is one aspect of choosing a PWM frequency: if there's 100 time units (which, by the way, are typically clock ticks of something like the CPU clock divided by some \$N\$), your duty cycle "granularity" cannot be better than 1%.


On the other hand, if you let's say set the max value to 106, then you might get super nice resolution on the duty cycle, but that doesn't help you, because now the output might be low and high for so long that whatever you drive with the PWM simply sees "on" and "off", unless you go through great lengths (build a mechanically large low-pass filter) to "smoothen" things out, and then you'd lose all ability to quickly adjust the duty cycle (because the filter will also smoothen out your adjusting).



PWMs are used for very different things – for example, to generate an analog voltage, as mentioned above, by low-pass filtering. In that case, using a high frequency might be beneficial, because your low-pass filter, needing to cut off the PWM frequency, is much easier to build when that frequency is high. On the other hand, in circuits where you work with sensitive analog voltages, having a fastly switching PWM signal is dangerous, due to that signal potentially coupling over.


Other uses are, and that's probably what your motor does internally, more digital: the PWM simply controls for how long something is switched on or off, for example, the internal power supply in DC brushless motors (which are, in fact, 3-phase AC motors that have a supply that generates three sine signals from the DC voltage it takes). For these applications, as said, the PWM frequency mustn't be too low, because then your motor will stop, start, stop,…, but it mustn't be higher than the frequencies the internal supply uses to generate the AC voltages.


Yet other uses are actually signal generation usages – for example, assume you have a microcontroller with a CPU clock of 16 MHz, and you want to generate a set of different frequencies (for example, you have a Modem that uses frequency shifting as modulation, so one frequency means "0", the other one means "1") – in that application, you might use a fixed duty cycle, and what you're really interested in is the PWM frequency!


There's also devices that communicate measurement values by PWM duty cycle – or take PWM duty cycle as input, for example these "neopixels" that you might have heard of. Of course, their interface controller has a specific timing range, so you have to configure your PWM frequency to make things work.




¹ this, for example, assumes you can both set the upper and lower limit, and that the counter counts up – there's no reason that all of that is true, you can implement PWM by counting down, or by not having a variable upper limit, but that's details.

No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...