When you are working on microcontrollers it can be very difficult to know what your code is doing, with MicroPython we can use REPL (Read Evaluate Print Loop) to see the debug output and run python commands in real time. This tutorial will be looking at how you can use a serial connection to take advantage of this powerful debugging tool for any microcontroller running the MicroPython Firmware (or Circuit Python!).
What you will need.
- Serial Client (PuTTY)
- Pycom WiPy3 (Any MicroPython or Circuit Python Board including Pycom, ESP32, micro:bit, and Circuit Playground Express)
- Pycom Expansion Board
- Micro USB Cable
Connecting the MicroPython Board
Now we are ready to get started! If you do not have the MicroPython Firmware installed on your board you will need to do this now. The Pycom Firmware tool makes this very easy and we will continue to add MicroPython Tutorials for how to update the other boards.
Now that the firmware is up to date all we need to do is connect the MicroPython board to your computer with a MicroUSB cable.
If you are using a Pycom board you will need to connect it to the Expansion board first so that you can use the USB interface.
Once the USB is connected, we will need to find out what serial port the MicroPython board is using.
To do this we will use the Windows Command Prompt, just type cmd into the start menu and choose Command Prompt (You can also use PowerShell).
We will now use the type mode command to show a list of all active serial ports. This command does not need any arguments to run so can be done just by entering mode as shown below.
You will now be able to read off the name of the COM port used by the MicroPython, It will probably be the highest numbered COM port. If you are unsure which one is yours remove the board and run mode again to spot the one that is missing.
Note: If you are working in Linux you can use the command ls /dev/ttyUSB*
Connecting to the Board.
We are now going to be using a serial client to connect to the MicroPython board. We will be using PuTTY, a very popular client that supports a lot of communication protocols, that you may recognise if you have done the tutorial on connecting to a headless Raspberry Pi W.
If you do not already have it installed, jump over to the Putty Website and download it.
Open the PuTTY client and enter the settings that will let us access the Serial on the MicroPython Board. First, choose the connection type then you can set the other setting as defined below.
Serial Line: COM43 (The one we got from the previous step)
Speed: 115200
Connection Type: Serial
Now hit Open!
We are now connected to the Micropython board.
You will see a blank window where it appears that nothing is happening, you will need to press Ctrl+C then you will see >>> this is the REPL console. You can also press Ctrl+D to soft reset the board and display the MicroPython Version and board information.
Running and debugging code.
You are now connected to the MicroPython REPL if you have a script set up to run on your MicroPython board you will see the output here. If you write the code below to you MicroPython Board you will see an output similar to what I have. You can also enter this code line by line directly into the REPL terminal.
import machine # Generic library for MicroPython Hardware print("Hello World\n") # print some Text. print("My CPU runs at",machine.freq(),"Hz") # Get the Frequency of the CPU
Thanks for following along with this tutorial, I hope that it has been helpful and you feel now feel more comfortable with connecting to the REPL terminal on the MicroPython boards.
The method used in this tutorial will apply to any microcontroller running MicroPython and as I have mentioned a few will also apply to the Adafruit Circuit Python boards.
If you want to know more about these boards and how to use them you can have a look at our MicroPython Tutorials or Pycom Tutorials.
That's everything for this tutorial! If something in this tutorial did not work as you expected or you would like some more detail about something covered feel free to join the discussion and post on the forum thread below and I or one of the team will help you out.