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, 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.
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.
If there is no code running, you will enter the REPL immediately after pressing Ctrl + C. There is no information about what your board was doing before you interrupted it because there is no code running.
Either way, once you press a key you'll see a
>>> prompt welcoming you to the REPL!
If you have trouble getting to the
>>> prompt, try pressing Ctrl + C a few more times.
The first thing you get from the REPL is information about your board.
This line tells you the version of CircuitPython you're using and when it was released. Next, it gives you the type of board you're using and the type of microcontroller the board uses. Each part of this may be different for your board depending on the versions you're working with.
This is followed by the CircuitPython prompt.
From this prompt you can run all sorts of commands and code. The first thing we'll do is run
help(). This will tell us where to start exploring the REPL. To run code in the REPL, type it in next to the REPL prompt.
help() next to the prompt in the REPL.
Then press enter. You should then see a message.
First part of the message is another reference to the version of CircuitPython you're using. Second, a URL for the CircuitPython related project guides. Then... wait. What's this?
To list built-in modules, please do `help("modules")`. Remember the libraries you learned about while going through creating code? That's exactly what this is talking about! This is a perfect place to start. Let's take a look!
help("modules") into the REPL next to the prompt, and press enter.
This is a list of all the core libraries built into CircuitPython. We discussed how board contains all of the pins on the board that you can use in your code. From the REPL, you are able to see that list!
import board into the REPL and press enter. It'll go to a new prompt. It might look like nothing happened, but that's not the case! If you recall, the
import statement simply tells the code to expect to do something with that module. In this case, it's telling the REPL that you plan to do something with that module.
dir(board) into the REPL and press enter.
This is a list of all of the pins on your board that are available for you to use in your code. Each board's list will differ slightly depending on the number of pins available. Do you see
D13? That's the pin you used to blink the red LED!
The REPL can also be used to run code. Be aware that any code you enter into the REPL isn't saved anywhere. If you're testing something new that you'd like to keep, make sure you have it saved somewhere on your computer as well!
Every programmer in every programming language starts with a piece of code that says, "Hello, World." We're going to say hello to something else. Type into the REPL:
Then press enter.
That's all there is to running code in the REPL! Nice job!
You can write single lines of code that run stand-alone. You can also write entire programs into the REPL to test them. As we said though, remember that nothing typed into the REPL is saved.
There's a lot the REPL can do for you. It's great for testing new ideas if you want to see if a few new lines of code will work. It's fantastic for troubleshooting code by entering it one line at a time and finding out where it fails. It lets you see what libraries are available and explore those libraries.
Try typing more into the REPL to see what happens!
When you're ready to leave the REPL and return to the serial console, simply press Ctrl + D. This will reload your board and reenter the serial console. You will restart the program you had running before entering the REPL. In the console window, you'll see any output from the program you had running. And if your program was affecting anything visual on the board, you'll see that start up again as well.
You can return to the REPL at any time!