Micropython Basics: Digital Inputs
Table of Contents
Now that you can save and manage your scripts, it’s time to interact with the real world using digital inputs. One of the simplest and most common examples is reading the state of a push button to control an LED.
In MicroPython, digital inputs and outputs are handled through the machine
module,
which provides access to the hardware pins of your microcontroller.
What Are Digital Inputs?
A digital input reads either a HIGH or LOW signal (1 or 0). These two states correspond to:
- HIGH (1): Voltage is present on the pin (usually 3.3V or 5V).
- LOW (0): Voltage is absent or connected to ground (0V).
Buttons, switches, and sensors (like infrared detectors) often output these digital signals. When pressed or triggered, they change the signal level, which MicroPython can read and respond to.
Hardware Setup: Button and LED
You’ll need the following components for this example:
- 1 × MicroPython-compatible board (ESP32, ESP8266, or Raspberry Pi Pico)
- 1 × Push button
- 1 × LED
- 1 × 330 Ω resistor (for the LED)
- 1 × 10 kΩ pull-down resistor (optional if not using internal pull-up)
- Breadboard and jumper wires
Wiring Guide
Connect the components as follows:
- LED anode (long leg) → GPIO pin (e.g., GP15)
- LED cathode (short leg) → resistor → GND
- Button one leg → GPIO pin (e.g., GP14)
- Button other leg → 3.3V (or use a pull-down resistor to GND)
When the button is pressed, the pin connected to it reads HIGH. When released, it reads LOW.
How MicroPython Reads Digital Inputs
MicroPython uses the Pin
class from the machine
module to configure a GPIO pin
as either an input or output.
For digital inputs, you can specify whether the pin should use an internal pull-up or pull-down resistor:
from machine import Pin
# Configure a pin as an input with internal pull-down resistor
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
# Read the state of the pin
state = button.value()
print(state)
The .value()
method returns 1
when the button is pressed (HIGH) and 0
when released (LOW).
Combining Input and Output
You can combine a button input and an LED output to make a simple interactive project. For example, when the button is pressed, the LED turns on; when released, it turns off.
from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value() == 1:
led.on()
else:
led.off()
time.sleep(0.05)
This loop constantly checks the button state and controls the LED accordingly.
The short delay (0.05s
) helps reduce flickering.
Debouncing Buttons
Debouncing ensures that only one input is registered per button press. You can implement it by adding a small delay after a button is detected, or by checking if the state remains stable over time.
For example, you might read the button state twice with a short delay in between, and only accept the press if both readings are identical.
machine.Timer
class or event-based callbacks
with interrupts (Pin.IRQ_FALLING
and Pin.IRQ_RISING
).
Testing and Troubleshooting
- Make sure your button wiring matches the pin configuration (pull-up or pull-down).
- If the LED doesn’t respond, double-check your GPIO pin numbers.
- Use the REPL to print out
button.value()
to confirm input readings.
Summary
In this section, you learned how MicroPython handles digital inputs using the machine.Pin
class.
You built a simple button-controlled LED circuit and understood how HIGH/LOW logic works on GPIO pins.
This foundation is essential for building interactive projects — next, we’ll explore how to work with analog inputs to read variable signals such as those from potentiometers or sensors.