From 7261e7ba4f63c435325d541f20cab39cd70a3936 Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:43:24 +0000 Subject: [PATCH] Add micropython materials folder Code for the updated MicroPython tutorial: blink, PWM blink, and PWM fade. Co-Authored-By: Claude Opus 5.5 --- micropython/README.md | 33 +++++++++++++++++++++++++++++++++ micropython/blink.py | 15 +++++++++++++++ micropython/pwm_blink.py | 10 ++++++++++ micropython/pwm_fade.py | 21 +++++++++++++++++++++ micropython/requirements.txt | 2 ++ 5 files changed, 81 insertions(+) create mode 100644 micropython/README.md create mode 100644 micropython/blink.py create mode 100644 micropython/pwm_blink.py create mode 100644 micropython/pwm_fade.py create mode 100644 micropython/requirements.txt diff --git a/micropython/README.md b/micropython/README.md new file mode 100644 index 0000000000..e3d269aa31 --- /dev/null +++ b/micropython/README.md @@ -0,0 +1,33 @@ +# MicroPython: An Intro to Programming Hardware in Python + +This folder provides the code examples for the Real Python tutorial [MicroPython: An Intro to Programming Hardware in Python](https://realpython.com/micropython/). + +The scripts run on a microcontroller board running MicroPython 1.29.0, such as a Raspberry Pi Pico, an ESP32, or an ESP8266. They don't run on your computer, because they use MicroPython's `machine` module. + +## Setup + +Install MicroPython on your board as described in the tutorial. Then, install the command-line tools on your computer: + +```console +$ python -m pip install -r requirements.txt +``` + +This installs `mpremote` for talking to your board and `esptool` for flashing ESP boards. + +## Usage + +Connect your board over USB and run a script on it with `mpremote`: + +```console +$ mpremote run blink.py +``` + +- `blink.py` blinks the onboard LED ten times. On a Raspberry Pi Pico, switch to the commented-out `Pin("LED")` line. +- `pwm_blink.py` lets the hardware blink an LED on pin 2 with PWM. This one needs an ESP board, because the Pico's PWM can't go as slow as 1 Hz. +- `pwm_fade.py` fades an LED on pin 2 in and out until you press Ctrl+C. + +To start a script automatically whenever the board powers on, copy it to the board as `main.py`: + +```console +$ mpremote cp pwm_fade.py :main.py +``` diff --git a/micropython/blink.py b/micropython/blink.py new file mode 100644 index 0000000000..546d9664f7 --- /dev/null +++ b/micropython/blink.py @@ -0,0 +1,15 @@ +import time +import machine + +blueled = machine.Pin(2, machine.Pin.OUT) +# On a Raspberry Pi Pico, use the onboard LED by name instead: +# blueled = machine.Pin("LED", machine.Pin.OUT) + +# Blink 10 times +for i in range(1, 11): + blueled.value(0) + time.sleep(0.5) + blueled.value(1) + time.sleep(0.5) + +print("DONE!") diff --git a/micropython/pwm_blink.py b/micropython/pwm_blink.py new file mode 100644 index 0000000000..843cdb35a2 --- /dev/null +++ b/micropython/pwm_blink.py @@ -0,0 +1,10 @@ +from machine import Pin, PWM + +# Set our pin 2 to PWM +pwm = PWM(Pin(2)) + +# Brightness between 0 and 65535 +pwm.duty_u16(45000) + +# Frequency in Hertz +pwm.freq(1) diff --git a/micropython/pwm_fade.py b/micropython/pwm_fade.py new file mode 100644 index 0000000000..7fd846c801 --- /dev/null +++ b/micropython/pwm_fade.py @@ -0,0 +1,21 @@ +from machine import Pin, PWM +import time + +# Set our pin 2 to PWM +pwm = PWM(Pin(2)) + +# Frequency = 100hz +pwm.freq(100) + +while True: + # Brightness between 0 and 65535 + for brightness in range(0, 65535, 5000): + pwm.duty_u16(brightness) + print(brightness) + time.sleep(0.1) + + # Brightness between 65535 and 0 + for brightness in range(65535, 0, -5000): + pwm.duty_u16(brightness) + print(brightness) + time.sleep(0.1) diff --git a/micropython/requirements.txt b/micropython/requirements.txt new file mode 100644 index 0000000000..cfda7ccd6a --- /dev/null +++ b/micropython/requirements.txt @@ -0,0 +1,2 @@ +esptool==5.7.0 +mpremote==1.29.0