Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Idea of using random numbers to generate pwm.

Status
Not open for further replies.

misterT

Well-Known Member
Most Helpful Member
I think this fits under Microcontrollers (embedded systems). This is just an idea I had few years ago and now I'm considering developing and testing it again.

Pulse Width Modulation (PWM) is usually generated with a counter and a compare value. When the counter is below the compare value, the output is low and otherwise high. The counter is basically a constant frequency sawtooth signal. And the generated PWM signal has the same frequency.. constant frequency which creates a strong peak in the frequency spectrum. This is a problem. Also a sawtooth counter has a poor performance around its most usual working point.. around 50%. At that point you could increase the frequency.

The main thing about a counter counting up from 0 to 255 over and over again at constan frequency is, that the histogram of the generated numbers over time is flat.. all the numbers appear equally often. Of course, because they are ordered from 0 to 255. My solution is to mix these numbers. Instead of using a counter, why not use a Random Number Generator which produces evenly distributed random numbers between 0 and 255. This spreads the frequency spectrum of the generated PWM and works on higher frequencies around the 50% midpoint.

I know this is not exactly very new idea any more. But I am surprised that modern microcontrollers don't have hardware random number generators with comparators and random-pwm output. Last time when I played with this idea, I used an XMega microcontroller with AES crypto engine to produce random numbers and DMA-channel to move them to a buffer efficiently. The comparator was a constant frequency interrupt routine.. so that had to be done in software. I think i got it working around 150kHz. I was planning to use it to control a switched-mode power supply.
 
Last edited:
I'm sorry, but I don't have the faintest idea what you're on about?.

PWM is squarewave, not sawtooth - where has sawtooth appeared from?, and what is a 'sawtooth counter'?.

And how would randomly creating the PWM values help? - the whole point of PWM is usually to create fixed 'voltage', speed' etc, often controlled by feedback.

Generally there are two systems, either constant frequency variable pulse width, or constant pulse width variable frequency.
 
I'm talking about how the final squarewave PWM signal is generated. Maybe a picture helps.
When a counter counts from 0 to 255 and then rolls over to 0 and keeps counting. It is a sawtooth pattern.

arduino_120.png


There are many benefits of using random pwm. Main benefit is reduced electromagnetic interference. Maybe just google "random pwm" and see how much research is done. Also called "stochastic signal density modulation". https://en.wikipedia.org/wiki/Stochastic_Signal_Density_Modulation
 
Last edited:
The sawtooth is entirely notional, unless you use a D-to-A converter.
 
Could not grasp your idea completely.

It is not clear to me if you intend to retain the possibility of varying the DC...? If so, where do you actually do it?

For a moment it reminded mi the BCM method.
 
It is not clear to me if you intend to retain the possibility of varying the DC...? If so, where do you actually do it?
The same way than in a traditional microcontroller pwm module. But instead of using a linear counter, this would use evenly distributed random numbers.

Traditional:
Take timer value, compare with desired duty cycle, set output high or low.

Random pwm:
Take random value, compare with desired duty cycle, set output high or low.
 
Random pwm:
Take random value, compare with desired duty cycle, set output high or low.

In other words, in the random PWM modality you vary the DC by varying the limit of the range where the random values occur, right?
 
You could dither the PWM frequency in a random pattern to reduce single frequency EMI, as they sometimes do with digital clocks, is that what you are trying to do?
 
evenly distributed random numbers
Do you mean evenly distributed in time or in value? Either way, is the result truly random?
To control a motor/lamp/etc with PWM you need a well-defined duty cycle, not one varying randomly.
 
Random pwm:
I probably don't understand but: Here is a picture in blue of noise from a PWM at a fixed frequency. In red is the noise from a spread spectrum PWM. (remember log graph) The noise is either a fixed 100khz or 95 to 105khz constantly moving.
1582233677130.png

There is no crystal to make the PWM but a VCO. The VCO is fed with a triangle wave at about 1khz.
1582234011866.png

Spread Spectrum is only used on power supplies where duty cycle effects Vout and Frequency has no effect on Vout. So do not use on full resonant supplies or you will see the 1khz ripple in the output.
 
In other words, in the random PWM modality you vary the DC by varying the limit of the range where the random values occur, right?
No, you vary the compare value. The same way you do traditional pwm. Only difference is that the counter values do not come in order. They come in random order.
 
I think I understand what you are trying to say .... The Period is randomized with the appropriate Duty Cycle you specify. Parallax does this with their Basic Stamp II PWM function .... It's great for DAC providing an analog reference, but does not work that well for motor control unless the motor accepts a Voltage such as some VFD motors.
 
I think I understand what you are trying to say .... The Period is randomized with the appropriate Duty Cycle you specify. Parallax does this with their Basic Stamp II PWM function .... It's great for DAC providing an analog reference, but does not work that well for motor control unless the motor accepts a Voltage such as some VFD motors.

No, I mean this (Signal Density Register is just another name for Duty Cycle):
SSDM-Generation.png

 
Last edited:
Honestly I think we are saying the same thing .... The Pseudo Random Number source controls the Period of the Duty Cycle which varies at a random interval from one cycle to the next. The Signal Density Register controls the ON time vs the OFF time within the Pseudo Random Number source Period ... IOW the Duty Cycle or Signal Density
 
What are you using random PWM for?
 
There seems to be a generalization here on the usage of this method ... I found this article that is not quite what you are after but it is very close ... the method in the article could allow communication based on probability over a noisy environment, knowing that any true random noise would have a tenancy to cancel out. But the article manly focuses on Stochastic arithmetic.

 
What are you using random PWM for?
Just a general interest.. studying. I have built an electric guitar from scratch, but never learned to play it. I like woodworking and electronics.. programming.

Do you know how a violin works? The bow is a random number generator and the violin resonates on a specific frequency.. but it needs to be given all frequencies... and that is what the bow does. It provides wide range of frequencies.

Also a Fourier transform of a sharp pulse is quite flat over wide frequency range. So picking a guitar string provides all frequencies.. and the guitar resonates.
 
Last edited:
is this a similar idea to the viper series of compact smps chips, where at low power levels the chip varies the switching freq?
 
misterT i like your idea but...

if you are scrambling the ramp values, you are also SIGNIFICANTLY increasing the number of switching events (on/off or off on). A PWM controller typically controls a Mosfet to switch the higher power load. Each time the switch is flipped, the rise time or fall time means it is between on and off for the transition and the Mosfet will then start dissipating heat snd you may run into problems. To solve this, you'll need to increase the duration of each "tic" (tic = time of each step in your ramp (even though your intervals are no longer forming a ramp)). after you increase the tic duration, your number of switching events per second goes down but, you'll be no better off than your initial (traditional) PWM.

If, on the other hand, you have some super MOSFET that can switch extremely fast, and can handle your new method without problem, then you could have solved your perceived problem by increasing the frequency of your traditional PWM to achieve "better" control as well with much simpler code.

Unfortunately, very high PWM switching frequencies can generate EMI that is much more difficult to shield, filter, or absorb.
 
Unfortunately, very high PWM switching frequencies can generate EMI that is much more difficult to shield, filter, or absorb.
it's a tradeoff, higher switching frequencies means that the EMI spurs are further apart across the spectrum.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top