PWM within an arduino interrupt ?

Lets say I have an external interrupt to the Arduino, triggering at a constant 30Hz.
At the moment, this controls two individual LED arrays, and there is a 30 Ohm rheostat on each LED array.

What Im trying to do is eliminate the rheostats (which often need twiddling), and emulate the ‘resistance’ via software, maybe via PWM.

But I dont think you can do PWM within an interrupt, because there is no Delay() available, and I cannot increase the external interrupt to trigger at say, 10KHz.

So what I was thinking, is maybe having a 555 timer chip, to do the PWM. I then control this from within the arduino interrupt, by turning it OFF, or ON and then programming it to emit PWM at a given (software controlled) rate.

I thought about a digital pot, but I cannot find any rated at 30 Ohms (min 150Ohms), and the the 32 step granualrity is presumably going to be too ineffective.

The other problem with a 555 timer, is that my understanding is that, Im not entirely sure how I can vary the duty cycle from between 0-100% at runtime. From what Ive read, you can wire it to have a duty cycle of < 50% with resistors+cap, but to then change it to > 50% you should rewire it with a different resistor/cap combination. So I wouldn’t be able to get the full 0-100% range.

There is one final problem (as always). If I went down the 555 route, I need it to emit ~800mA. Whereas the 555 can only do 220mA, so I guess Id then need the 555 to control a transistor, with the transistor switching a 7.5V power source

I cant help thinking Im maybe over engineering this, but Id really like to remove the manual rheostats from my circuit, so they can be controlled via primitive AI in the software.

Im really interested in any other ideas how to achieve this.

Thanks :slight_smile:

PWM is the way to go. Note that functions like tone() and analogWrite() are set-and-forget forms of PWM. It doesn't matter how often your 30 Hz external interrupt comes in. The PWM goes on in "the background" whether there is an external interrupt or not.

Now a PWM pin out of the Arduino is likely not powerful enough (in terms of current) to power an LED array. So the PWM pin must control a transistor (or similar) to provide enough current.

If you provide a schematic of what you have so far perhaps we can provide more concrete suggestions.

-- The Aussie Shield: breakout all 28 pins to quick-connect terminals

Thanks, thats interesting.

I hadn't thought of AnalogWrite(), although admittedly Im not sure how I would use this to have a transistor PWM an external power source Im guessing I can rule out Tone(), since it mentions it can conflict with PWM on pin 3, which is where my interrupt is coming from. Or maybe I can switch to a different pin?

I cant provide a nice schematic unfortunately, but the project Im working on is EyeWriter 2. There is a breadboard sim here http://www.instructables.com/id/The-EyeWriter-20/step11/Full-Circuit/ and a direct image link here... http://www.instructables.com/files/orig/FAD/ICOC/GH4NNAKE/FADICOCGH4NNAKE.jpg

The 30Hz interrupt comes in via the PS3 eye VSYNC, which triggers every time the cam takes a full frame. This is used to alternative an array of infrared leds around the cam, followed by two side-arrays of leds to provide 'glints' on the eye. This is then used to capture an unilluminated image of the eye, in conjunction with a luminated image, and these are used to try and track where the pupil is, and subsequently where the eye is gazing.

What Im finding ins the rheostats are quite a big obstacle to calibrating the device quickly and easily, which is why Id like to take them out of the equation, and control them automatically via software.

Thanks for your advice so far :)

Well, here's a basic circuit that shows how to use a MOSFET transistor:

http://ruggedcircuits.com/html/circuit__7.html

The "stuff" above transistor Q1 (D1, the 12V/4A fan terminals, etc.) would be replaced by the LED array and power source for them.

-- The Rugged Motor Driver: two H-bridges, more power than an L298, fully protected

Its reassuring to know what Im trying to do isn't just a load of nonsense :D

Theres already a mosfet in my circuit, but Ill be honest, I didn't really understand its function. Ill read up a lot more on mosfets now that I know its heading in the right direction.

Thanks for your invaluable help :)

If you do end up needing to change resistances at some point still, you can always a relay to switch resistors in/out.
For example, here is a DPDT switch that puts two 68 ohm resistors in parallel, or in series. A DPDT relay could do the same.

Would look into other solutions if you needed more ranges.

relay_resistor.jpg

But I dont think you can do PWM within an interrupt, because ...

You don't want to either. In general you shouldn't try to do very much during an interrupt in order to keep it short. Generally all you do is set or clear a flag and then exit the interrupt. The main program checks the flag and does the appropriate work.

Don