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

    Wake machine.deepsleep using pin input from accelerometer [SOLVED]

    Scheduled Pinned Locked Moved WiPy 2&3
    deepsleepinterruptpinaccelerometer
    5 Posts 2 Posters 1.4k Views 3 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

      I'm trying to create an accelerometer interrupt for machine.deepsleep(). From what I can tell there is no built-in function for that. Since deep sleep can be interrupted with a pin I was thinking that there might be a pin that allows the accelerometer to relay an interrupt to the device.

      I've tried P13, which is denoted as "Accelerometer Interrupt" on the Pytrack pinout but I didn't get a response (even when the device was not asleep). Here is the code I was using

      from machine import Pin
      ​
      def pin_handler(arg):
          print("got an interrupt in pin %s" % (arg.id()))
      ​
      p_in = Pin('P13', mode=Pin.IN, pull=Pin.PULL_UP) 
      p_in.callback(Pin.IRQ_FALLING, pin_handler)
      

      Any ideas or suggestions would be great!

      My system specs:

      (sysname='WiPy', nodename='WiPy', release='1.18.0.r1', version='v1.8.6-849-9569a73 on 2018-07-20', machine='WiPy with ESP32', pybytes='0.9.0')
      
      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @alexpul
        last edited by

        @alexpul Did you configure the accelerometer (enable it, set it to monitor events and send interrupts)?

        You need to at least instantiate the LIS2HH12 class, and call the enable_activity_interrupt method.

        There may be more stuff to do. Check the sources of the PyTrack library for more information.

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

          @jcaron This is the code I've tried to test if the accelerometer can wake from machine.deepsleep()

          from LIS2HH12 import LIS2HH12
          import pycom
          import machine
          import utime
          
          pycom.heartbeat(False)
          
          def accel_activity_handler(pin_o):
              pycom.rgbled(0x800080)
              utime.sleep_ms(200)
              pycom.heartbeat(False)
          
          ## ACCELEROMETER INTERRUPT
          acc = LIS2HH12()
          acc.enable_activity_interrupt(1000, 200, handler=accel_activity_handler)
          
          machine.deepsleep(100*1000)
          

          When I shake the device nothing happens...

          (Note that this script does allow for a deep sleep interrupt but only when pytrack.go_to_sleep() is used to enter deep sleep. I need to use machine.deepsleep() though..)

          J 1 Reply Last reply Reply Quote 0
          • J Offline
            jcaron @alexpul
            last edited by

            @alexpul In that code you don't enable wake-on-pin, and you don't do anything visible before going back to sleep, so even if it wakes up you won't see anything...

            Note that as far as I know, the handler will only be called if the interrupt happens while the device is awake, not during sleep.

            Start by using acc.enable_activity_interrupt without any deep sleep to see if the interrupt gets triggered (i.e. just remove the machine.deepsleep in your code).

            Once you have confirmed that, you can add the wake-on-pin, and do something visible when you wake up before going back to sleep.

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

              Ah ha. I was using the wrong interrupt script.

              This is the correct working code for an accelerometer interrupt (P13) of machine.deepsleep():

              import machine
              machine.pin_deepsleep_wakeup(['P13'], machine.WAKEUP_ANY_HIGH, False)
              machine.deepsleep(10*1000)
              

              So for pin interrupts there is a callback() function to create interrupts of active and sleep modes but the pin_deepsleep_wakeup() is needed to interrupt deep sleep (is that what you meant by "wake-on-pin" @jcaron?). There is no interrupt handler for pin_deepsleep_wakeup() because the working memory is cleared after deep sleep (as mentioned by @jcaron below).

              @jcaron I had tested my interrupt handler without sleep and it did work. In my main.py I had the following script to test that it woke from deep sleep and ran main.py.

              import pycom
              import utime
              
              pycom.heartbeat(False)                      #turn off LED
              pycom.rgbled(0xFF0000)                    #red LED
              utime.sleep(2)
              pycom.heartbeat(False)                      #turn off LED
              
              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