2026IoTPicowLab/Lab09/MicroPython/Documentation/Run a MicroPython Script Automatically on Power-Up.txt
2026-03-31 00:45:08 -04:00

32 lines
No EOL
1.8 KiB
Text

Run Code on Power Up
(From Google AI)
To run a MicroPython script automatically on power-up, you must
save your code to the device's filesystem with the specific filename main.py. The board's bootloader is programmed to look for this file and execute its contents after finishing its initial setup process.
Steps to Save and Run Code on Power-up
The general process involves connecting your board to a computer, accessing its file system, and saving your script with the required name.
Connect your board: Plug your MicroPython board (like a Raspberry Pi Pico or ESP32) into your computer via USB.
Open your IDE/Editor: Use a MicroPython-compatible IDE like Thonny to connect to the board's serial port.
Save your file as main.py:
In Thonny, write your code in the editor window.
Go to File > Save as.
Select the option to save the file to the "Raspberry Pi Pico"
(or your specific board's name) instead of your computer.
Name the file main.py and click OK or Save.
Power cycle the board: Eject or unmount the device safely from your computer's file system, and then unplug and re-plug the USB cable (or press the RST button if available).
Upon reconnection, the code within main.py will run automatically.
Advanced Boot Options
MicroPython actually uses two special files during the boot process:
boot.py: This file runs first and is intended for low-level configuration, such as setting up the system path or network connections. You typically do not need to modify this file unless you are customizing the boot process itself.
main.py: This file runs after boot.py and should contain your primary application logic.
By correctly naming your script main.py, you ensure it behaves like an embedded system program, running immediately when power is applied, without needing a computer connection or manual command.