# Using a timer with pyb/machine

**URL:** <https://forums.openmv.io/t/using-a-timer-with-pyb-machine/9173>\
**Category:** OpenMV Boards\
**Created:** [January 30, 2024, 2:52pm UTC](https://forums.openmv.io/t/using-a-timer-with-pyb-machine/9173 "2024-01-30T14:52:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbottin](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mbottin](https://forums.openmv.io/u/mbottin)\
**Post date:** [January 30, 2024, 2:52pm UTC](https://forums.openmv.io/t/using-a-timer-with-pyb-machine/9173/1 "2024-01-30T14:52:32Z")

</div>

Hi,

I can use an interrupt timer with the ‘pyb’ library, but if I understand correctly, it would be obsolete.  
I’ve tried to do the same thing with the ‘machine’ library, but I get a ‘Timer does not exist’ error message.  
However, it works perfectly with a led and machine.Pin for example.

Could you clarify this for me or provide me with an example that’s supposed to work?

Best regards,

Mike

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [January 30, 2024, 7:34pm UTC](https://forums.openmv.io/t/using-a-timer-with-pyb-machine/9173/2 "2024-01-30T19:34:43Z")

</div>

? [class Timer – control hardware timers — MicroPython 1.20 documentation (openmv.io)](https://docs.openmv.io/library/machine.Timer.html#machine.Timer)

---

<div class="post-metadata">

**Author:** ![mbottin](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@mbottin](https://forums.openmv.io/u/mbottin)\
**Post date:** [January 31, 2024, 9:37am UTC](https://forums.openmv.io/t/using-a-timer-with-pyb-machine/9173/3 "2024-01-31T09:37:50Z")

</div>

Yes, I’ve tried to use it but without success.

Here is the simple code that I used :

```auto
from machine import Pin, Timer

led = Pin('P7', Pin.OUT_PP)

def blink(timer) :
    led.toggle()

cadenceur = Timer(2)
#cadenceur.init(mode=Timer.PERIODIC, freq=2, callback=blink)

while(True):
    pass

```

On launch, I get the error message “ValueError : Timer doesn’t exist”.  
 ![Image du presse-papiers (1)](https://canada1.discourse-cdn.com/flex029/uploads/openmv1/original/2X/b/b95c74d8ce6f1b1f2a56cd2d0d43667da448bf37.jpeg)

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [January 31, 2024, 6:51pm UTC](https://forums.openmv.io/t/using-a-timer-with-pyb-machine/9173/4 "2024-01-31T18:51:29Z")

</div>

Yeah, sorry, I need to improve that documentation. Only soft timers are supported in the machine module for all micropython boards:

```auto
from machine import Pin, Timer

led = Pin('P7', Pin.OUT)

def blink(timer) :
    led.toggle()

cadenceur = Timer(-1)
cadenceur.init(mode=Timer.PERIODIC, freq=2, callback=blink)

while(True):
    pass

```

E.g. in the code base:

```auto
machine_timer_obj_t *self = m_new_obj(machine_timer_obj_t);
    self->pairheap.base.type = &machine_timer_type;
    self->flags = SOFT_TIMER_FLAG_PY_CALLBACK | SOFT_TIMER_FLAG_GC_ALLOCATED;
    self->delta_ms = 1000;
    self->py_callback = mp_const_none;

    // Get timer id (only soft timer (-1) supported at the moment)
    mp_int_t id = -1;
    if (n_args > 0) {
        id = mp_obj_get_int(args[0]);
        --n_args;
        ++args;
    }
    if (id != -1) {
        mp_raise_ValueError(MP_ERROR_TEXT("Timer doesn't exist"));
    }

    if (n_args > 0 || n_kw > 0) {
        // Start the timer
        mp_map_t kw_args;
        mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
        machine_timer_init_helper(self, n_args, args, &kw_args);
    }

    return MP_OBJ_FROM_PTR(self);

```

Anyway, the soft timer should be fine for what you want. That gets dispatched from a hardware timer that’s keeping the whole system time for \_ms, \_us, etc.

This is different from PYB where you were interfacing directly with hardware timers. In the machine module to make things more abstract this has changed. Hardware timers are still used to control PWM. But, it’s also abstracted so that you’re just asking for PWM versus a timer and all the modes to generate PWM.
