Scratch Visual Programming for ESP32
Scratch Visual Programming for ESP32
Section titled “Scratch Visual Programming for ESP32”Prerequisites
Section titled “Prerequisites”- Basic familiarity with Scratch or other block-based programming interfaces
- An ESP32 development board
Key Concepts
Section titled “Key Concepts”What is visual programming?
Visual programming lets users build programs by dragging and dropping logic blocks instead of writing C++ syntax. Blocks represent programming concepts such as loops, conditionals, and variable operations — users simply combine them like puzzle pieces.
Why is visual programming a good fit for ESP32?
ESP32’s hardware interaction (LED blinking, sensor reading, Wi-Fi connection) lets students see the physical results of programming immediately. Visual programming is particularly valuable for:
- K-12 Education: Students learn programming logic without syntax frustration
- Rapid Prototyping: Quickly test hardware ideas without writing code
- Non-Developer Audiences: Product managers, designers, and educators quickly grasp ESP32 capabilities
- Workshop Settings: Short sessions focused on hardware interaction, not coding
What visual programming tools support ESP32?
| Tool | Based On | Platform | Code Generated | Best For |
|---|---|---|---|---|
| mBlock | Scratch 3.0 | Desktop/Web | Arduino C++ | K-12 STEM programs |
| Mixly | Google Blockly | Desktop (multi-OS) | Arduino C++ | Chinese education market |
| ArduBlock | OpenBlocks | Arduino IDE plugin | Standard Arduino code | Transitioning from blocks to text |
| Arduino PLC IDE | - | Desktop | - | Industrial (not recommended for ESP32) |
What makes mBlock stand out?
mBlock is the most mature Scratch-based platform for ESP32. It supports:
- Drag-and-drop blocks generating Arduino C++ code
- Switchable block and code views
- Built-in serial monitor
- Direct upload to ESP32
- Wi-Fi and MQTT block extensions
Mixly is the go-to choice for the Chinese education market (~100 MB lightweight install, Chinese/English interface). ArduBlock is best for users already working in Arduino IDE who want a gradual transition.
Implementation Steps
Section titled “Implementation Steps”How do I install mBlock and connect the ESP32?
- Download mBlock from mblock.makeblock.com and install the desktop application
- Launch mBlock, go to Devices tab, and add a new device
- Search for “Arduino” or “ESP32” and select the appropriate board (e.g., “Arduino Uno” — mBlock uses this as a generic code generator)
- Connect the ESP32 via USB
Note: mBlock generates Arduino C++ code uploaded via the standard Arduino upload process. You need the ESP32 Arduino Core installed first (see 01-04).
How do I create my first blink program?
Drag and drop the following blocks in order:
- From Events, drag “when Arduino starts”
- From Control, drag “forever” loop
- Inside the loop, add in sequence:
- Pins → “set pin 2 to HIGH”
- Control → “wait 1 seconds”
- Pins → “set pin 2 to LOW”
- Control → “wait 1 seconds”
when Arduino starts forever set pin 2 to HIGH wait 1 seconds set pin 2 to LOW wait 1 secondsClick Upload to compile and upload. The built-in LED on most ESP32 DevKits (GPIO 2) should start blinking immediately.
How do I add sensor reading?
Using a DHT11 temperature/humidity sensor as an example:
- Connect DHT11 to ESP32: VCC → 3.3V, DATA → GPIO 4, GND → GND
- In mBlock, search and add the “DHT11” extension from the Extensions menu
- Use the blocks inside a “forever” loop:
when Arduino starts forever set variable temperature to (read temperature from DHT11 pin 4) set variable humidity to (read humidity from DHT11 pin 4) Serial print (join "Temp: " temperature) Serial print (join " Humidity: " humidity) Serial print newline wait 5 secondsOpen the serial monitor to see live temperature and humidity data.
What C++ code is generated behind the blocks?
Switch to Code View — mBlock displays the Arduino C++ code corresponding to your blocks:
#include <Arduino.h>#include <DHT.h>
DHT dht(4, DHT11);float temperature = 0;float humidity = 0;
void setup() { Serial.begin(115200); dht.begin(); pinMode(2, OUTPUT);}
void loop() { temperature = dht.readTemperature(); humidity = dht.readHumidity(); Serial.print("Temp: "); Serial.print(temperature); Serial.print(" Humidity: "); Serial.print(humidity); Serial.println(); delay(5000);}This is the most powerful learning feature of visual tools: students can visually see how their blocks translate into real code, building a bridge from block-based to text-based programming.
Verification
Section titled “Verification”- mBlock (or chosen tool) successfully installs and detects the ESP32
- The blink program uploads and executes correctly
- The sensor reading program displays correct values in the serial monitor
- Generated C++ code can be viewed and understood
- The board can be switched between block and code views
Troubleshooting
Section titled “Troubleshooting”The board is not detected in mBlock — what now?
This is usually a USB driver issue:
- Install the CP2102 or CH340 driver (depending on your board’s chip)
- In mBlock, go to Connect and select the correct USB port
- Restart mBlock after installing the driver
Code compiles but fails to upload?
Check that ESP32 board support is installed in the Arduino IDE. mBlock relies on the background Arduino IDE for compilation:
- Manually install ESP32 board support in Arduino IDE first
- In mBlock, verify the upload settings point to the correct board
- Manually upload the exported
.inofile via Arduino IDE if needed
Blocks for a specific sensor are not available?
- Check if an extension exists for your sensor in the mBlock extension library
- Use the “custom block” feature to write a simple Arduino code snippet
- Switch to Arduino IDE or PlatformIO for unsupported sensors
Best Practices
Section titled “Best Practices”- Block view for logic learning, code view for syntax transition: The biggest advantage is showing the connection between blocks and code
- Start with mBlock for K-12: Best ESP32 support, largest block library, and most classroom resources
- Limit visual programming to introductory content: Transition to text-based programming once logic is understood (typically after 4-8 sessions)
- Combine with physical computing: Most engaging when connected to real LEDs, sensors, and motors
- Use for pre-sales demos: An excellent way to show non-technical buyers how ESP32 works without showing code
Summary
Section titled “Summary”- Visual programming tools (mBlock, Mixly, ArduBlock) enable ESP32 programming without writing C++ code
- mBlock is the most comprehensive option, supporting ESP32 with extensions for Wi-Fi, MQTT, and common sensors
- Visual tools generate standard Arduino C++ code — students can see the code behind the blocks
- Best suited for K-12 education, rapid prototyping, and non-developer audiences
- Visual programming should be a stepping stone to text-based programming (Arduino IDE or PlatformIO)
References
Section titled “References”We provide ESP32 ODM design-to-manufacturing services. From prototype to production — the team behind this tutorial can build it with you.
Talk to us →