-
Notifications
You must be signed in to change notification settings - Fork 1
Sample Workflow 1
This is a work in progress
This example consists of a single file (main.py) that is created and tested using Visual Studio Code and the MPRemote extension. The purpose is to a simple example of managing a project. To follow along, you will need the following:
- A microcontroller flashed with the latest MicroPython version
- A development host with Visual Studio Code and the MPRemote extension installed
The example was written using an ESP32-S3-DevKitC-1-N8 and MicroPython 1.20
When working through this example, you will use VS Code to create the MicroPython code on a development host. You will then upload the code to the microcontroller. Any changes are made on the local copy first and then uploaded.
Working this way ensures the "golden copy" of the code always resides on the development host.
Start Visual Studio Code on your development host and click the snake icon in the action pane (far left side). You will see a brief description of how to get started with the extension. Let's focus on the first step, opening the project folder.
- Click the Open Folder button
- Browse to an existing empty directory or click New Folder in the dialog box to create a new one.
- Click Open Folder
Notice how the empty folder is now open in Visual Studio Code's Explorer pane.
- Hover your mouse over the name of your project folder in the Explorer pane.
- Click the icon for New File.
- Name the file main.py.
Notice how the file opens in a new editor window. Copy the following code and paste it in the empty editor window.
# Traffic light example using built-in NeoPixel on an ESP32 DevKit board.
from machine import Pin
from neopixel import NeoPixel
from time import sleep
GPIO = 48 # Check ESP32 board silkscreen for the NeoPixel's GPIO number.
rgb = NeoPixel(Pin(GPIO), 1)
while True:
print("Red light (stop)")
rgb[0] = (8, 0, 0) # Red, green, blue values. Range 0..255
rgb.write()
sleep(15)
print("Green light (go)")
rgb[0] = (0, 8, 0)
rgb.write()
sleep(12)
print("Amber light (caution)")
rgb[0] = (8, 3, 0)
rgb.write()
sleep(3)
Figure 1: Using the NeoPixel as a traffic light.
Save the file you just created before proceeding. Now we're going to upload main.py to the microcontroller's flash filesystem.
- Click the snake icon again.
- Click the Sync button.
- Read the message about overwriting and click OK.
Notice how the VS Code Terminal Window shows the MPRemote command used to copy main.py to the microcotroller.
In this step, you can see the output of the print statements and watch for any errors.
- If you're not already looking at the MPREMOTE: GETTING STARTED pane, click the snake icon.
- Click the REPL button.
- Press the Reset button on your microcontroller.
Notice the MPRemote command that runs in the terminal window and the resulting output from the microcontroller boot sequence and the print statements in main.py. The NeoPixel on the board should cycle through the colors of a traffic signal.
When editing the code, it's important to note changes are always made to the copy on the development host and then uploaded, just like when we first created main.py. Working in this manner ensures the most up-to-date copy of the code is always on the development host.
- Exit the infinite while loop by clicking anywhere inside the terminal window and pressing CTRL + C.
- Press CTRL + X to exit the REPL prompt.
- Go back to the editor window where main.py is open and append the following code after the amber light section.
print("The Wind Cries Mary")
rgb[0] = (0, 0, 8)
rgb.write()
sleep(30)
Save the file locally and repeat the Sync and REPL commands you used earlier. When you reset the board, you will now be treated to an entirely new traffic light color.
Congratulations if you've figured out the Easter egg hidden in the print statement for this last section.
You can happily go on coding using the iterative process described above. You'll sleep soundly knowing the "golden copy" of your code is always on your development host instead of sitting on the flash filesystem of an easily lost microcontroller board. You can also take things one step further and use VS Code's git functionality.
- In VS Code, click the Source Control icon.
- Initialize the repository by clicking the button.
- Publish the files to GitHub or any other Git service.