como-leer-pulsador-con-micropython

How to Read a Button with MicroPython

  • 6 min

In this MicroPython tutorial, we will continue learning how to interact with basic hardware, seeing how to perform reading a button as a digital input.

A button is an electronic component that allows or prevents the flow of electric current when pressed.

In its normal state (not pressed), the button can be open (does not conduct current) or closed (conducts current).

In a microcontroller, we can use a digital input to detect whether the button is pressed or not. Which, in principle, is very common and quite usual, but has a couple of “little things” we need to know.

So let’s see how to read a button or a switch with a digital pin in MicroPython.

Configuring a GPIO Pin as Input

As we saw in the previous post, in MicroPython GPIO pins are configured using the Pin class from the machine module.

Let’s remember, in summary, that to configure a pin as input, we use the Pin.IN mode. Then we can read its state using the value() method (without parameters).

from machine import Pin

# Configure pin 5 as input
button = Pin(5, Pin.IN)

state = button.value()
Copied!

The value() method

  • Returns 1 if the pin is in HIGH state (button pressed)
  • 0 if it is in LOW state (button not pressed).

Pull-Up and Pull-Down Resistors

When working with digital inputs, it is necessary to ensure that the pin has a defined state when the button is not pressed.

For this, we use pull-up or pull-down resistors.

  • Pull-Up Resistor: Connects the pin to a high voltage (VCC) through a resistor. When the button is not pressed, the pin reads HIGH. When pressed, the pin is connected to ground (GND) and reads LOW.
  • Pull-Down Resistor: Connects the pin to ground (GND) through a resistor. When the button is not pressed, the pin reads LOW. When pressed, the pin is connected to VCC and reads HIGH.

Many devices include internal pull-up or pull-down resistors. We can activate them from MicroPython, using the Pin.PULL_UP or Pin.PULL_DOWN modes.

# Configure pin 5 as input with internal pull-up resistor
button = Pin(5, Pin.IN, Pin.PULL_UP)
Copied!

In this case, when the button is not pressed, the pin will read HIGH. When pressed, it will read LOW.

Handling Button Bounce

A common headache when working with buttons is bounce. When a button is pressed or released, the contacts often generate multiple rapid transitions between HIGH and LOW before stabilizing (which will ruin your measurement).

To handle bounce, we can implement a simple software debounce. A simple way to do this is to introduce a small delay after detecting a press.

from machine import Pin
import time

# Configure pin 5 as input with internal pull-up resistor
button = Pin(5, Pin.IN, Pin.PULL_UP)

# Variable to store the last state of the button
last_state = button.value()

while True:
    current_state = button.value()
    if current_state != last_state:
        time.sleep(0.05)  # Small delay to avoid bounce
        current_state = button.value()  # Read state again
        if current_state == 0:  # Button pressed (LOW due to pull-up)
            print("Button pressed")
        else:
            print("Button not pressed")
        last_state = current_state
    time.sleep(0.01)  # Small pause to avoid repeated readings
Copied!

In this code, after detecting a change in the button state, we wait 50 ms before reading the state again. This helps avoid false detections due to bounce.

There are other ways to manage bounce. This one is simple, and therefore very used, but for complex applications better methods may be necessary.

Practical Examples