In this Section
Programs on PyCubed are simply plain-text files that are saved in the root directory of the board. Changing the program involves merely editing the text file and clicking save! The syntax is exactly like Python3 (more on this later).
Every time it's powered, PyCubed will automatically run the file main.py
if it exists on the drive.
In the previous section, Connect & Verify, we learned PyCubed ships with a main.py
file that cycles the RGB LED through a rainbow of colors. Now, we'll see how to create our own program on PyCubed and how to execute it rather than main.py
.
<aside> 💡 Remember Coding and using PyCubed is exactly like using other CircuitPython boards. Learn, troubleshoot, and get inspired by seeking out CircuitPython projects on the web.
</aside>
Let's start off by creating a PyCubed program that prints "Hello World" to the serial port once, then once per second it will print the time (in seconds) it's been powered.
Plug your board into your computer and open Mu
Paste the following into the coding window within Mu
import time
import board
# print a string only once
print('Hello World!')
# ------- MAIN LOOP -------
while True:
# print a string followed by the time in seconds since boot
print('Time on: ',time.monotonic())
time.sleep(1) # sleep 1 second
Click the "Save" button in Mu, navigate to the PYCUBED drive (if it isn't there already), and name the file something like HelloWorld.py
. After saving the file, my Mu looks like this:
Notice that once the HelloWorld.py
file was saved, PyCubed will restart, and will always begin by running the default main.py
file. We could have edited main.py
, but creating another file allows us to quickly iterate through our coding approach.
Click inside the REPL window and press Ctrl+C
to HALT the current program (main.py).
<aside>
💡 Keep in mind:
Depending on the code that's running, you may need to press Ctrl+C
multiple times in order for Mu to catch the keyboard interrupt)
</aside>
Press any key (like ENTER
) to enter the REPL
Run our file by typing import HelloWorld
(capitalization matters!) in the REPL and pressing ENTER
The code will now run, and you should see something like what is shown below. Remember that your "Time on:" value could be very different depending on how long the board has been plugged into your computer.