How to Write Microcontroller Programs Using C#

Due to personal interest in researching microcontrollers, but not being very familiar with C, I looked for materials to study if there were other methods. I discovered the C# nanoframework framework abroad that can be used to write microcontroller programs. In this article, I will summarize some experiences of writing microcontroller programs in C# based on the pitfalls I have encountered, hoping to help those who love C#.

First, we need to prepare the materials:

1. ESP32 board that supports WiFi and Bluetooth, and an Android data cable as shown:

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

2. Then prepare VS Code and VS2022, this article will use VS2022 for the tutorial.

After preparation, connect the ESP32 device to the computer (using the Android cable).

Start building the ESP32 nanoframework development environment.

Open VS Code and then click on Extensions.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Search for nanoframework and then install it.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

After installation is complete; open VS2022, click continue but no code is needed.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Click on Manage Extensions.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C# Search and install the nanoframework extension. After installation, close the entire VS2022.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

After closing VS2022, this installation extension interface pops up, click install.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Long wait for installation.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

After installation is complete, reopen VS2022.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Then click on Create New Project.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Search for nanoframework, then click the first one and click next.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Create a project Demo example and click create.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Click View => Other Windows => Device Explorer in sequence.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Click settings.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Click all on both sides (COM port black list: indicates shield COM).

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Then close the settings; we come to VS Code.

Find the project path just created in VS2022 and open it.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Install the C# VSCode extension.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Note! Please install the ESP32 driver first.

ESP32 link: https://pan.baidu.com/s/1V90LMRk13ZaUMJxM2teXbw

Extraction code: ztip

Copy the GitHub mirror nanoframework/nf-interpreter: nanoFramework Interpreter, CLR, HAL, PAL and reference target boards (github.com)How to Write Microcontroller Programs Using C#https://github.com/nanoframework/nf-interpreter

Install .NET nanoFramework firmware flashing

dotnet tool install -g nanoff

How to Write Microcontroller Programs Using C#

After successful installation, a message will be displayed showing the command to call the tool and the installed version. Similar to the following example:
You can invoke the tool using the following command: nanoff
Tool 'nanoff' (version '9.9.9') was successfully installed.

How to Write Microcontroller Programs Using C#

Installation Path Issues

⚠️ Known issue when installed in a path containing diacritics. This is caused by a known bug in the STM32 multidimensional data set programmer. For example, if your user path is this case, you must install it in a location without these paths. To achieve this, use the following .NET Core CLI command that specifies the path where the tool will be installed: nanoff

dotnet tool install nanoff --tool-path c:\a-plain-simple-path-to-install-the-tool

How to Write Microcontroller Programs Using C#

Note that this restriction does not apply if you are not using with STM32 devices.

Update .NET nanoFramework firmware flashing

To update the .NET nanoFramework firmware Flasher tool, use the following .NET Core CLI command:

How to Write Microcontroller Programs Using C#

ESP32 Usage Example

There are multiple ESP32 images available, some of which are specifically built for targets. Please check the list.

ESP32_PSRAM_REV0 image is only available for any variant of the ESP32 series (with or without PSRAM) and all chip revisions. You can read more here about the differences between various images.

FEATHER_S2 image is only available for almost all variants of the ESP32-S2 series that expose embedded USB CDC pins. You can read more here about the differences between various images.

When using, you can add to use a specific image. Alternatively, if you just specify the platform, the most suitable image will be selected based on the capabilities of the connected device. An output similar to this will be displayed to suggest which image to use: nanoff--target MY_TARGET_NAME_HERE--platform esp32nanoff

No target name was provided! Using 'ESP32_REV0' based on the device characteristics.

How to Write Microcontroller Programs Using C#

Some ESP32 development boards have issues entering bootloader mode. This can often be overcome by holding down the BOOT/FLASH button on the board. If this situation is detected, the following warning will be displayed:

*** Hold down the BOOT/FLASH button in ESP32 board ***

How to Write Microcontroller Programs Using C#

Update the firmware of the ESP32 target

To update the firmware of the ESP32 target connected to COM31 to the latest available development version.

nanoff --update --target ESP32_PSRAM_REV0 --serialport COM3

How to Write Microcontroller Programs Using C#

Update the firmware of ESP32-S2 KALUGA 1 using local CLR file

Update the firmware of the ESP32-S2 KALUGA 1 target connected to COM31 using a local CLR file (e.g., from a build). This file must be a binary with a valid CLR from the build. No further checks or validations will be performed on the file content.

nanoff --update --target KALUGA_1 --serialport COM31 --clrfile "C:\nf-interpreter\build\nanoCLR.bin" 

How to Write Microcontroller Programs Using C#

Show details of connected ESP32 device

Display details of the ESP32 device connected to COM31.

nanoff --platform esp32 --serialport COM31 --devicedetails 

How to Write Microcontroller Programs Using C#

Official nanoFramework firmware flashing tool

nanoframework/nanoFirmwareFlasher: 🧰 nano firmware flasher (.NET nanoFramework toolbox) (github.com)

Link: https://pan.xunlei.com/s/VMrXz6nf9tQBsdoKxOm_nYccA1 Extraction code: eidf

Download this firmware from Thunder Cloud Disk and extract it to D:\

nanoff --update --target KALUGA_1 --serialport COM31 --clrfile "D:\nanoCLR.bin" 

Run the flashing program to flash the firmware and wait for success, then enter VS Code.

Press ctrl+shift+p; then search for nanoframework and click on nanoframework flash device.

Wait a moment and a dialog box will pop up.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Install your ESP32, I installed the one in the red box.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Firmware flashing completed.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Then open VS2022, at this time we can see the device and then click run.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

Alright, the run is complete.

How to Write Microcontroller Programs Using C#How to Write Microcontroller Programs Using C#

If there are friends who love C# microcontroller research, you can add my QQ: 239573049

Leave a Comment