Micropython Basics: Digital Output
Table of Contents
Before working with sensors or complex hardware, it’s important to master digital outputs. A digital output allows your microcontroller to control external components by switching its GPIO pins ON (HIGH) or OFF (LOW).
The most common way to start learning digital output is through an LED — a simple visual indicator that helps you confirm your program’s logic and timing.
Understanding Digital Output
Digital output pins on your microcontroller can send two voltage levels:
- HIGH (1): The pin outputs a voltage (usually 3.3V or 5V, depending on the board).
- LOW (0): The pin outputs 0V (ground).
By controlling these voltage levels, MicroPython can turn components on or off — such as lighting up an LED, activating a buzzer, or triggering a relay module.
Basic LED Circuit
To demonstrate digital output, you’ll need:
- 1 × MicroPython board (ESP32, ESP8266, or Raspberry Pi Pico)
- 1 × LED
- 1 × 330 Ω resistor (to limit current)
- Breadboard and jumper wires
Wiring Instructions
Connect the components as follows:
- LED anode (long leg) → GPIO pin (e.g., GP15)
- LED cathode (short leg) → resistor → GND
This setup ensures that when the GPIO pin goes HIGH, current flows through the LED, causing it to light up.
Controlling an LED in MicroPython
You can control the LED by configuring a GPIO pin as an output using the Pin
class from the
machine
module.
from machine import Pin
import time
led = Pin(15, Pin.OUT)
while True:
led.on() # Turn LED on
time.sleep(1)
led.off() # Turn LED off
time.sleep(1)
This code toggles the LED on and off every second — your first MicroPython “blinking LED” program!
on()
and off()
methods make your code more readable, but you can also use
led.value(1)
and led.value(0)
for more explicit control.
Understanding the Logic
When you set the output pin HIGH
, it sources voltage to the LED circuit.
When you set it LOW
, it sinks current to ground, turning the LED off.
Some boards have onboard LEDs already wired to a specific GPIO pin (often labeled as “LED” or “BUILTIN”). You can use that pin to run your first test without external components.
from machine import Pin
import time
led = Pin("LED", Pin.OUT) # Works on Raspberry Pi Pico and some ESP boards
while True:
led.toggle() # Alternate between on/off automatically
time.sleep(0.5)
toggle()
method is convenient for blinking patterns or status indicators
without manually tracking the LED state.
Expanding to Multiple Outputs
You can control multiple LEDs (or other digital devices) at once by creating multiple Pin
objects.
from machine import Pin
import time
led1 = Pin(15, Pin.OUT)
led2 = Pin(16, Pin.OUT)
while True:
led1.on()
led2.off()
time.sleep(0.5)
led1.off()
led2.on()
time.sleep(0.5)
This example alternates between two LEDs — an excellent introduction to sequencing and pattern generation.
Common Digital Output Devices
- Relays: Switch high-power devices like lamps or motors.
- Buzzers: Produce tones or alerts when powered.
- Laser Diodes / IR Emitters: Used in communication or detection projects.
- Indicators: Visual feedback systems (status LEDs, signal lights).
Testing and Troubleshooting
- Check your wiring and resistor orientation.
- Ensure the correct GPIO pin number is used in your code.
- Confirm that the pin supports digital output (some are input-only).
- Try using
led.value(1)
andled.value(0)
manually in the REPL to test responsiveness.
Summary
Digital outputs form the foundation of MicroPython hardware control. You learned how to configure output pins, manage logic levels, and safely drive simple devices like LEDs.
With this knowledge, you can now build more dynamic behaviors — blinking sequences, indicators, or feedback systems for sensors and automation projects.
Next, we’ll explore analog inputs (ADC) — how to read variable signals from sensors such as potentiometers, light sensors, and temperature probes.