Learn electronics, coding, and projects — step by step.

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.

✅ Example Devices: LEDs, relays, buzzers, laser diodes, or even transistors and motor drivers that use logic-level signals.

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.

💡 Tip: Always use a resistor with your LED to prevent excessive current draw, which can damage both the LED and your microcontroller.

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!

⚙️ Note: The 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)
✅ Tip: The 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).
💡 Best Practice: Use a transistor or MOSFET as a driver when controlling devices that draw more current than a GPIO pin can supply. GPIOs typically handle around 12–20 mA safely.

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) and led.value(0) manually in the REPL to test responsiveness.
⚠️ Warning: Never connect high-current loads (like motors or solenoids) directly to GPIO pins. Always use a driver circuit or relay module to protect your board.

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.

×



Related Articles: (by Series)