MicroPython on a Pico: import a module of your own
MicroPython on a Raspberry Pi Pico: main.py imports helpers.py from the next tab and prints a random number from it once a second. The whole build, a Raspberry Pi Pico, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $4. On Mokxi they're free.
What you'll learn
- Importing a module of your own
- Putting reusable code in a separate file
- Calling a function from another file
- Making random numbers with the random module
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico
- Parts: 1
How it works
This project has two files. helpers.py holds a small function called pick. main.py does import helpers and calls it. MicroPython finds helpers.py on the board's filesystem, next to main.py.
Every second the loop prints a random number between 1 and 100. Nothing is wired. Splitting code into modules keeps bigger programs tidy.
Parts list
1 part, plus the jumper wires. They retail for about $4; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
Kits that can build it
These 4 starter kits have every part in the circuit. Each kit's page lists what else it can build.
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Your own module: helpers.py is in the next tab. import finds it on the
# board's filesystem, next to main.py, the same way it finds machine.
import time
import helpers
while True:
print("random number:", helpers.pick(1, 100))
time.sleep(1)
# helpers.py: a module of your own. main.py imports it and calls pick().
import random
def pick(low, high):
"""A whole number from low to high, both included."""
return random.randint(low, high)
Code walkthrough
The key functions of the sketch, and what each one does.
pick helpers.py
Lives in helpers.py. It takes low and high and returns random.randint(low, high), a whole number that can be either end. main.py calls helpers.pick(1, 100) to get a number from 1 to 100.
def pick(low, high):
"""A whole number from low to high, both included."""
return random.randint(low, high)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- ImportError: no module named helpers This project
The file must be called helpers.py and sit next to main.py. Check the spelling of the name inimport helpers. - The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it. - A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.