Below is an example of how to code/prototype/debug on the SAM32 using CircuitPython.

Example 1

Let's pretend we want to write a program to blink the green LED on the SAM32

  1. Plug your board into your computer and open Mu

  2. Paste the following into the coding window within Mu

    import board
    import time
    import digitalio
    
    # Built in LEDs
    led = digitalio.DigitalInOut(board.LED)
    led.switch_to_output()
    
    ######################### MAIN LOOP ##############################
    delay = 2 # make bigger to slow down
    i = 0
    while True:
      print(i)
      led.value = 1
      time.sleep(delay) 
      led.value = 0
      time.sleep(delay)
      i += 1
    
  3. Click the "Save" button in Mu, navigate to the CIRCUITPY drive (if it isn't already), and name the file something like blink. After saving the file, my Mu looks like this:

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8fb20d13-d098-46a7-b9c0-72021b994f6c/Untitled.png

    Notice that once the [blink.py](<http://blink.py>) file was saved, the SAM32 board restarted, but will always begin by running the default [main.py](<http://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).

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

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/caf72070-6f5d-4fc6-84c1-b439e867a48a/Untitled.png

  6. Run our blink file by typing import blink in the REPL and pressing ENTER

    The code will now run, with the green LED blinking on the board and printing an incrementing value to the REPL every 4 seconds.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/c073d74e-723b-40c6-8352-961b299daaa9/Untitled.png

  7. Let's increase the the blink rate. In the code-region above the REPL in Mu, change the delay varriable on line 10 from 2 to 0.5 and click "Save" (or Ctrl+S).

  8. Notice how the SAM32 did not restart, and our prior blink code is still running. Click inside the REPL and press Ctrl + C to halt the board, then Ctrl + D to reboot.

    <aside> 💡 A Ctrl + D reboot is required for CircuitPython to re-read the internal memory and discover any new edits or files.

    </aside>

  9. The board will naturally start running [main.py](<http://main.py>) again after the reboot, so press Ctrl + C once more and enter the REPL.

  10. Enter import blink into the REPL and press ENTER. The board will now run your updated blink.py file.

When writing CircuitPython programs, you can imagine how helpful it is to create "checkpoints" or "snapshots" of the code simply by saving the file with a different name. Think: blink1.py , blink2.py , etc..

Example 2

Now that you've experienced saving and calling your own program from the SAM32, let's try editing [main.py](<http://main.py>) directly from Mu.