In this video I show the difference between fast pulse width modulation and phase-correct pulse width modulation in an AVR microcontroller. Edit 2022-06-01: the source code can be found here pastebin.com/X...
Excellent explanation. I’ve been working on a couple of pwm projects and have been completely confused by the difference. My only criticism of this video is you didn’t explain why someone would use fast over phase correct or vice versa :). Either way this gave me a good start. Thanks!
This video was planned as a short additional material for my students, not as a self-explanatory, full tutorial... if I ever find the time... Just in short, fast-mode gives you twice the pwm frequency at otherwise same performance, but it is also the standard mode in most cases. If you run two channels at different duty cycles still they will either be switched on or off at the same time giving you larger current transients in total. Phase-correct pwm not only distributes the switching times more widely, it also can be used if you need to avoid overlaps between switching, e.g. if you use one channel for the switching of the upper and one for the lower transistors in a H-bridge.
my question is , why my 25 KHz signal on my Arduino Uno (Atmega 328 p) is very stable almost perfect while on Attiny85 there is a constant shift of duty cycle like a few percent and also frequency is not very stable for example between 25 000 - 25 400 it varies etc. Do you have any idea why is it so ? How ti improve it? I am not going to solder this until I make it more accurate (it will be for my ESC of brushed motors controller project and I need it to be very stable).
all Arduino boards I know of (especially Uno and Nano) use a 16 MHz quartz crystal, sacrificing 2 pins of the package of the Atmega328. You can also run the Atmega328 on an internal RC-oscillator like the ATtiny. The internal oscillator can be calibrated, but by default it is specified to be accurate to about 2% as you observed.
Hi, I need your brain power. Dual PWM resistor weighting in analogue adds more resolution and bits. I'm making an AM transmitter. Can we do that digitally??? Combining bits from D9 D10 phase correct to 14 bit possible? I have 31.25 KHz on each pin as PWM. A0 takes in Audio. ADC is 10bit only but I want more PWM resolution to suppress noise I need to combine the high and low bits digitally and perhaps add a bit or two from Arduino clock, maybe.. so this 31.25KHz PWM is gonna turn on and off 1600KHz carrier via a nand gate. We got audio now at 1600KHz No one is able to help me. Not even Arduino forums. Please suggest.
With "digitally" do you mean OOK (on-off-keying)? Otherwise I don't see how "switching the carrier with a NAND gate" will give you a continuous modulation of the amplitude of the carrier wave. Well, in principle of course, if you switch on and off the carrier wave and then pass it through a high-pass filter getting rid of the PWM signal, you would get the carrier wave and its sidebands through and thus your desired signal, the separation between 31 kHz and 1600 kHz might be enough to handle for a quite simple filter. But then you should also be able to do this simultaneously from two resistor-weighed outputs - however you would need to handle the carrier switching and filtering separately for both channels and only mix them afterwards. Essentially you would need a bandpass filter for each "PWM channel" around the carrier frequency, wide enough for your desired audio frequencies and narrow enough to suppress the PWM frequency. After the filter you mix the two signals using weighed resistors.
that could almost sound like a question I ask my students... If you are using an ATmega and you are using the same timer for both pwm-channels, then you can change the duty cycles individually by setting the OCRxA and OCRxB to different values. E.g. for Timer1 this would be OCR1A and OCR1B. Now you can use one of the ADC channels to read the potentiometer value and use this value to set the corresponding OCR-value. In Timer1 there is are 10bit PWM modes which suit the 10bit ADC of the Atmega ideally. In the main loop you would periodically start an AD-conversion and then just write OCR1A=ADC. In Arduino-speech you could do almost the same by writing analogWrite(analogRead(chan)/4) here you need to divide by 4 because in the default settings analogRead is 10bit wide but analogWrite only 8bit.
there is really not much of a schematics - the main connection is a 10 kΩ potentiometer to give a variable voltage to pin ADC0 and the multiplexed 4-digit LED display, connection description is in the source code which I had to tidy up a bit: pastebin.com/XrgEnbUL
If you just want to control individual loads then you would prefer fast PWM because it gives you twice the pulse frequency and thus is easier to filter, less flicker. But if you want to control a load in an H-bridge configuration then the phase-correct one will give you non-overlapping signals for the high side and low side drivers.
Excellent explanation. I’ve been working on a couple of pwm projects and have been completely confused by the difference. My only criticism of this video is you didn’t explain why someone would use fast over phase correct or vice versa :). Either way this gave me a good start. Thanks!
This video was planned as a short additional material for my students, not as a self-explanatory, full tutorial... if I ever find the time...
Just in short, fast-mode gives you twice the pwm frequency at otherwise same performance, but it is also the standard mode in most cases. If you run two channels at different duty cycles still they will either be switched on or off at the same time giving you larger current transients in total.
Phase-correct pwm not only distributes the switching times more widely, it also can be used if you need to avoid overlaps between switching, e.g. if you use one channel for the switching of the upper and one for the lower transistors in a H-bridge.
@@uwezimmermann5427 Thank you! that helps a lot.
Hello Sir, can I ask you a question via email about the MPPT algorithm that I am trying to make using Arduino Mega 2560?
I use this for motor control on a STM32. But I have never heard the expression phase-correct, I have only heard center aligned.
different manufacturers, different names...
my question is , why my 25 KHz signal on my Arduino Uno (Atmega 328 p) is very stable almost perfect while on Attiny85 there is a constant shift of duty cycle like a few percent and also frequency is not very stable for example between 25 000 - 25 400 it varies etc. Do you have any idea why is it so ? How ti improve it? I am not going to solder this until I make it more accurate (it will be for my ESC of brushed motors controller project and I need it to be very stable).
all Arduino boards I know of (especially Uno and Nano) use a 16 MHz quartz crystal, sacrificing 2 pins of the package of the Atmega328. You can also run the Atmega328 on an internal RC-oscillator like the ATtiny. The internal oscillator can be calibrated, but by default it is specified to be accurate to about 2% as you observed.
@@uwezimmermann5427 Thanks for the reply. I really appreciate it.
Hi,
I need your brain power. Dual PWM resistor weighting in analogue adds more resolution and bits.
I'm making an AM transmitter. Can we do that digitally??? Combining bits from D9 D10 phase correct to 14 bit possible? I have 31.25 KHz on each pin as PWM. A0 takes in Audio. ADC is 10bit only but I want more PWM resolution to suppress noise
I need to combine the high and low bits digitally and perhaps add a bit or two from Arduino clock, maybe.. so this 31.25KHz PWM is gonna turn on and off 1600KHz carrier via a nand gate. We got audio now at 1600KHz
No one is able to help me. Not even Arduino forums. Please suggest.
With "digitally" do you mean OOK (on-off-keying)? Otherwise I don't see how "switching the carrier with a NAND gate" will give you a continuous modulation of the amplitude of the carrier wave.
Well, in principle of course, if you switch on and off the carrier wave and then pass it through a high-pass filter getting rid of the PWM signal, you would get the carrier wave and its sidebands through and thus your desired signal, the separation between 31 kHz and 1600 kHz might be enough to handle for a quite simple filter. But then you should also be able to do this simultaneously from two resistor-weighed outputs - however you would need to handle the carrier switching and filtering separately for both channels and only mix them afterwards.
Essentially you would need a bandpass filter for each "PWM channel" around the carrier frequency, wide enough for your desired audio frequencies and narrow enough to suppress the PWM frequency. After the filter you mix the two signals using weighed resistors.
ı need some help. ı want to keep first pwm stabil and control second pwm with potentiometer
that could almost sound like a question I ask my students... If you are using an ATmega and you are using the same timer for both pwm-channels, then you can change the duty cycles individually by setting the OCRxA and OCRxB to different values. E.g. for Timer1 this would be OCR1A and OCR1B. Now you can use one of the ADC channels to read the potentiometer value and use this value to set the corresponding OCR-value. In Timer1 there is are 10bit PWM modes which suit the 10bit ADC of the Atmega ideally. In the main loop you would periodically start an AD-conversion and then just write OCR1A=ADC.
In Arduino-speech you could do almost the same by writing analogWrite(analogRead(chan)/4) here you need to divide by 4 because in the default settings analogRead is 10bit wide but analogWrite only 8bit.
@Uwe Zimmermann It is so important proposal for my school project. thanks a lot for your help.
Can I get the code and schematic pls?
there is really not much of a schematics - the main connection is a 10 kΩ potentiometer to give a variable voltage to pin ADC0 and the multiplexed 4-digit LED display, connection description is in the source code which I had to tidy up a bit: pastebin.com/XrgEnbUL
@@uwezimmermann5427 thanks
what is the use of one over the other.
If you just want to control individual loads then you would prefer fast PWM because it gives you twice the pulse frequency and thus is easier to filter, less flicker. But if you want to control a load in an H-bridge configuration then the phase-correct one will give you non-overlapping signals for the high side and low side drivers.