In this Section

↩ Return Home

↩ Return to Resources

↩ Return to Quick Start

Your First Program


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>

Example 1: Hello World


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.

  1. Plug your board into your computer and open Mu

  2. 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
    
  3. 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:

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/45c058c7-bdb3-4ea1-831d-045138dd959b/Untitled.png

    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.

  4. 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>

  5. Press any key (like ENTER) to enter the REPL

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f151a2ba-a69c-417e-ac44-1321cbc6358c/Untitled.png

  6. 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.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/05a11f76-4fba-40ad-ba0d-2b20051a6b43/Untitled.png