🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Frequency measurement with hardware interrupt handler

    UIFlow
    5
    8
    10.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      Amani4791
      last edited by

      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:
      counter+=1

      pin0.irq(trigger=machine.Pin.IRQ_FALLING,
      handler= event_interrupt)*

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

      IAMLIUBOI 1 Reply Last reply Reply Quote 0
      • A
        Amani
        last edited by

        Anybody, anyhow?

        1 Reply Last reply Reply Quote 0
        • IAMLIUBOI
          IAMLIUBO @Amani4791
          last edited by

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

          A 1 Reply Last reply Reply Quote 0
          • A
            Amani @IAMLIUBO
            last edited by

            @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?...

            ajb2k3A 1 Reply Last reply Reply Quote 0
            • A
              Amani
              last edited by

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

              1 Reply Last reply Reply Quote 0
              • ajb2k3A
                ajb2k3 @Amani
                last edited by ajb2k3

                @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

                UIFlow, so easy an adult can learn it!
                If I don't know it, be patient!
                I've ether not learned it or am too drunk to remember it!
                Author of the WIP UIFlow Handbook!
                M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

                1 Reply Last reply Reply Quote 0
                • A
                  Amani
                  last edited by

                  @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?...

                  1 Reply Last reply Reply Quote 0
                  • stanelyS
                    stanely
                    last edited by

                    @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, https://www.ti.com/lit/ds/symlink/lm331.pdf

                    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.

                    Thanks

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post