2026IoTPicowLab/Lab08/CircuitPython/Documentation/RunCodeonPowerUp.txt

25 lines
No EOL
1.4 KiB
Text

Run Code on Power Up
(From Google AI)
In CircuitPython, code automatically runs on power-up or reset if the file is named code.py. Alternatively, it will also work if named main.py.
How it Works
When a CircuitPython board powers on, it automatically looks for and executes specific files in order:
boot.py: This file runs first, if present. It is typically used for initial setup configurations, such as re-assigning pins before the main program starts.
code.py (or main.py): After boot.py finishes, the board runs the code in this file. This is where your main program loop (e.g., a while True: loop) should reside.
Steps to Make Your Code Run on Power-up
Connect your board to your computer via a data-enabled USB cable. It will appear as a USB flash drive named CIRCUITPY.
Open the code.py file located on the CIRCUITPY drive using a code editor like the Mu editor or VS Code.
Write or paste your Python code into this file.
Save the file. As soon as you save the file to the CIRCUITPY drive, the board automatically soft-reboots and runs the newly saved code.
Power cycle the board. To confirm it runs on power-up, simply unplug the board and plug it back in. The program will start running immediately after the board finishes booting.
Any code in the code.py file will run every time the device is powered on or reset, without needing a computer connection (unless you want to view the serial output).