Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Accelerometer always produces two interrupts [SOLVED]

    Scheduled Pinned Locked Moved Getting Started
    accelerometerpytrackinterrupt
    5 Posts 2 Posters 1.1k Views 2 Watching
    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 Offline
      alexpul
      last edited by alexpul

      When I use the accelerometer on my WiPy3 and Pytrack to produce an interrupt, it always produces at least two interrupts in a row even when I just bump it in one direction. I assume this comes from the acceleration and then deceleration of the board. Does anyone know how to produce just one accelerometer interrupt? My thoughts were either to (1) only allow and acceleration (+) to produce an interrupt or (2) to clear the queue of interrupts once one has been detected - but I don't know how to achieve either.

      Here is the interrupts code I'm working with:

      from pytrack import Pytrack
      from LIS2HH12 import LIS2HH12
      import pycom
      import time
      import machine
      import utime
      
      py = Pytrack()
      
      pycom.heartbeat(False)
      
      py.setup_int_pin_wake_up(False)
      py.setup_int_wake_up(True, True)
      
      int_counter = 0
      
      def accel_activity_handler(pin_o):
          pycom.rgbled(0x800080)
          print('saw the interrupt')
          
          global int_counter
          int_counter+=1
          print(int_counter)
      
          pycom.heartbeat(False)
      
      ## ACCELEROMETER INTERRUPT
      acc = LIS2HH12()
      acc.enable_activity_interrupt(1000, 200, handler=accel_activity_handler)
      

      And the output from one bump of the board producing two interrupts:

      >>> saw the interrupt
      1
      saw the interrupt 
      2
      
      1 Reply Last reply Reply Quote 0
      • D Offline
        dmayorquin
        last edited by

        Hi @alexpul, you are activating the activity and inactivity interrupts of the accelerometer:

        py.setup_int_wake_up(True, True)
        

        According to your acc.enable_activity_interrupt() settings, the Pytrack generates an interruption after 200 ms of inactivity and another interruption if the motion goes beyond 1000 mg.

        If you just want to detect the motion, use:

        py.setup_int_wake_up(True, False)
        

        This enables activity detection only. Hope this works.

        A 1 Reply Last reply Reply Quote 1
        • A Offline
          alexpul @dmayorquin
          last edited by

          @dmayorquin Great tip, I didn't see that!

          However, when I changed that line there was no change in the board behaviour (still two interrupts)...
          Even when I tried

          py.setup_int_wake_up(False, False)
          

          (and resetting the board to clear past handlers) there were still two interrupts...Thoughts?

          D 1 Reply Last reply Reply Quote 0
          • D Offline
            dmayorquin @alexpul
            last edited by

            @alexpul I think I've found the problem. In the LIS2HH12.py module, the line #148 says:

            self.int_pin.callback(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=self._int_handler)
            

            which detects both rising and falling edge of the interruption. If you check the code of the function begining at line #161:

            def _int_handler(self, pin_o):
            

            you will see that the state of pin_o is evaluated (True for activity and False for inactivity). So, one solution is to modify your handler in main.py:

            def accel_activity_handler(pin_o):
                if pin_o():
                    pycom.rgbled(0x800080)
                    print('saw the interrupt')
            
                    global int_counter
                    int_counter+=1
                    print(int_counter)
                    time.sleep(1)
                    pycom.rgbled(0)
            

            Another solution is to change the line #148 in LIS2HH12.py so it rises just one interruption related to activity detection:

            self.int_pin.callback(trigger=Pin.IRQ_FALLING, handler=self._int_handler)
            
            A 1 Reply Last reply Reply Quote 2
            • A Offline
              alexpul @dmayorquin
              last edited by

              @dmayorquin Amazing! Both of those solutions worked for me. Thanks for your help :)

              1 Reply Last reply Reply Quote 0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better đź’—

              Register Login
              • 1 / 1
              • First post
                Last post

              Pycom on Twitter

              Pycom