How to use a Timer to blink a LED



  • Hi there,

    I've followed following example form "5. Hardware Timers" with no success

    from machine import Timer
    from machine import Pin
    led = Pin('GP16', mode=Pin.OUT) # enable GP16 as output to drive the LED
    tim = Timer(3) # create a timer object using timer 3
    tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode
    tim_ch = tim.channel(Timer.A, freq=5) # configure channel A at a frequency of 5Hz
    tim_ch.irq(handler=lambda t:led.toggle(), trigger=Timer.TIMEOUT)

    If I type every line into the REPL, I got following error...

    from machine import Timer
    from machine import Pin
    led = Pin('G16', mode=Pin.OUT)
    tim = Timer(3)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: cannot create 'Timer' instances

    Now, if I follow the example under "Quick reference for the WiPy" (which is different to previous one!!!)...

    from machine import Timer
    from machine import Pin
    tim = Timer(0, mode=Timer.PERIODIC)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: type object 'Timer' has no attribute 'PERIODIC'

    I would appreciate any help in regards to:

    1. Has anyone found a Timer Tutorial here?
    2. What is wrong with the snip codes that I 've pasted?

    Cheers
    ColombianAus



  • @jmarcelino

    thank you mate...spot on. ...Do you know what is the most recent copy "Micropython Documentation"? for WiPy 2.0?

    cheers
    ColombianAus



  • You're following the documentation for WiPy 1 but you have the new WiPy 2 which does Timers differently.

    All the new documentation is at https://docs.pycom.io

    The section you want is at https://docs.pycom.io/pycom_esp32/pycom_esp32/tutorial/includes/timers.html

    Use the Alarm example


Log in to reply
 

Pycom on Twitter