ElectronicsEmbedded & MCUExplainerIoT HardwaresIoT Software&ToolsProgrammingTutorials/DIY

Programming Languages for Microcontrollers, Arduino, ESP, and Similar Boards

Microcontrollers, including Arduino, ESP8266, ESP32, and other development boards, support a variety of programming languages depending on their architecture, capabilities, and intended use. In this article, we will explore the different languages commonly used for programming microcontrollers and how they are applied.

1. C/C++

Overview:

C and C++ are the most widely used programming languages for microcontrollers due to their efficiency, low-level hardware access, and compatibility with embedded systems.

Use Cases:

  • Arduino boards: Uses a simplified version of C++ with the Arduino API.
  • ESP8266 & ESP32: Supports C/C++ through the ESP-IDF framework and Arduino core.
  • AVR/PIC Microcontrollers: Programmed using C in Atmel Studio, MPLAB, etc.

Example (Arduino C++)

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

Advantages:

  • Efficient memory usage.
  • Direct hardware control.
  • Large community support.

2. MicroPython

Overview:

MicroPython is a lightweight version of Python designed for microcontrollers, making it easier to program boards without requiring complex toolchains.

Use Cases:

  • ESP8266 & ESP32: Supports MicroPython natively.
  • Pyboard: Designed specifically for MicroPython.
  • Raspberry Pi Pico: Supports MicroPython as an alternative to C++.

Example (MicroPython for ESP32)

from machine import Pin
import time

led = Pin(2, Pin.OUT)

while True:
    led.value(1)
    time.sleep(1)
    led.value(0)
    time.sleep(1)

Advantages:

  • Easy to learn and write.
  • Interpreted language (no need for compilation).
  • Large standard library.

3. CircuitPython

Overview:

CircuitPython is a variant of MicroPython, developed by Adafruit, designed to simplify microcontroller programming.

Use Cases:

  • Adafruit boards (Feather, Trinket, etc.).
  • ESP8266 & ESP32 (limited support).
  • Raspberry Pi Pico.

Example:

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(1)
    led.value = False
    time.sleep(1)

Advantages:

  • No need for a compiler.
  • Plug-and-play execution.
  • Easy USB file transfer for code.

4. JavaScript (Espruino)

Overview:

Espruino is a JavaScript interpreter designed for microcontrollers, allowing real-time scripting.

Use Cases:

  • Espruino boards.
  • ESP8266 & ESP32 (limited).
  • STM32-based microcontrollers.

Example:

setInterval(function() {
    digitalWrite(LED1, !digitalRead(LED1));
}, 1000);

Advantages:

  • No compilation required.
  • Can be programmed directly via a web interface.
  • Low memory footprint.

5. Lua (NodeMCU)

Overview:

Lua is a lightweight scripting language, used mainly in the NodeMCU firmware for ESP8266 and ESP32.

Use Cases:

  • ESP8266 with NodeMCU firmware.
  • IoT applications.

Example (Lua for ESP8266):

led = 3
gpio.mode(led, gpio.OUTPUT)
while true do
    gpio.write(led, gpio.HIGH)
    tmr.delay(1000000)
    gpio.write(led, gpio.LOW)
    tmr.delay(1000000)
end

Advantages:

  • Lightweight and fast.
  • Ideal for IoT and scripting applications.

6. Rust

Overview:

Rust is gaining popularity in embedded systems due to its safety features and memory efficiency.

Use Cases:

  • Embedded Linux systems.
  • Bare-metal microcontrollers (STM32, ESP32).

Example (Rust for ESP32):

fn main() {
    loop {
        println!("Hello, Embedded Rust!");
    }
}

Advantages:

  • Memory safety.
  • High performance.
  • Prevents common programming errors.

7. Assembly Language

Overview:

Assembly is a low-level language used for maximum hardware efficiency and optimization.

Use Cases:

  • AVR/PIC microcontrollers.
  • Performance-critical applications.
  • Bootloader programming.

Example (AVR Assembly for ATmega328P):

ldi r16, 0xFF
out DDRB, r16
loop:
in r17, PINB
out PORTB, r17
rjmp loop

Advantages:

  • Direct hardware control.
  • Highly optimized performance.

8. Go (TinyGo)

Overview:

TinyGo allows programming microcontrollers using the Go programming language.

Use Cases:

  • ESP32, Arduino, and ARM Cortex boards.
  • IoT and robotics applications.

Example (TinyGo for Arduino):

package main

import (
    "machine"
    "time"
)

func main() {
    led := machine.LED
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})
    
    for {
        led.High()
        time.Sleep(time.Second)
        led.Low()
        time.Sleep(time.Second)
    }
}

Advantages:

  • Memory efficiency.
  • Ideal for modern applications.

Conclusion

The choice of programming language for microcontrollers depends on:

  • Board compatibility (Arduino, ESP, STM32, etc.).
  • Performance requirements.
  • Ease of development.
  • Memory constraints.

For beginners, C/C++ and MicroPython are the best options. Advanced users might explore Rust, Go, and Assembly for high-performance applications. By understanding the available languages, you can select the best tool for your microcontroller projects.

Read This: Understanding Serial.begin(), Baud Rate, and Serial Communication in Arduino

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *