Frequency measurement with hardware interrupt handler

  • Hi there!

    I need to measure frequency output of a flow meter 0-1000Hz.
    (Core2, Pin GPIO27)
    I have setup some code in UIFlow and defined IRQ and handler in "Execute Code" block.
    Reading works but very unstable (Jumps between 300..1000).
    Probably I need to run the handler in IRAM?
    Where/ how can I put the IRAM_ATTR?

    My execute code block:
    *def event_interrupt(pin0):
    global counter, semaphore
    if Semaphore:

    handler= event_interrupt)*

    Thanks a lot!
    0_1630190352278_2021-08-29 00_38_29-M5Flow.png

  • Anybody, anyhow?

  • Hi @amani4791 ,
    Did you try download to device and run it?

  • @iamliubo
    Yes, I ran it on Core2.
    It measures frequency but very unstable.
    I meanwhile found that it's better if I introduce a wait loop. Looks like writing to the display consumes too much time.
    But I think there should be a possibility to use iram. It shouldn't happen that such a relatively "slow" signal can be missed?...

  • Somewhere I read that it's possible to use the "button" element to get interrupt: but how to link GPIO to the button?

  • @amani He means did you save it to the Core2 so that it loads when the core2 powers up and not just press the > button.
    Have you looked under the Hardwares>pin menu and tried with these blocks?0_1632510223551_Screenshot 2021-09-24 at 20.03.25.png

  • @ajb2k3 : thanks for clarification. I really only used "play" button 😬
    So after donloading and booting directly from Core2 it performed better.
    But after I reintroduced temperature sensor reading (i2c) it was again very unstable.
    To me it looks like there is not really interrupt driven counting activated...
    (If I e.g. load the frequency measurement only and only update LCD w/o delay the frequency reading is totally off (too small count). With some delay it is better. But with some more workload it is not reliable that way?...

  • @Amani , I also need to measure frequency using an M5 Stack Core 2. I was looking at your code and wondering if you ever solved this problem? I see that you're using a counter that's incremented in an interrupt handler. How do you get the frequency from that?

    The way I understand Micro Python interrupts is that they're soft and give you jitter of some milliseconds, like over 2. Because the M5 Stack UIFlow runs on top of Micro Python, it does not seem likely that interrupts with adequate resolution are possible. You'll be lucky to measure a 500Hz signal.

    But I'm very interested to hear if you came up with a solution.

    I'm planning to solve this problem with a frequency to voltage converter. TI makes a low voltage part, LM331. The output is a voltage proportional to frequency that can be input to an ADC pin. Here's a link to the datasheet,

    I don't see another way to solve this problem using an M5 Stack. I'm hoping you found one that you can tell me about.