Can you use Pin.callback(...) to invoke deepsleep.go_to_sleep(...)?



  • Hello,

    Is it possible to invoke deepsleep.go_to_sleep() form a Pin.callback()?

    Look at this code:

    from machine import Pin, Timer
    from deepsleep import DeepSleep
    import deepsleep
    
    
    def handler(button):
       ds.enable_pullups('P18')  # can also do ds.enable_pullups(['P17', 'P18'])
       ds.enable_wake_on_fall('P18') # can also do ds.enable_wake_on_fall(['P17', 'P18'])
       print("Button pushed")
       ds.go_to_sleep(20)
    
    
    ds = DeepSleep()
    
    # get the wake reason and the value of the pins during wake up
    wake_s = ds.get_wake_status()
    print(wake_s)
    
    if wake_s['wake'] == deepsleep.PIN_WAKE:
       print("Pin wake up")
    elif wake_s['wake'] == deepsleep.TIMER_WAKE:
       print("Timer wake up")
    else:  # deepsleep.POWER_ON_WAKE:
       print("Power ON reset")
    
    
    button=Pin("G17",Pin.IN, pull=Pin.PULL_UP)
    irq=button.callback(trigger=button.IRQ_FALLING, handler=handler)
    
    print('booted---')
    
    

    This code does invoke the deepsleep but the SiPy does not wake up anymore (only pressing the reset button helps)

    It did work at some point though, just bringing 'P18' low to wake it up did not work, but let's solve that in another post.




  • Hello,

    Thank you for pointing that out.

    I changed the pins to G30 (sleep) and G13 (wakeup) and now the system works fine.

    So, the new code is:

    from machine import Pin, Timer
    from deepsleep import DeepSleep
    import deepsleep
    
    
    def handler(button):
        ds.enable_pullups('G31')  # can also do ds.enable_pullups(['P17', 'P18'])
        ds.enable_wake_on_fall('G31') # can also do ds.enable_wake_on_fall(['P17', 'P18'])
        print("Button pushed")
        ds.go_to_sleep(20)
    
    
    ds = DeepSleep()
    
    # get the wake reason and the value of the pins during wake up
    wake_s = ds.get_wake_status()
    print(wake_s)
    
    if wake_s['wake'] == deepsleep.PIN_WAKE:
        print("Pin wake up")
    elif wake_s['wake'] == deepsleep.TIMER_WAKE:
        print("Timer wake up")
    else:  # deepsleep.POWER_ON_WAKE:
        print("Power ON reset")
    
    
    button=Pin("G30",Pin.IN, pull=Pin.PULL_UP)
    irq=button.callback(trigger=button.IRQ_FALLING, handler=handler)
    
    print('booted---')
    


  • @ddecoene
    Unfortunately G17 is the pin used to communicate between the SiPy and the the deep sleep shield so setting it as a pull up and/or pressing the button affects that communication and so the wake isn’t properly set anymore.

    I guess you could try disabling the pull up and also waiting for the button to released before calling the ds. functions but not sure if it would be reliable.



3 out of 3

Pycom on Twitter