EMBEDDED PROGRAMMING
Embedded Programming means programming for embedded Systems. The languages(like C or CircuitPython/MicroPython) used on embedded systems, needs less resources in computing power or storage compared to the languages used in regular computers, but also comes with less features like garbage collecting. Embedded Systems are specialized Computers on smaller scale used for example in Washmaschines, smarthome devices or drones. They are called microcontroller or microprocrssor.
The most common programming language for Microcontroller is C/C++. This will be our first language for comparison. The second language we will use, is Micropython.
In oder to compare the two microcontrollers We went back to look over the data sheets. After looking over the sheets We found that there are some small differences in the Performance.
The ESP32 is the more powerful microcontroller of the two, as it has a higher clock speed. The second biggest difference is that the ESP32-S3 comes with Wi-Fi and Bluetooth, allowing it to gather information from a local computer or even communicate with other microcontrollers that have built-in Wi-Fi or Bluetooth (so-called IoT applications). Both of these microcontrollers can understand multiple programming languages, from C++ to MicroPython and JavaScript. Looking over the datasheet, the ESP32 has higher power usage compared to the RP2040. There are many different variables to consider. Depending on the project, the main application must be taken into consideration.
The comparison of the two languages will happen on the ESP32-S3
For our comparison we are using a Script, which Leen Skaf provided. Its written in C/C++ and she translatet it in Micropython. Its a Script for reading sensor data from a DHT22 humidity and temperature Sensor.
#include
// Pin definition for Arduino Uno (using digital pin 2 for DHT22 sensor)
const int dhtPin = 2; // Pin 2 is used for the DHT22 sensor
// Define the DHT sensor type (DHT22 in this case)
#define DHTTYPE DHT22
// Create an instance of the DHT sensor
DHT dht(dhtPin, DHTTYPE);
void setup() {
// Initialize serial communication at 9600 baud
Serial.begin(9600);
// Start the DHT22 sensor
dht.begin();
}
void loop() {
// Read temperature in Celsius from the DHT22 sensor
float temperatureC = dht.readTemperature();
// Read relative humidity percentage from the DHT22 sensor
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again)
if (isnan(temperatureC) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
} else {
// Print the temperature and humidity values to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
// Wait 1 second before taking a new reading
delay(1000);
}
This simple code, reads temperature and humidity of the environment and displays the code
Output of the Arduino IDE Sketch uses 298996 bytes (8%) of program storage space. Maximum is 3342336 bytes.
Leen was so kind and also provided the Script in Micropython
%serialconnect
import machine
import time
import dht
import math
# Set up the DHT22 sensor on pin D3.
#Adjust the pin number to 3 for D3 on ESP32 C3
dht_pin = machine.Pin(3)
sensor = dht.DHT22(dht_pin)
while True:
try:
# Perform a sensor measurement.
sensor.measure()
temperature = sensor.temperature() # Temperature in Celsius.
humidity = sensor.humidity() # Humidity in percentage.
# Check if readings are valid.
if math.isnan(temperature) or math.isnan(humidity):
print("Failed to read from DHT sensor!")
else:
print("Temperature: {} °C".format(temperature))
print("Humidity: {} %".format(humidity))
except Exception as e:
print("Error reading sensor:", e)
time.sleep(1) # Wait 1 second before the next reading.
Programming Language | Memory Uptake | Memory Maximum | Percentage |
---|---|---|---|
Arduino | 298996 B | 3342336 B | 6 % |
Micropython | 4248 B | 3342336 B | 0,12 % |
As you can see the memory usage is completly different between MP and C/C++. The MP Code is 98,58 % smaller or is 1,42% of the size of the C/C++ Code
The Workflow for the ArduinoIDE with C/C++ is shown here
As you can see the Workflow with the Arduino IDE is much simpler. In the case of an ESP you just need to add the ESP32 Boards in the Boardmanager, install you librarys, paste the code, select the correct board and port and just upload. While working with Micropython you need to first flash a complete new firmware onto the board and upload the code via a terminal, which is not beginner friendly. But at the end, you can squeeze more code onto your board.