This question is for an Uno, or more specifically, an atmega328P.
My project involves sending variable-duty PWM signals from 4 different pins to 4 high-side power switches to control 4 independent loads. Each of these power switches can handle X amps to the load. The 4 PWM signals are always the same frequency and always the same duty cycle (duty cycle will change with time but all 4 signals will be the same at a given time).
I would like the option of also being able to connect a larger single load that requires ~4X amps by having the 4 power switches operating "in parallel". I'm imagining doing this by ensuring that the 4 PWM signals are in-sync with each other to turn the power switches on/off at the same times.
Can this be achieved with a 5V, 16MHz atmega328p simply by using pins D9+D10 (Timer 1) and D3+D11 (Timer 2) pins, using prescaling factors, and calling analogWrite() on the 4 pins? I know I can use prescaling factors to get these 4 pins to output 122.55 Hz PWM signals, which is a desirable frequency. However, will the signals be in sync? Or are some extra software adjustments required?
Why do you need 4 outputs? Why don't you just send the same PWM signal to all 4 switches?
Hi gfvalvo, good question; I wasn't clear on that.
The 4 separate outputs are for when multiple independent loads are connected. I need to be able to control which of the 4
outputs switches are operational.
I'm all ears if you want to suggest a different hardware approach to the problem, but it seems like I'm very close to the solution if the signals can be synced via software (if they aren't already synced by default).
Timers can be synchronized, see the controller data sheets.
It's been a while since I dug that deeply into the ATmega's timers, so I can't answer the syncing question. But, if it turns out to be unworkable, you could use a single PWM output and 4 additional digital outputs that would individually gate the (single) PWM on/off at the 4 different switches. Might just require an extra AND gate.
Timers can be synchronized, see the controller data sheets
I've never tried it but this is your best bet. You can synchronize timer0/1/2 to all start at the exact time.
The other option is go for an XMEGA CPU. It's timers have 4 output compare (PWM) units. Unfortunately, it's not Arduino IDE compatible.
The Mega timers only have 3 (PWM) outputs.
The new XMEGA controller indeed has up to 4 outputs on three type 0 timers and additional wave form generation features.
The frequency, duty cycle and phase are all identical. You need only to control which of the 4 output pins sends the signal.
You could simply do something similar to the tone library. Have one timer. In that timers interrupt service routine, simply switch the required pins. Best would be with direct port manipulation (instead of digitalWrite() ) and even better if all 4 pins are on the same port then you could do everything with one statement and use the DDR (pinMode() ) to determine which pins are active.
What about feeding the output from a single PWM pin to some AND gates and then enabling or disabling the gates with other I/O pins that are just set HIGH or low as required. You could use a 74HC08.
Or you can use the GTCCR register (Atmega 328) to synchronise timers. I used that to synchronise two different Atmega 328s so a model train could run from a track section with power controlled by one 328 to the next section controlled by a different 328