Circuit Python Basics
For this course, we’ll be programming in circuit python. It’s Python, but with added bits for controlling electronics!
In this first tutorial, we’ll learn:
- how to connect our M4 to our computer
- how to connect our M4 board’s “serial console”
- how to write “print” statements
- how to save/write code to our python board
- how to communicate with our microcontroller via the serial console
Installing Mu and Connecting your M4 board
- Follow this link to install the mu editor on your computer.
- Open the Mu Editor.
- Select “Circuit Python” as your target programming language
- Connect your microcontroller (M4) to your Mac via Micro USB.
Mu Basics
Here’s a simple cheat sheet for how to use various functions in the Mu Editor.
Saving & Running Code
Let’s write our first program. Programs are a set of instructions that get executed/carried out by a computer. Generally, these programs have user inputs, and outputs. Let’s write our first physical program that has our microcontroller talking back to us via our “serial console”. With your M4 connected to your computer via USB:
- In Mu, Click “Load”
- Navigate to the “CircuitPy” drive in your finder
-
Select “code.py” to open
- “CMD-A” to select all code, and delete, so we’re starting with a fresh canvas.
- on line 1, write:
` print(“Hello Python!) `
- In Mu, press on “Serial” to open your serial console. (This is what our M4 tells to us)
- Press “Save”.
- If you unplug or reset the board before your computer finishes writing the file to your board, you can corrupt the drive.
- If this happens, you may lose the code you’ve written, so it’s important to backup your code to your computer regularly.
- You should now see in the serial console below, with your print statement
- Try writing and saving a few more print statements onto the code.py file like so:
print("hello Python!)
print("this is line 2!)
print("and I'm a serial console talking to you)