""" Pico micropython example, blinking leds when a button is clicked. Demonstrates using lightsleep() for low power usage. Should work on pico and pico2. - Nick Parlante This code is placed in the public domain, free for any use. Button is on 17 LED is on 16 xVsysG3.3 1918G1716 ------------------- u| ------------------- 1213G1415 """ import machine from machine import Pin import utime # Pins held high, ground to activate BUTTON = 17 DEV = 18 LED1 = 25 LED2 = 16 # True = output some text per button click # showing that serial output works PRINT = False # Use pull-up .. avoid pull_down errata, ground on click, # so .value() == 0 -> clicked button = Pin(BUTTON, Pin.IN, Pin.PULL_UP) dev_mode = Pin(DEV, Pin.IN, Pin.PULL_UP) led1 = Pin(LED1, Pin.OUT) led2 = Pin(LED2, Pin.OUT) led1.low() led2.low() # Count number of clicks to relay in printing click_count = 0 def blink_led(led, delay_ms): "Blink led twice with given delay." for i in range(2): led.high() utime.sleep_ms(delay_ms) led.low() utime.sleep_ms(delay_ms) # lightsleep for the given ms, unless dev mode, # in which use regular utime sleep def dev_sleep(ms): "Sleep, either regular or lightsleep() depending on dev mode." if dev_mode.value() == 0: utime.sleep_ms(ms) else: machine.lightsleep(ms) # Fast blink at boot blink_led(led1, 50) if PRINT: print('start low power blink ticks: {}'.format(utime.ticks_ms())) while True: # If button is clicked -> do something, if button.value() == 0: blink_led(led1, 100) blink_led(led2, 100) click_count += 1 if PRINT: print('button click: {} ticks: {}'.format(click_count, utime.ticks_ms())) # Sleep 50 ms, then loop around to check again. # 50 ms is fast enough to detect clicks while saving significant power. dev_sleep(50)