# How many PWM outputs can you run at once on a AT328 ?

Re another poster who wanted to run 3 seperate simultaneous PWMS...

Since these are timer-based, there must be limit to how many analogWrites at once ...

thanks in advance for authoritative help.

regards

Allan

there must be limit to how many analogWrites at once

One. It's a single core processor.

Are you sure?

I've made a revcounter running on a pro-mini driving a 270 degree quadrature ( sin/cos meter) with two coils, each of which is driven with PWM at once , and it works perfectly.

So the answer must be at least two.

Allan

I enclose the relevant snippet of code…

``````  //   0..90
if ((angle >= 0) && (angle < 90)) {  //first quadrant
analogWrite( coil_0_minus , 0);
analogWrite( coil_1_minus , 0);
analogWrite( coil_0_plus , coil_0_val = sinA[angle]);
analogWrite( coil_1_plus , coil_1_val = cosA[angle]);
}
// 90 .. 180
if ((angle >= 90) && (angle <180)) {  //second quadrant
analogWrite( coil_0_minus , 0);
analogWrite( coil_1_plus  , 0);
analogWrite( coil_0_plus  , coil_0_val = cosA[angle - 90]);
analogWrite( coil_1_minus , coil_1_val = sinA[angle - 90]);
}

// 180..270
if ((angle >= 180) && (angle < 270)) {  //third quadrant
analogWrite( coil_0_plus  , 0);
analogWrite( coil_1_plus  , 0);
analogWrite( coil_0_minus  ,coil_0_val =  sinA[angle - 180]);
analogWrite( coil_1_minus , coil_1_val =  cosA[angle - 180]);
}

// 270-360
if ((angle >= 270) && (angle < 360)) {  //fourth quadrant
analogWrite( coil_0_plus   , 0);
analogWrite( coil_1_minus  , 0);
analogWrite( coil_0_minus  , coil_0_val = cosA[angle - 270]);
analogWrite( coil_1_plus   , coil_1_val = sinA[angle - 270]);
}
``````

As you see, I’m using 4 analog outputs, but 2 of them are grounded in each drive quadrant.
Allan

Are you sure?

Absolutely certain. In fact, the language doesn't even support it.

If you'd asked "How many concurrent hardware PWM outputs can I have active simultaneously?" then my answer would've been "For an Uno, six (plus however slower rate ones you can support using software)"

Edit: You posted your code just before I got my reply up - Your "analogWrite"s are consecutive.

It can output up to 6 PWM signals at once (there are three timers, 2 PWM outputs per timer).

Note that other libraries that make use of the timers will take out the PWM pins associated with that timer, and Tone() will take out timer2's two channels for the same reason.

Your finer pinout diagrams will show which timer is associated with which pin (it's expressed like OC1A - that's channel A on timer1. The OC stands for "output compare" which is what the PWM generation mechanism is called - it "outputs" the result of "comparing" a specified value against the current value of the timer)

Edit: You posted your code just before I got my reply up - Your "analogWrite"s are consecutive.

They are indeed - but this bit of code only runs rwice a second, so the pwm keeps going in the gaps…

Allan

AWOL:
One. It's a single core processor.

Except for configuration, the processor core isn't involved in PWM. It's a timer function as DrAzzy describes.

If you're doing software PWM via blink without delay style programming, you can have quite a few.
Here are 8 demo'd on a 1284P (out of 13 programmed, I only 8 buttons for testing tho).

The frequency you are using might become a limiting factor. I coded for an entire grand piano range, this was using the middle of the piano range.

MrMark:
Except for configuration, the processor core isn't involved in PWM. It's a timer function as DrAzzy describes.

I never said otherwise - I was responding to

there must be limit to how many analogWrites at once

.

You can only do one analogWrite at a time.

allanhurst:
Re another poster who wanted to run 3 seperate simultaneous PWMS...

Since these are timer-based, there must be limit to how many analogWrites at once ...

An Arduino UNO (Atmega328) has 3 hardware timers and using the Arduino core library each timer controls two PWM pins. So you can control six PWM pins using an Atmega328 for PWM output using analogWrite() at the same time, controlled by hardware. Additionally you can perhaps use one or two pins for output of "software PWM".