PWM on any Pin?

Is it feasible to attempt this? I was wondering if a library existed for such a thing. I know cycling digitalWrite on and off would work, but only so long the code was at that point, at other points that PWM cycle would freeze.

I want to flash a number of LEDs on an existing project that has LEDs on non-PWM pins.

This is called software PWM. It's very inferior to hardware PWM. If you have enough hardware PWM pins then you're better to change the wiring to use those pins for the LEDs. If that's not possible then software PWM might be a reasonable solution. digitalWrite is very inefficient so it's not suitable for software PWM. My favorite software PWM library is this (for AVR Arduino boards only):

I have a fork of it that allows you to use Arduino pin numbers to define your PWM channels:

Hello there!

What you could try is hooking up a PWM pin to the gate of a N-Channel MOSFET. Tie the source to ground. You could have several LEDs and their current limiting resistors hooked up to one transistor and one PWM pin. The voltage and current for the LEDs would come from the supply not the pin, and you can modify the resistor values according to the current that the FET allows. With different resistor values, each LED could be a different brightness, but could not be controlled individually, if thats what you want.

Why PWM if you only want to toggle LEDs on and off?

Essentially each LED is an indicator. Red Green and Blue. A low low high flash pattern means this, a off high off pattern means that.

mattlogue:
Essentially each LED is an indicator. Red Green and Blue. A low low high flash pattern means this, a off high off pattern means that.

If this implies flash on/off periods on the order of tenths of a second or longer then a software solution is almost certainly reasonable. Hardware PWM is more applicable to flash intervals much faster than detectable by the human eye (490 Hz by default) as might be used to vary the apparent intensity of the LED.

See this thread for instance: Demonstration code for several things at the same time

Thanks Folks.

I may use the softPWM lib once I get my flash routine debugged. That essentially is BWOD for three independent LEDs with an optional duration for each. It ends up being much more complex, sixty lines with my lackluster coding skill.

Matt

I may use the softPWM lib once I get my flash routine debugged. That essentially is BWOD for three independent LEDs with an optional duration for each. It ends up being much more complex, sixty lines with my lackluster coding skill.

I think you missed something... PWM is for dimming.

To flash the LED on/off you can use digitalWrite() with delay() or with millis() timers like the Blink Without Delay Example.

Execution will "freeze" (pause) during delay() so you'll want to use the millis() method. You can have multiple-simultaneous millis() timers so you could have 3 LEDs blinking at different rates, if that's what you wanted.

If you have delay() anywhere else in your program, you'll probably want to get rid of that too.

For what you're doing, you can probably make one millis() timer with an if-statement to update the LED pattern twice per second (or whatever you want).

If you know how to write a function, you can call that LED function twice per second... Functions make your code a little "cleaner" and you can have a smaller-simpler main loop. But, if you don't know how to make a function, you can save that for a later time and just put everything in your if-statement.

So... Your loop would be looping "real fast" and if a half-second hasn't passed your if-statement will skip-over the blink/LED pattern code.

Thanks DVDdoug!
I was wondering how long this misunderstanding of PWM was going to go on!

Beginners: Think about the letters P W M, there is no F in there.

My understanding of this:

mattlogue:
Essentially each LED is an indicator. Red Green and Blue. A low low high flash pattern means this, a off high off pattern means that.

is that "low low high" means "dim dim bright".