Scratch Visual Programming for ESP32
Scratch Visual Programming for ESP32
Overview
Section titled “Overview”This section introduces visual (block-based) programming tools for the ESP32. By the end of this section, you will be able to:
- Identify the major visual programming platforms that support ESP32
- Set up a block-based programming environment for ESP32
- Program an ESP32 using Scratch-like blocks
- Advise education buyers on visual programming options
Prerequisites
Section titled “Prerequisites”- Basic familiarity with Scratch or other block-based programming interfaces
- An ESP32 development board
Key Concepts
Section titled “Key Concepts”Why Visual Programming for ESP32
Section titled “Why Visual Programming for ESP32”Visual programming lowers the barrier to entry for ESP32 development. Instead of writing C++ syntax, users drag and drop logic blocks. This approach is particularly valuable for:
- K-12 Education: Students learn programming logic (loops, conditionals, variables) without syntax frustration
- Rapid Prototyping: Quickly test hardware ideas without writing code
- Non-Developer Audiences: Product managers, designers, and educators who need to understand ESP32 capabilities
- Workshop Settings: Short sessions where the focus is on hardware interaction, not coding
Available Visual Programming Tools
Section titled “Available Visual Programming Tools”mBlock (Makeblock)
The most mature Scratch-based platform for ESP32.
- Based on: Scratch 3.0
- Platform: Desktop (Windows/macOS) and Web
- ESP32 Support: Native via “mBuild” ecosystem or generic ESP32
- Features:
- Drag-and-drop blocks generate Arduino C++ code
- Switchable between block view and code view
- Built-in serial monitor
- Upload directly to ESP32
- Supports Wi-Fi and MQTT blocks
- Best For: Classroom education, K-12 STEM programs
Mixly (BNU/China)
A visual programming tool widely used in Chinese STEM education.
- Based on: Google Blockly
- Platform: Desktop (Windows/macOS/Linux)
- ESP32 Support: Yes, with board package
- Features:
- Generates Arduino C++ code
- Supports most common sensors and actuators
- Chinese and English interface
- Lightweight installation (~100 MB)
- Best For: Chinese education market, budget-constrained classrooms
ArduBlock
An older but still functional block programming plugin for the Arduino IDE.
- Based on: OpenBlocks
- Platform: Plugin for Arduino IDE (Java-based)
- ESP32 Support: Via Arduino IDE’s ESP32 core
- Features:
- Runs inside Arduino IDE
- Generates standard Arduino code
- Very simple interface (fewer blocks than mBlock)
- Best For: Transitioning from blocks to text within the Arduino IDE
Arduino PLC IDE (Visual Programming)
Arduino’s official visual programming environment for industrial applications.
- Platform: Desktop
- ESP32 Support: Via Arduino Portenta (not standard ESP32)
- Note: Not recommended for standard ESP32 education
Implementation Steps
Section titled “Implementation Steps”Step 1: Install mBlock for ESP32
Section titled “Step 1: Install mBlock for ESP32”- Download mBlock from mblock.makeblock.com
- Install the desktop application
- Launch mBlock
- Go to Devices tab and add a new device
- Search for “Arduino” or “ESP32”
- Select the appropriate board (e.g., “Arduino Uno” — mBlock uses this as generic code generator)
- Connect the ESP32 via USB
Note: mBlock generates Arduino C++ code. The compiled code is uploaded to the ESP32 via the standard Arduino upload process. You need the ESP32 Arduino Core installed (see 01-04).
Step 2: Create a Simple Blink Program
Section titled “Step 2: Create a Simple Blink Program”- From the Events category, drag “when Arduino starts” block
- From the Pins category, drag “set pin 13 to HIGH” block
- From the Control category, drag “wait 1 seconds” block
- From the Pins category, drag “set pin 13 to LOW” block
- From the Control category, drag “wait 1 seconds” block
- Add a “forever” loop block around steps 2-5
Blocks Arrangement:
when Arduino starts forever set pin 13 to HIGH wait 1 seconds set pin 13 to LOW wait 1 seconds- Click Upload to compile and upload to ESP32
- Observe the built-in LED (GPIO 2 on most ESP32 DevKits) blinking
Step 3: Add a Sensor Reading
Section titled “Step 3: Add a Sensor Reading”- Connect a DHT11/DHT22 sensor to the ESP32:
- VCC → 3.3V
- DATA → GPIO 4
- GND → GND
- In mBlock, add the “DHT11” extension from the Extensions menu
- Use the “read temperature from DHT11 pin 4” block
- Use the “read humidity from DHT11 pin 4” block
- Display the values using a “say” block or send to Serial
Blocks Arrangement:
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 secondsStep 4: View the Generated Code
Section titled “Step 4: View the Generated Code”In mBlock, switch to Code View to see the Arduino C++ code generated by the 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(13, 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 a powerful learning feature: students can see how their visual blocks translate into real C++ code, building a bridge 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”Board not detected in mBlock
Section titled “Board not detected in mBlock”Causes:
- USB driver not installed
- Wrong port selected
- mBlock Arduino connector not activated
Solutions:
- Install CP2102 or CH340 driver
- In mBlock, go to Connect and select the correct USB port
- Restart mBlock after installing the driver
Code compiles but does not upload
Section titled “Code compiles but does not upload”Causes:
- ESP32 board package not installed in behind-the-scenes Arduino IDE
- Wrong board selected for compilation
Solutions:
- 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
Blocks for specific sensor not available
Section titled “Blocks for specific sensor not available”Causes:
- Extension not installed
- Sensor not supported by the visual tool
Solutions:
- Check if an extension exists for your sensor
- 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”- Use block view for logic learning, code view for syntax transition: The biggest advantage of visual tools is showing the connection between blocks and code
- Start with mBlock for K-12: It has the best ESP32 support, largest block library, and most classroom resources
- Limit visual programming to introductory content: Students should transition to text-based programming once they understand logic concepts (typically after 4-8 sessions)
- Combine with physical computing: Visual programming is most engaging when connected to real LEDs, sensors, and motors
- For pre-sales demos: Visual programming is 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)