How to turn on and off a pwm pulsed IR LED

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Jester
Posts: 1
Joined: Sat Oct 25, 2014 1:23 pm
Location: Dresden, Germany

How to turn on and off a pwm pulsed IR LED

Post by Jester » Wed Oct 29, 2014 1:10 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

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

Post by dhylands » Wed Oct 29, 2014 10:54 pm

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Thu Oct 30, 2014 7:02 am

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
Peter Hinch
Index to my micropython libraries.

Post Reply