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

Micropython Basics: Key Micropython Modules

Table of Contents

MicroPython comes with a collection of built-in modules that make it easy to interact with hardware, manage timing, connect to networks, and even send data to the internet. Understanding these modules is essential to writing practical MicroPython programs for embedded systems.

Here are some of the most commonly used modules you’ll encounter:

machine — Hardware Control

The machine module provides access to the hardware-specific features of your microcontroller. It allows you to work directly with GPIO pins, timers, ADC, PWM, I²C, SPI, and UART interfaces.

Some of the most useful classes and methods include:

  • Pin — for controlling digital input/output pins
  • ADC — for reading analog values
  • PWM — for generating analog-like output signals
  • I2C / SPI — for communication with sensors and peripherals
  • UART — for serial communication

from machine import Pin, ADC, PWM

led = Pin(2, Pin.OUT)     # Configure GPIO2 as output
sensor = ADC(Pin(34))     # Configure ADC pin on ESP32
pwm = PWM(Pin(15))        # PWM on GPIO15
pwm.freq(1000)

The machine module is the foundation of MicroPython hardware programming — almost every hardware interaction you perform will start here.

💡 Tip: The machine module varies slightly between boards. For example, some pins or features may not be available on ESP8266 but exist on ESP32. Always check your board’s documentation for supported peripherals.

time — Delays and Timing

The time module provides basic functions to control delays, measure elapsed time, and schedule actions. It’s especially useful in embedded systems where you often need to pause execution or perform tasks at regular intervals.


from time import sleep, ticks_ms, ticks_diff

# Delay example
print("LED on")
sleep(1)
print("LED off")

# Timing example
start = ticks_ms()
# ... do something ...
duration = ticks_diff(ticks_ms(), start)
print("Operation took:", duration, "ms")

The sleep() function halts execution for the given number of seconds, while ticks_ms() and ticks_diff() are used to track precise timing intervals in milliseconds.

⚙️ Note: MicroPython also provides sleep_ms() and sleep_us() for millisecond and microsecond delays, respectively. These are especially useful for accurate sensor timing or communication protocols.

network — Wi-Fi Connectivity

The network module handles all network-related operations, such as connecting to Wi-Fi networks or configuring your board as an access point. This is a core feature of ESP32 and ESP8266 boards, which include built-in Wi-Fi radios.


import network

wlan = network.WLAN(network.STA_IF)  # Station mode (connect to router)
wlan.active(True)
wlan.connect("MyWiFi", "password123")

# Wait until connected
while not wlan.isconnected():
    pass

print("Connected! IP:", wlan.ifconfig()[0])

Once connected, your board has full internet access — you can send HTTP requests, use MQTT, or host a web server.

✅ Example: Use network.AP_IF to set up your board as an Access Point instead of connecting to an existing router. This is useful for offline or IoT setups.

urequests — Simple HTTP Requests

The urequests module is a lightweight version of Python’s requests library, designed for microcontrollers. It allows you to send HTTP GET or POST requests to APIs or web services, making your MicroPython board capable of interacting with cloud platforms.


import urequests

response = urequests.get("https://api.github.com")
print(response.status_code)
print(response.text)
response.close()

You can also send data using POST requests:


data = {"sensor": "temperature", "value": 26.5}
response = urequests.post("https://example.com/data", json=data)
print("Response:", response.text)
response.close()
⚠️ Warning: The urequests module is not included by default on all MicroPython builds. If you encounter an ImportError, use upip or copy the urequests.py file manually to your board’s filesystem.

Other Useful Modules

While the above are the most common, MicroPython also provides several additional modules:

  • os — manage files and directories on the board’s filesystem
  • sys — access system information and runtime parameters
  • json — encode and decode JSON data (useful for IoT APIs)
  • socket — create raw network connections or simple web servers
  • gc — control garbage collection to free up memory
💡 Tip: You can explore available modules interactively from the REPL using:
help('modules')
This command lists all built-in and installed modules currently available on your board.

Together, these modules form the backbone of MicroPython programming — allowing you to control hardware, manage time, connect to Wi-Fi, and communicate with online services. As you build more advanced projects, you’ll find yourself combining these modules frequently to create fully interactive, connected devices.

×



Related Articles: (by Series)