iotdata, by Data Frontieriot
data
Block coding

From blocks to code: the Python and C++ behind the IDE

How each card in the block coding IDE becomes code, the difference between Arduino’s setup and loop and the MicroPython loop, and a side-by-side example of the same robot arm program in Python and C++.

Data Frontier Engineering· Published October 22, 2026

Each card in the block coding IDE is an instruction, and the IDE translates the list of cards into real code. The code panel shows that translation as you build. This article explains how it's done, for anyone who wants to understand what goes to the board and, one day, write code directly.

Two ways to organize a program

On Arduino (C++), every program has two required functions:

  • setup() runs once, when the board powers up. It's where you declare a pin as an output and attach the servo to its pin.
  • loop() runs again and again, forever. It's the robot's "repeat forever".

In MicroPython (ESP32), the main.py file runs from top to bottom when the board powers up. Whatever sits at the top is the setup; "repeat forever" becomes a while True loop.

The IDE makes this split on its own: setup cards go to the top, and whatever is inside "repeat forever" goes into the loop.

The same program in both languages

The program: repeat forever; the base servo follows the joystick; if the distance is less than 10 cm, beep.

In C++, for the Arduino Nano:

#include <Servo.h>

Servo servoBase;

long distanciaCm(int trig, int echo) {
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long us = pulseIn(echo, HIGH, 30000);
  return us == 0 ? 999 : us / 58;
}

void setup() {
  servoBase.attach(3);
  pinMode(7, OUTPUT);
  pinMode(8, INPUT);
  pinMode(10, OUTPUT);
}

void loop() {
  servoBase.write(map(analogRead(A0), 0, 1023, 0, 180));
  if (distanciaCm(7, 8) < 10) {
    tone(10, 2000, 200);
    delay(200);
  }
  delay(20);
}

In MicroPython, for the ESP32:

from machine import Pin, PWM, ADC, time_pulse_us
import time

servo_base = PWM(Pin(13), freq=50)
joy_x = ADC(Pin(32))
joy_x.atten(ADC.ATTN_11DB)
trig = Pin(18, Pin.OUT)
echo = Pin(19, Pin.IN)
buzzer = PWM(Pin(23), freq=2000, duty_u16=0)

def angulo(servo, graus):
    servo.duty_ns(500_000 + int(graus * 1_900_000 / 180))

def distancia_cm():
    trig.on(); time.sleep_us(10); trig.off()
    us = time_pulse_us(echo, 1, 30000)
    return 999 if us < 0 else us / 58

while True:
    angulo(servo_base, joy_x.read_u16() * 180 / 65535)
    if distancia_cm() < 10:
        buzzer.duty_u16(32768); time.sleep_ms(200); buzzer.duty_u16(0)
    time.sleep_ms(20)

The code the IDE generates may use different names and details, but this is the structure.

What the translation takes care of for you

  • Helper functions. Measuring distance takes five lines; the card is just one. The IDE writes the function once and calls it wherever the card appears.
  • Pin setup. Each pin used in a card gets its setup at the top, without repetition.
  • Conversions. An angle in degrees becomes a pulse width; the joystick reading becomes an angle; centimeters become the comparison threshold.
  • A pause in the loop. The 20 ms at the end of the loop give the servo time to move and keep the ultrasonic sensor from being read faster than it can handle.

Why show the code

Seeing the code next to the cards bridges the gap between blocks and text. "If" becomes if, "repeat forever" becomes loop or while True, and the number you typed shows up in the right place. Anyone who wants to go further can copy the code, open it in the Arduino IDE or in Thonny and carry on from there.

Build the example in the block coding IDE and switch the board: the code alongside changes language, and the cards stay the same.