Fast pwm question?

forgive me upfront. i have been reading for hours now.

I have read Secrets of Arduino PWM and several other web pages.

question:

using timers to control pwm is still inside the main loop? no one is really clear about that…

thanks for the help

Setting up PWM is a set & forget kind of function. Do it once in setup().
If you need to change it, call a function to change it.
It disrupts things if you keep setting it over & over on loop().

using timers to control pwm is still inside the main loop?

Can you explain more about what you mean?

The hardware timers and their outputs operate in the background independent of loop(), but changes to the set up parameters can certainly be done in loop().

thanks guys for the reply.

what i was hoping i could do but couldn't find the answers for..

setup()

set 2 pwm channels or pins using fast pwm or something similar. (these run high power LEDs)

void loop()

do some other stuff
user inputs // momentary buttons
temp control // using a PID algorithm

// all results in

set fast pwm registers to new values (if needed)

thanks again.

what i was hoping i could do but couldn't find the answers for..

Sure you can do that.

What do you mean “couldn’t find the answer”?

Do you mean you couldn’t find anyone that had done it before and put it on line?

No not exactly. Though code examples of exactly what I want would be great :slight_smile:

I am not sure if I could set a timer to run while executing code in the main loop.

What I really want is to run 2 or more pwm pins independently while executing code in the main loop. Depending on conditions, change the pwm executing on the 2 pins.

I could be worried about nothing.

I have a high powered led project I have been working on. 2 leds run with pwm on 2 different pins. Depending on user input through buttons, the pwm of each led is set. Each led can be low, high, off, and there is a flash button that causes a strobe between the 2 leds. My heat sinks are not big and I want to keep it that way due to area constants. With my rudimentary code skills I have managed to get this portion the project working except that when the ambient temperature is above 30 degrees c i have a heat problem in the led high mode.

So... In the case where leds are on high. I have to add temp control which I have done and am working to understand pid control. But it's turning into quite a bit of math and I'm worried about execution time in the main Loop while keeping the pwm(s) running without visible shutters or jitter..

Am I worried about nothing?

Please keep in mind, I am still quite new to arduino, coding and software.

Thanks for any advice.

What I really want is to run 2 or more pwm pins independently while executing code in the main loop. Depending on conditions, change the pwm executing on the 2 pin

I can’t see any problem with that. You simply change the duty cycle of a PWM pin with an analogWrite call.

PWM is a set and forget and their is no way of telling the state of the PWM pin in normal operation. You can mess about with the registers but I think the simplest way of reading the state of a PWM pin is simply to loop it back to an input pin. You can read that with a normal digitalRead call or use the direct register addressing if you want the read to be quick.

Rontopia:
...using fast pwm or something similar. (these run high power LEDs)...

Why do you think you need fast PWM for power LEDs.
Default PWM (500Hz or 1khz) should be fast enough.

What is the power rating of the LEDs.
Are you using resistive current limiting or constant current switching LED driver boards.
Leo..

Wawa:
Why do you think you need fast PWM for power LEDs.
Default PWM (500Hz or 1khz) should be fast enough.

500 Hz is fast enough. I should be more specific. when I say fast pwm I am referring to using one of the 3 available timers rather than calling analogwrite in the main loop … this is where im getting confused.

Wawa:
What is the power rating of the LEDs.
Are you using resistive current limiting or constant current switching LED driver boards.
Leo…

im using a simple low side mosfet n-ch for each Led. I have previsions to limit current. the leds (both of them) are running on their own switching power supply that can supply 1 amp nominal. right now i can get away with this because only one LED is on at a time…

like this

for (int a = 1; a < counterOne; a++)
{
analogWrite(rLed, ledDuty1);
analogWrite(lLed, ledDuty2);
}
for (int b = 1; b < counterOne; b++)
{
analogWrite(rLed, ledDuty2);
analogWrite(lLed, ledDuty1);
}

Grumpy_Mike:
I can’t see any problem with that. You simply change the duty cycle of a PWM pin with an analogWrite call.

PWM is a set and forget and their is no way of telling the state of the PWM pin in normal operation. You can mess about with the registers but I think the simplest way of reading the state of a PWM pin is simply to loop it back to an input pin. You can read that with a normal digitalRead call or use the direct register addressing if you want the read to be quick.

hi Grumpy,

when you say "PWM is set and forget" what do you mean? set and forget when using a one of the timers that run outside the main loop? or set and forget when you do basic pwm with analog write inside the main loop?

this is where im getting confused.

i can set a timer to run pwm lets say pin 9. when the part is booted, the timer is loaded with whatever code and it runs forever or until something breaks, it runs out of power, or trump does something ends the world yet again. right? that's running outside the main loop. no matter what go's on in the loop the timer will keep counting and the led will keep turning on and off at a set frequency.

now there are 2 places where i get lost

sticking with using the external timer, i can use an analogwrite to change the pwm of the timer from inside the main loop?

or.. does set and forget mean something else? like, a timer is set during setup() and that is that.

I know it seems like im asking the same questions but, I still haven't found any code examples that change one of the timers settings from within the main loop. perhaps im looking in the wrong places?

set and forget when using a one of the timers that run outside the main loop? or set and forget when you do basic pwm with analog write inside the main loop?

Their is no difference from calling analogWrite inside or outside the main loop. Repeated calls of analogWrite faster than you can get a cycle of PWM out is going to screw you up no matter where it happens.

or trump does something ends the world yet again. right?

Yes.

i can use an analogwrite to change the pwm of the timer from inside the main loop?

Yes.

I know it seems like im asking the same questions

Yes.

I still haven’t found any code examples that change one of the timers settings from within the main loop.

Sadly Arduino tutorials sometimes contain a mistake. In that one the code at the end says:-

// wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);

This is rubbish, however what the delay does is to stop you changing the PWM value too quickly.