Page 1 of 1

How to turn on and off a pwm pulsed IR LED

Posted: Wed Oct 29, 2014 1:10 pm
by Jester
I will implement an infrared protocol with the pyboard.
Therefore i need to turn on and off a pwm pulsed IR LED.

What i did so far:

Code: Select all

class IrLed(object):
    def __init__(self, pinId, timerId, timerChannel, freq):
        self._timer = pyb.Timer(timerId, freq=freq * 2)
        self._timerChannel = timerChannel
        self._pinId = pinId

    def On(self):
        self._timer.channel(self._timerChannel, pyb.Timer.OC_TOGGLE, pin=self._pinId)

    def Off(self):
        pyb.Pin(self._pinId, mode=pyb.Pin.OUT_OD, pull=pyb.Pin.PULL_DOWN)

irLed = IrLed(pyb.Pin.board.Y1, 8, 1, 56000)
I didn't found a better way to turn on and off the LED, but this solution works so far. The problem is, that i will toggle the LED inside an timer interrupt. But the implementation of the On method creates a new timerChannel object, what is not allowed.

Is there a way, to create the timer once in the init method and then switch the timer on and off?

Re: How to turn on and off a pwm pulsed IR LED

Posted: Wed Oct 29, 2014 10:54 pm
by dhylands
You can turn the pulse off by changing the pin mux (aka alternate function).

What are you trying to do with the IR?

If the timer is generating a 56 kHz signal, and the pin is configured as a GPIO, then it won't see the 56 kHz signal (it will see the GPIO value). When you configure the pin to be the timer channel, then it will see the 56 kHz signal.

You can call pin.init to change the function of a pin. I'm pretty sure that doesn't allocate any memory.

Re: How to turn on and off a pwm pulsed IR LED

Posted: Thu Oct 30, 2014 7:02 am
by pythoncoder
I'm not entirely sure what you're aiming at, but If you're using PWM can't you turn the LED off by setting the PWM ratio to zero? That way you can leave the timer, channel and pin configurations static.

Regards, Pete