• I have a project where I'd like to pulse a buzzer. right now i'm using setInterval as I read that digitalPulse should only be used for short durations (in the microseconds). I also recently realized that pwm can be used for this purpose.

    the one thing i dont like about setInterval is that it makes the code harder to read with various nested intervals if I want to have different buzz patterns. why does it say digitalpulse should only be used for short intervals? is there some problem if I want to have it on for a second? or a half second?
    which of these will yield best power utilization as well? it's not super-critical but if one is 10x more power than another, i'll do the power efficient one.


  • It depends a little on your board what's better but basically:

    • setInterval uses the real-time clock (mainly). The CPU can totally go to sleep while it's waiting for the timer, but it's a bit less accurate.
    • digitalPulse uses a dedicated hardware timer. It's more accurate but the CPU's clock has to be on all the time for it to work (even if the CPU itself is sleeping).

    Honestly, digitalPulse will be totally fine to use for what you're doing, since I guess the buzzer won't be on that often.

    However nothing would stop you from writing a little utility function that used setTimeout in a more digitalPulse-y style if you really wanted, which could really tidy up your code.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

digitalPulse, pwm or setInterval for flashing/beeping?

Posted by Avatar for MikeD @MikeD