Serial Console
Last updated
Last updated
One of the staples of CircuitPython (and programming in general!) is something called a "print statement". This is a line you include in your code that causes your code to output text. A print statement in CircuitPython (and Python) looks like this:
This line in your code.py would result in:
However, these print statements need somewhere to display. That's where the serial console comes in!
The serial console receives output from your CircuitPython board sent over USB and displays it so you can see it. This is necessary when you've included a print statement in your code and you'd like to see what you printed. It is also helpful for troubleshooting errors because your board will send errors and the serial console will display those too.
The serial console requires an editor that has a built-in terminal, or a separate terminal program. A terminal is a program that gives you a text-based interface to perform various tasks.
CircuitPython extension in VS Code comes with a Serial Console. Every time VS Code is restarted, the CircuitPython extension reactivates and opens Circuit Python Serial Monitor in the VS Code Terminal. If Terminal is not showing up, Click View in the VS Code menu and select Terminal.
Click on the little icon circled in red in the below image, which should allow you to select COM port to which the Feather Sense board is connected.
Once connected you should see COM port in place of connection icon at the bottom right of the VS Code window as shown in the below image. Update code as below and Serial Console should show the output.
The other feature of the serial connection is the Read-Evaluate-Print-Loop, or REPL. The REPL allows you to enter individual lines of code and have them run immediately. It's really handy if you're running into trouble with a particular program and can't figure out why. It's interactive so it's great for testing new ideas.
To use the REPL, you first need to be connected to the serial console. Once that connection has been established, you'll want to press CTRL+C.
If there is code running, in this case code measuring distance, it will stop and you'll see Press any key to enter the REPL. Use CTRL-D to reload.
Follow those instructions, and press any key on your keyboard.
The Traceback (most recent call last):
is telling you the last thing your board was doing before you pressed Ctrl + C and interrupted it. The KeyboardInterrupt
is you pressing CTRL+C. This information can be handy when troubleshooting, but for now, don't worry about it. Just note that it is expected behavior.
Once you press a key you'll see a >>>
prompt welcoming you to the REPL!
From this prompt you can run all sorts of commands and code. The first thing you'll do is run help()
. This will tell you where to start exploring the REPL. To run code in the REPL, type it in next to the REPL prompt.
Type help()
next to the prompt in the REPL and press enter.
You should see the following message.
The REPL can also be used to run code. Be aware that any code you enter into the REPL isn't saved anywhere.