101 Guide To Microcontroller Programming Inside Virtual Machines
Table of Contents
Introduction#
Hey everyone, I’m back with a new entry. This time, I’d like to guide you on how to create a simple hello world program for the ESP32-C6 inside a Virtual Machine.
Having all the toolchain inside a VM is a great way to unclutter your host machine. Programming inside a VM or containers for web development is quite common, but for microcontrollers, it’s a thing I don’t encounter that often.
What we need#
For this tutorial, we will need:
- Fedora Server 41 ARM
- UTM
- ESP32-C6
- USB-C cable
Creating the Virtual Machine#
Let’s start by creating the Virtual Machine. To archive this, you need a software capable of virtualizing your OS environment. Get UTM from the macOS Apple Store or directly from their website.
We also need to get the ISO we are going to use to create the VM. In this tutorial, we are using Fedora Server. Download the ARM ISO from here.
We will follow similar steps to install the OS similar to the ones on this post I made some time ago. We can follow along, with the difference that we are selecting Virtualize this time.
Once the OS is ready and SSH is working, let’s install the dependencies!
Installing dependencies inside the VM#
Once you’re inside the VM, let’s run the following commands:
# Update the virtual OS
sudo dnf update -y
# Installing the needed software
sudo dnf install -y python3 python3-pip
# Install the udev needed to connect to the microcontrollers
curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/system/99-platformio-udev.rules | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
# Install PlatformIO Core CLI
curl -fsSL -o get-platformio.py https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py python3 get-platformio.py
We are using the PlatformIO CLI because the graphical interface installed by the plugin doesn’t work by default inside the VM. As a workaround, you could run
pio home --host 0.0.0.0 --port 8008
to access the VSCode plugin GUI interface from your host browser.
Creating our basic project template#
Once we have finished installing the tooling, we can start creating our hello world project.
As a first step, let’s activate the Python environment created by PlatformIO, to achieve that run:
# Activate the Python environment for PlatformIO
source ~/.platformio/penv/bin/activate
Then let’s create our project, navigate to the directory where you want to host the project, and run the following:
# Find the name of your microcontroller from the supported list, copy this name
pio boards | grep "YOUR_BOARD_NAME_HERE"
# We use pio to create the basic template project
pio project init --project-dir . --board "YOUR_BOARD_NAME_HERE"
# Open your project in VSCode
code .
As simple as that, we have our base project ready for us, now let’s do some code!
Programming the Hello World#
Once you have opened the project, open the file called main.c
located inside /src/
and add the following:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
void app_main() {
while (true) {
printf("Hello world\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
The ESP32-C6 is an ARM device, so we are using the Espressif framework instead of the Arduino framework. Check with your manufacturer the type of architecture your board uses.
This program will print to the serial bus the string Hello world\n
every second.
We also need to specify the baud rate so that we can monitor the output correctly. Open the file called platformio.ini
and append at the end the following line:
monitor_speed = 115200
With all this done, we are one step closer to seeing our awesome code working!
Compiling, flashing, and monitoring the microcontroller#
Now it’s time to connect our board via USB to the VM:
- Connect the board using USB to your host machine.
- Open the VM window in UTM and, in the top right, press the USB plug icon and connect your board to the VM.
- Check that the board is read by Fedora by running
lsusb
. You should see the same name that appeared on the UTM menu.
We can use the PlatformIO extension to make the next steps easier. Install the extension inside the VM. After reloading, you will see at the bottom left these 3 icons: check mark, right arrow, and electric plug.
Now let’s do the final steps!
- Press the check mark to build the project.
- Press the right arrow to flash your board (some boards may need to press some buttons on the board to activate the flash mode).
- Press the electric plug icon to see the
hello world
string being sent to the serial monitor every second!
Congratulations, now you can start developing inside a VM for your microcontrollers.
Conclusion#
Developing for microcontrollers inside a virtual machine not only helps keep your host system clean and organized but also creates a portable and reproducible development environment. By following this guide, you’ve set up a Fedora Server VM with all the necessary tools to program an ESP32-C6, created a basic “Hello World” project, and successfully flashed it to your board.
This approach combines the flexibility of modern tools like PlatformIO with the advantages of virtualization, providing a robust setup for any microcontroller project. Whether you’re just starting with embedded systems or looking for a cleaner workflow, this method proves that virtual machines are a practical solution for microcontroller development.
Now that you’ve mastered the basics, you can explore more complex projects, experiment with different boards, or even automate your setup further. Happy coding, and enjoy building your next microcontroller masterpiece!