Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions micropython/README.md
Original file line number Diff line number Diff line change
@@ -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 <kbd>Ctrl</kbd>+<kbd>C</kbd>.

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
```
15 changes: 15 additions & 0 deletions micropython/blink.py
Original file line number Diff line number Diff line change
@@ -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!")
10 changes: 10 additions & 0 deletions micropython/pwm_blink.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 21 additions & 0 deletions micropython/pwm_fade.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions micropython/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
esptool==5.7.0
mpremote==1.29.0
Loading