KevsRobots Learning Platform
24% Percent Complete
By Kevin McAleer, 7 Minutes
Moving from Arduino C++ to MicroPython means unlearning a few habits and learning new ones. The good news? Python syntax is generally simpler and more readable.
The three biggest changes:
Letâs dive into each difference with side-by-side examples.
In Arduino C++, every statement ends with a semicolon. Forget one, and your code wonât compile. Python doesnât use semicolons at all.
Arduino C++:
int ledPin = 13;
int brightness = 128;
digitalWrite(ledPin, HIGH);
MicroPython:
led_pin = 13
brightness = 128
led.value(1)
Notice how much cleaner Python looks? Each line is a statement - no semicolons needed. Python uses line breaks to separate statements.
Common mistake: Adding semicolons in Python (theyâre technically allowed but unnecessary and considered bad style).
In C++, you must declare the type of every variable. In Python, the type is inferred automatically.
Arduino C++:
// Must declare types explicitly
int counter = 0;
float temperature = 23.5;
bool isRunning = true;
String message = "Hello";
char letter = 'A';
MicroPython:
# Types are inferred automatically
counter = 0 # int
temperature = 23.5 # float
is_running = True # bool (note: capital T!)
message = "Hello" # str
letter = 'A' # str (single chars are also strings)
Key differences:
True and False (capitalized!)snake_case in Python (convention), not camelCasePython variables can change type during runtime. This is powerful but requires care:
MicroPython:
value = 42 # int
print(value) # 42
value = "Hello" # Now it's a string!
print(value) # Hello
value = 3.14 # Now it's a float!
print(value) # 3.14
You canât do this in C++ - once an int, always an int. Pythonâs flexibility is useful but can cause bugs if youâre not careful.
Comments work similarly, with one syntax change:
Arduino C++:
// Single-line comment
int x = 10; // End-of-line comment
/* Multi-line comment
can span many lines
like this */
MicroPython:
# Single-line comment
x = 10 # End-of-line comment
"""Multi-line comment
can span many lines
like this"""
# Or use multiple single-line comments
# which is also common
# in Python code
Most Python programmers use # for everything, even multi-line comments. The triple-quote style ("""text""") is technically a string literal, not a comment, but it works the same way.
In C++, curly braces {} define code blocks. Indentation is just for readability. In Python, indentation IS the syntax. No braces needed.
Arduino C++:
void setup() {
pinMode(13, OUTPUT);
if (temperature > 25) {
digitalWrite(13, HIGH);
Serial.println("Hot!");
}
}
// Bad indentation, but still works:
void loop(){
if(temperature>25){
digitalWrite(13,HIGH);
}}
MicroPython:
# Indentation defines the block
from machine import Pin
led = Pin(13, Pin.OUT)
if temperature > 25:
led.on()
print("Hot!")
# Bad indentation = syntax error!
if temperature > 25:
led.on() # ERROR: IndentationError
Indentation rules:
: start indented blocksStrings are much easier in Python:
Arduino C++:
String name = "Arduino";
String greeting = "Hello, " + name + "!";
int length = greeting.length();
String upper = greeting;
upper.toUpperCase();
MicroPython:
name = "Arduino"
greeting = f"Hello, {name}!" # f-string (formatted string)
# or: greeting = "Hello, " + name + "!"
length = len(greeting)
upper = greeting.upper()
Pythonâs f-strings (formatted strings) are incredibly powerful:
temperature = 23.5
humidity = 67
# Arduino way
message = "Temp: " + str(temperature) + "C, Humidity: " + str(humidity) + "%"
# Python way
message = f"Temp: {temperature}C, Humidity: {humidity}%"
Much cleaner!
Converting between types is straightforward in both languages:
Arduino C++:
int number = 42;
String text = String(number);
float decimal = float(number);
String numText = "123";
int parsed = numText.toInt();
MicroPython:
number = 42
text = str(number)
decimal = float(number)
num_text = "123"
parsed = int(num_text)
Pythonâs conversion functions are simpler: int(), float(), str(), bool().
Serial output is much simpler in Python:
Arduino C++:
Serial.begin(9600);
int value = 42;
Serial.print("Value: ");
Serial.println(value);
MicroPython:
value = 42
print(f"Value: {value}")
# No setup needed - print() just works!
Arduino uses const or #define. Python uses naming convention:
Arduino C++:
const int LED_PIN = 13;
#define MAX_TEMP 30
MicroPython:
# Use ALL_CAPS for constants (convention, not enforced)
LED_PIN = 13
MAX_TEMP = 30
# These can technically be changed (Python doesn't enforce const)
# but ALL_CAPS signals "don't change this"
Letâs convert a complete Arduino snippet:
Arduino C++:
// Sensor reading
int sensorPin = A0;
int ledPin = 13;
int threshold = 500;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int sensorValue = analogRead(sensorPin);
Serial.print("Sensor: ");
Serial.println(sensorValue);
if (sensorValue > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(1000);
}
MicroPython:
# Sensor reading
from machine import Pin, ADC
import time
sensor_pin = ADC(Pin(26)) # ADC0 on Pico
led_pin = Pin(25, Pin.OUT)
threshold = 32000 # 16-bit ADC value
while True:
sensor_value = sensor_pin.read_u16()
print(f"Sensor: {sensor_value}")
if sensor_value > threshold:
led_pin.on()
else:
led_pin.off()
time.sleep(1)
Key changes:
setup() and loop() - just regular codeprint() instead of Serial.print()snake_caseExercise 1: Spot the Syntax Errors
This MicroPython code has syntax errors from C++ habits. Find them:
from machine import Pin;
import time;
led_pin = Pin(25, Pin.OUT);
while True
led_pin.on();
time.sleep(1);
led_pin.off();
time.sleep(1);
Answer:
Errors:
while TrueCorrected:
from machine import Pin
import time
led_pin = Pin(25, Pin.OUT)
while True:
led_pin.on()
time.sleep(1)
led_pin.off()
time.sleep(1)
Exercise 2: Convert Arduino to Python
Convert this Arduino code to MicroPython:
int buttonPin = 2;
int ledPin = 13;
bool buttonPressed = false;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
buttonPressed = digitalRead(buttonPin);
if (buttonPressed) {
digitalWrite(ledPin, HIGH);
}
}
Answer:
from machine import Pin
button_pin = Pin(2, Pin.IN)
led_pin = Pin(13, Pin.OUT)
while True:
button_pressed = button_pin.value()
if button_pressed:
led_pin.on()
Exercise 3: Practice F-Strings
Create a MicroPython program that reads a sensor value and prints:
Try using an f-string!
Answer:
from machine import Pin, ADC
import time
sensor = ADC(Pin(26))
while True:
value = sensor.read_u16()
voltage = (value / 65535) * 3.3
print(f"Sensor: {value} ({voltage:.2f}V)")
time.sleep(1)
You can use the arrows â â on your keyboard to navigate between lessons.
Comments