button_press_once.py

Here we use a more complex if structure to identify a button press: we check to see if the button is currently pressed and if the current value of the button is different than the previous value (prev_val). Send the following example to your ESP32.

Code

'''
button_once.py
'''

from machine import Pin
from time import sleep_ms

button = Pin(12, Pin.IN, Pin.PULL_UP)
prev_val = button.value()

while True:
    if not button.value() and button.value() is not prev_val:
        print('Button pressed once!')
    prev_val = button.value()
    sleep_ms(20)

Take a moment to run and use button_press_once.py while asking the following question: can I reliably press the button and get only one Button pressed! message? Try it!

button_count.py

Now that we can reliably count a button press without repeat triggers we can rewrite button_once.py to count each button press. Send the following to your ESP32.

Code

'''
button_count.py
'''

from machine import Pin
from time import sleep_ms

button = Pin(12, Pin.IN, Pin.PULL_UP)
prev_val = button.value()
count = 0

while True:
    if not button.value() and button.value() is not prev_val:
        count+=1
        msg = ' '.join(['Button pressed', str(count), 'times!'])
        print(msg)
    prev_val = button.value()
    sleep_ms(20)

Take a moment to run and use button_count.py while asking the following, more interesting, question: can I reliably count four button presses? Try it!

button_count_limit.py

'''
button_count_limit.py
'''

from machine import Pin
from time import sleep_ms

button = Pin(12, Pin.IN, Pin.PULL_UP)
prev_val = button.value()
limit = 5
count = 0

while True:
    if not button.value() and button.value() != prev_val:
        count += 1
        msg = ' '.join(['Button pressed', str(count), 'times!'])
        print(msg)
        if count == limit:
            print('limit reached!')
            count = 0
    prev_val = button.value()
    sleep_ms(20)