Ready to enjoy physical computing and have fun making IoT devices? MicroBlocks is the answer!
MicroBlocks is a coding environment for microcontrollers that’s human friendly, but more than that, it’s education friendly. Founder John Maloney worked on Scratch for a decade; his sidekick Bernat Romagosa founded Snap4Arduino, and the third musketeer, Jens Mönig, heads Snap! development, so MicroBlocks and Snap! can communicate using standard web services.
I got involved with MicroBlocks when I was developing the WebThings Gateway by Mozilla.
What’s so cool about MicroBlocks? It’s similar to Scratch and Snap!, where command blocks (rectangular) can be executed, and reporter blocks (rounded) can be evaluated, simply by clicking on them. MicroBlocks works the same way, except your commands control a microcontroller that can interact with the physical world. You can click a block to turn on LEDs, drive servos and motors, and play music. You can sense inputs from buttons, temperature sensors, accelerometers, and so much more. MicroBlocks scripting offers students what they expect — coding interaction with the physical world that’s live.
Other blocks-based physical computing environments, such as Snap4Arduino and the Scratch micro:bit extension, support live programming by running your code on a PC and sending commands to a microcontroller. But the microcontroller does nothing when detached.
With MicroBlocks, your code runs directly on the microcontroller. As you work, your code is incrementally compiled, downloaded, and stored in persistent Flash memory so it still works even when the microcontroller is untethered and powered by a battery pack. This portability and autonomy allows MicroBlocks projects to be worn, carried in a pocket, or deployed to collect data outdoors.
When MicroBlocks detects which board is connected to your PC, it will automatically pull in libraries to support the features of that board. For the BBC micro:bit, two libraries are automatically added to the blocks palette: Basic Sensors (reporter blocks) and LED Display (command blocks). When you attach external components (e.g., NeoPixels, or a distance sensor) you can manually add libraries to support those components. Above shows two core categories (Input and Pins), and two from added libraries (Basic Sensors and LED Display).
These libraries make physical computing so much easier. Trying them out is just a few clicks away.
For sensors and actuators external to the board, simply enter the pin number associated with the attached component. For example, Attach buzzer to pin (14) sets up your board to play musical tones or even songs, on a piezo speaker attached to pin 14. A favorite built-in example among students is the theme song for Harry Potter.
Different categories of blocks are color coded, making it easier to figure out what the blocks do. Core categories also include:
- Control: “when” hat blocks, conditionals and loops, time delays, broadcasts, comments
- Operators: math, expressions, logic
- Variables: to create local or global variables and set or change them
- Data: list and string functions
- My Blocks: to create your own command or reporting functions.
Outside these core functions, the rest of the blocks and libraries typically relate to physical computing. Actuators are controlled as Outputs. Sensors are monitored as Inputs. Communication can be done over both wired and wireless channels including I2C, SPI, peer-to-peer radio, HTTP, Wi-Fi, and infrared remote control. Above shows a snippet from a fun built-in example called RadioTexting.
One of the coolest aspects of MicroBlocks is the ability to plot data in real time! It lets you understand the output of a sensor so you can create algorithms. This image shows a plot of motion sensed from a micro:bit accelerometer while a person was walking. This live plotting made it possible to create a Step Counting library.
Turning Microcontrollers into IoT Devices
How do we teach the next generation to create smart, truly connected devices? IoT vendors like to tout “end-to-end” security, but that means data from your device go directly to the cloud, which disables interoperability between devices on your local network. And what if internet access is down? Or the cloud provider goes out of business? Who else can access your data?
Fortunately, MicroBlocks offers simple HTTP libraries and a Web Thing library that’s compatible with Snap! and the Mozilla WebThings Gateway. Students can easily create their own IoT devices, using HTTP as a ubiquitous interoperability, control, and monitoring layer. These free, open source tools provide complete privacy and standard web security, with no dependency on big tech titans and no need to ask permission for cloud subscriptions, input a credit card, or give up privacy by sharing data.
To see some easy web devices made with the BBC micro:bit and WebThings Gateway, read more at makezine.com/go/microblocks.
Low-Cost Boards, High Educational Value
What hardware can you program? Anything that can run the MicroBlocks virtual machine (VM). Several pre-compiled VMs come built into MicroBlocks, and can be installed on your microcontroller board via a menu command. For less common boards, you can build and install the VM using PlatformIO. Many boards from the following processor families are supported:
• Microchip (Atmel) SAMD21 (Arm Cortex M0)
• Nordic nRF51, nRF52 (Arm Cortex M0, M4)
• Espressif ESP8266 and ESP32 (Tensilica Xtensa)
Other boards in these families are often easy to add. For example, dozens of ESP8266 boards can run the pre-compiled NodeMCU VM, although some use different pins for the user LED.
Get Started with MicroBlocks
1. Download MicroBlocks for Windows, Mac, Linux, or Chromebook and follow the setup instructions there.
2. You can also run MicroBlocks in a Chromium-based browser such as Chrome or Edge. To connect to your board via the WebSerial API, visit chrome://flags and enable the Experimental Web Platform Features flag. (Assuming WebSerial becomes a standard feature of Chrome, that step will no longer be necessary.) Then just browse to microblocks.fun/run/microblocks.html.
3. Once you’ve got MicroBlocks, see microblocks.fun/learn for next steps. For educators and makers alike, the site offers excellent Activity Cards and Guided Activities to get you started.