Push Buttons and Switches

The WEMOS S3 MINI PRO includes three onboard push buttons that can be used as digital inputs.

They are useful for learning:

    • Digital input

    • Reading GPIO states

    • Pull-up and pull-down logic

    • Button polling

    • Switch bouncing

    • Software debouncing

    • Interrupts

    • Controlling other onboard devices


1. Onboard Button Connections

Button GPIO
Button 1 GPIO0
Button 2 GPIO47
Button 3 GPIO48


2. Digital Input

A digital input normally has two states:

HIGH
LOW

or:

1
0


3. Configuring a Button in MicroPython

from machine import Pin

button = Pin(47, Pin.IN)

Read the state using:

button.value()


4. Pull-Up and Pull-Down Resistors

Example using a pull-up:

from machine import Pin

button = Pin(47, Pin.IN, Pin.PULL_UP)

With active-low logic:

Released -> 1
Pressed -> 0


5. Reading a Button

from machine import Pin
import time button = Pin(47, Pin.IN, Pin.PULL_UP) while True:
print(button.value())
time.sleep_ms(100)


6. Button Polling

while True:
if button.value() == 0:
print("Button pressed")


7. Switch Bouncing

A mechanical button may temporarily behave like:

0 -> 1 -> 0 -> 1 -> 0 -> 1

before settling.

This is called switch bouncing.


8. Software Debouncing

from machine import Pin
import time button = Pin(47, Pin.IN, Pin.PULL_UP) while True:
if button.value() == 0:
print("Button pressed")
time.sleep_ms(50) while button.value() == 0:
pass


9. Detecting a Single Press

from machine import Pin
import time button = Pin(47, Pin.IN, Pin.PULL_UP) previous_state = 1 while True:
current_state = button.value() if previous_state == 1 and current_state == 0:
print("Button pressed") previous_state = current_state
time.sleep_ms(10)


10. Using Buttons to Control Other Devices

Buttons can be used to:



    • Turn an LED on or off

    • Change RGB LED colours

    • Change display screens

    • Start an animation

    • Increase or decrease a value

    • Move an object in a game

    • Select a menu option


11. Example: Button Controlling an LED

from machine import Pin
import time
import s3minipro button = Pin(47, Pin.IN, Pin.PULL_UP) while True:
if button.value() == 0:
s3minipro.rgb_led(255, 0, 0)
else:
s3minipro.rgb_led(0, 0, 0) time.sleep_ms(20)


12. Interrupts

from machine import Pin

button = Pin(47, Pin.IN, Pin.PULL_UP)

def button_pressed(pin):
print("Button pressed") button.irq(
trigger=Pin.IRQ_FALLING,
handler=button_pressed
)


13. Avoiding Unnecessary Global Variables

def read_button():
state = button.value()
return state


14. Exercises

Possible topics:



    • Reading button states

    • Detecting press and release

    • Button polling

    • Software debouncing

    • Detecting a single press

    • Using multiple buttons

    • Controlling LEDs with buttons

    • Using buttons with the display

    • Interrupt-driven input


15. Possible Modifications



    • Use one button to cycle through RGB colours

    • Use two buttons to increase/decrease a value

    • Navigate menus

    • Move objects on the display

    • Compare debounce methods

    • Use interrupts instead of polling


16. Key Lesson

Physical Button Press
|
v
GPIO Input
|
v
MicroPython Program
|
v
Program Decision
|
v
LED / Display / Other Device
Topic revision: r1 - 19 Nov 2026, JamesInkoom