Running C# on an ESP32 Microcontroller

Running C# on an ESP32 Microcontroller

Introduction

The ESP32, a low-cost, low-power system on a chip microcontroller with integrated Wi-Fi and dual-mode Bluetooth, has gained popularity in the IoT domain.

Typically, ESP32 is associated with C/C++ programming. However, with the advent of platforms like nanoFramework, running C# code on ESP32 is now a feasible and exciting option. This blog post guides you through setting up and running C# on an ESP32 microcontroller.

Why C# on ESP32?

C#, a language developed by Microsoft, is known for its simplicity and powerful capabilities. By running C# on ESP32, developers can leverage the powerful features of C# in IoT projects, making the development process smoother, especially for those already familiar with the .NET ecosystem.

Setting Up Your Environment

Prerequisites

  1. ESP32 Development Board: Any standard ESP32 board will do.
  2. Visual Studio 2019 or later: Ensure you have the latest version of Visual Studio.
  3. .NET Core: Required for the nanoFramework extension.

Installing nanoFramework

nanoFramework is an open-source .NET platform that enables the writing of managed code applications for constrained embedded devices.

  1. Visual Studio Extension: Install the nanoFramework extension for Visual Studio from the Visual Studio Marketplace.
  2. nanoFramework Flashing Tool: Download and install the nanoFramework flashing tool from their official website.

Preparing the ESP32 Board

Before you can run C# code, the ESP32 board must be prepared with the nanoFramework firmware.

  1. Connect the ESP32: Use a micro USB cable to connect your ESP32 board to your computer.
  2. Open the Flashing Tool: Launch the nanoFramework flashing tool.
  3. Flash the Firmware:
    • Select the COM port that the ESP32 board is connected to.
    • Choose the appropriate firmware for ESP32.
    • Start the flashing process.

Once the process is completed, your ESP32 is ready for C# programming.

Writing Your First C# Program

  1. Create a nanoFramework Project:
    • In Visual Studio, create a new project.
    • Select the nanoFramework template (e.g., “nanoFramework Console Application”).
    • Name your project.
  2. Write the C# Code:
    • You can write standard C# code here. For example, a simple blinking LED program.
    • Use the GPIO (General Purpose Input/Output) libraries provided by nanoFramework to control the ESP32 hardware.
  3. Example Code:
using System
using System.Device.Gpio
using System.Threading

public class Program 
{
    public static void Main() 
    { 
        GpioController gpionew GpioController(); 
        
        int ledPin2// Change according to your board 
        gpio.OpenPin(ledPin, PinMode.Output); 
        while (true) 
        { 
            gpio.Write(ledPin, PinValue.High); 
            Thread.Sleep(1000); 
            gpio.Write(ledPin, PinValue.Low); 
            Thread.Sleep(1000); 
        }
   }
}

Deploying and Running the Code

  1. Connect Your ESP32 Board: Ensure your ESP32 board is connected to your computer.
  2. Select the Device in Visual Studio:
    • In the Visual Studio toolbar, select the nanoFramework device selector.
    • Choose your ESP32 board from the list.
  3. Deploy the Code:
    • Click the “Start” button or press F5 to build and deploy your application to the ESP32 board.
  4. Observe the Output: Your ESP32 board should now be running the C# code, and you should see the intended behaviour (like an LED blinking).

Conclusion

Running C# on ESP32 opens a new realm of possibilities for developers in the IoT space, combining the efficiency and flexibility of C# with the power of the ESP32.

Whether you’re a hobbyist or a professional, leveraging C# for your IoT projects can streamline the development process and enhance the capabilities of your applications.

With the steps outlined in this guide, you’re now equipped to embark on your journey of integrating C# with ESP32, bridging the gap between .NET development and embedded systems.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.