Skip to content

5. Embedded Programming

Group Assignment

Here the Fab Academy students from Fab Lab Reykjavik are reporting their group work. The general group assignment for Embedded Programming is:

  • Demonstrate and compare the toolchains and development workflows for available embedded architectures
  • Document your work to the group work page and reflect on your individual page what you learned

In consultation with our instructor, we are each taking different views of the assignment, and reporting results here.

  • Albert: Describing how to use micropython in VSCode in programming for RP2040
  • Anna: Something ESP and Thonny
  • Evert: Something Arduino
  • Magnus: Something RP2040

Micropython, VSCode and RP2040 programming

To set up an environment to program a RP2040 with micropython using VSCode there are two major steps. One needs to prepare the RP2040 and load micropython. Then the VSCode environment needs to be prepared including necessary extensions.

Prepare RP2040

An online tutorial was instructive in getting started.

RP2040

  1. Put RP2040 into boot mode by holding B button and connecting computer via USB-C. This mounts the RP2040 as a drive

Mounted RP2040

  1. Download the current micropython. This is firmware which support an RP-2040 and micropython
  2. Drag the resulting file onto the mounted drive. The RP2040 reboots and has micropython installed

Prepare VSCode

  1. Ensure that Python 3.9 or newer is installed on system
  2. Install Visual Studio Code
  3. Install micropico extension in VSCode
  4. In VSCode open project folder via ‘File/Open Folder…’
  5. Via Control-Shift-P (Command-Shift-P on Mac) do ‘Micropico: Initialize MicroPico Project’

This configures and results in a message. A set of files is also created.

When the RP2040 is connected as noted above and these steps completed, there will be an indicator at the bottom of VSCode which says “Pico Connected.”

  1. Now prepare to run code by creating a file with appropriate code

from machine import Pin
from time import sleep

led = Pin('LED', Pin.OUT)
print('Blinking LED Example')

while True:
  led.value(not led.value())
  sleep(0.5)
Pushing ‘Run’ at the bottom of the VSCode window results in a blinking LED on the RP2040.

LED blinking on RP2040

Documentation on the RP2040 is available on the micropython.org site.

This creates an environment to work with micropython in VSCode for the RP2040.

Thonny

Thonny.org

There are 2 main options to install Thonny. If you have python already then you can just have python install Thonny for you.

pip install thonny

Alternatively you can install them in a bundle from thonny git. Or you can use the installer for Thonny alone. Make sure to select the correct installer. You if on a windows computer then “thonny-4.1.7.exe” is the installer you need. Then run the following code to ensure python is working with Thonny. Then do the pip install thonny in python.

First you can test python is working by running this code in Thonny.

Now Thonny is running and python is working. Let’s try to work with the micro controller.

ESP32-S3

This is a good tutorial for getting Thonny and the ESP32 running on your machine, Youtube Tutorial.

I was still having issues so I decided to follow the instruction on the on the Seeed Studios wiki ESP32S3. My opinion is to get the light blinking in arduino IDE before moving to Thonny.

After using Arduino you need to erase the chip. Go to windows powershell and use this code.

python -m esptool --chip ESP32-S3 erase_flash
If you need to erase the chip before loading micro python then this is the place to go. See link.

You can download the image for the chip here.

Chat GPT

Me - what is the mirco python code for blinking one led

GPT- 
Here's a simple MicroPython script to blink an LED connected to a GPIO pin (e.g., pin 2 on an ESP8266 or ESP32):

python
Copy
Edit
from machine import Pin
import time

led = Pin(2, Pin.OUT)  # Set GPIO 2 as an output pin

while True:
    led.value(1)  # Turn the LED on
    time.sleep(1)  # Wait for 1 second
    led.value(0)  # Turn the LED off
    time.sleep(1)  # Wait for 1 second

Explanation:
Pin(2, Pin.OUT): Initializes GPIO 2 as an output pin.
led.value(1): Turns the LED on.
led.value(0): Turns the LED off.
time.sleep(1): Pauses execution for 1 second.
Let me know if you need modifications (e.g., different pin, faster blinking, etc.). 🚀