Interacting with the Serial Console

Once you've successfully connected to the serial console, it's time to start using it.

The code you wrote earlier has no output to the serial console. So, we're going to edit it to create some output.

Open your code.py file into your editor, and include a print statement. You can print anything you like! Just include your phrase between the quotation marks inside the parentheses. For example:

Download: file
import time
import adafruit_trellism4

trellis = adafruit_trellism4.TrellisM4Express()

while True:
    print("Hello, CircuitPython!")
    trellis.pixels[0, 0] = (100, 0, 0)
    time.sleep(0.5)
    trellis.pixels[0, 0] = (0, 0, 0)
    time.sleep(0.5)

Save your file.

Now, let's go take a look at the window with our connection to the serial console.

Excellent! Our print statement is showing up in our console! Try changing the printed text to something else.

Download: file
import time
import adafruit_trellism4

trellis = adafruit_trellism4.TrellisM4Express()

while True:
    print("Hello back to you!")
    trellis.pixels[0, 0] = (100, 0, 0)
    time.sleep(0.5)
    trellis.pixels[0, 0] = (0, 0, 0)
    time.sleep(0.5)

Keep your serial console window where you can see it. Save your file. You'll see what the serial console displays when the board reboots. Then you'll see your new change!

The Traceback (most recent call last): is telling you the last thing your board was doing before you saved your file. This is normal behavior and will happen every time the board resets. This is really handy for troubleshooting. Let's introduce an error so we can see how it is used.

Delete the ) at the end of (100, 0, 0) from the line trellis.pixels[0, 0] = (100, 0, 0) so that it says trellis.pixels[0, 0] = (100, 0, 0.

Download: file
import time
import adafruit_trellism4

trellis = adafruit_trellism4.TrellisM4Express()

while True:
    print("Hello back to you!")
    trellis.pixels[0, 0] = (100, 0, 0
    time.sleep(0.5)
    trellis.pixels[0, 0] = (0, 0, 0)
    time.sleep(0.5)

Save your file. You will notice that your red LED will stop blinking, and you may have a colored status LED blinking at you. This is because the code is no longer correct and can no longer run properly. We need to fix it!

Usually when you run into errors, it's not because you introduced them on purpose. You may have 200 lines of code, and have no idea where your error could be hiding. This is where the serial console can help. Let's take a look!

The Traceback (most recent call last): is telling you that the last thing it was able to run was line 9 in your code. The next line is your error: SyntaxError: invalid syntax. This error might not mean a lot to you, but combined with knowing the issue is on line 9, it gives you a great place to start!

Go back to your code, and take a look at line 9. Obviously, you know what the problem is already. But if you didn't, you'd want to look at line 9 and see if you could figure it out. If you're still unsure, try googling the error to get some help. In this case, you know what to look for. You removed a parenthesis. Fix the typo and save your file.

Nice job fixing the error! Your serial console is streaming and your NeoPixel Is blinking red again.

The serial console will display any output generated by your code. Some sensors, such as a humidity sensor or a thermistor, receive data and you can use print statements to display that information. You can also use print statements for troubleshooting. If your code isn't working, and you want to know where it's failing, you can put print statements in various places to see where it stops printing.

The serial console has many uses, and is an amazing tool overall for learning and programming!

This guide was first published on Oct 31, 2018. It was last updated on Oct 31, 2018. This page (Interacting with the Serial Console) was last updated on Jun 17, 2019.