Below is an example of how to code/prototype/debug on the SAM32 using CircuitPython.
Let's pretend we want to write a program to blink the green LED on the SAM32
Plug your board into your computer and open Mu
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
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:
Notice that once the blink.py
file was saved, the SAM32 board restarted, but 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).
Press any key (like ENTER
) to enter the REPL
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.
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
).
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>
The board will naturally start running main.py
again after the reboot, so press Ctrl + C
once more and enter the REPL.
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..
Now that you've experienced saving and calling your own program from the SAM32, let's try editing main.py
directly from Mu.