A New Method for Microcontroller Programming: MicroBlocks

Translated and organized from: https://makezine.com/2020/07/31/microcontrollers-meet-microblocks/

A New Method for Microcontroller Programming: MicroBlocksAuthor Introduction Kathy Giori is a senior product manager at Mozilla; she has promoted open-source hardware and software at Arduino, Qualcomm, and various startups. She holds electrical engineering degrees from the University of Minnesota and Stanford University.

The latest issue (vol 74) of Make magazine features the theme “The Rise of Python”

A New Method for Microcontroller Programming: MicroBlocks

In addition to Python, it also introduces MicroBlocks as “A New Way to Code Microcontrollers”. MicroBlocks is a development environment for microcontrollers that is not only user-friendly but also suitable for education. When you program, your code is compiled, downloaded, and stored incrementally in the microcontroller’s flash memory, allowing it to run even when powered by batteries. This portability and autonomy make MicroBlocks projects suitable for wearable products, pocket-sized devices, or outdoor deployments for collecting sensor data.

A New Method for Microcontroller Programming: MicroBlocks

The founder of MicroBlocks, John Maloney, worked on Scratch for ten years. His partner, Bernat Romagosa, founded Snap4Arduino, and the third key person, Jens Mönig, led the development of Snap!, allowing MicroBlocks to communicate using standard web services and Snap!.

Improving Learning Through “Real-Time” Coding

What’s fun about MicroBlocks? It resembles Scratch and Snap!, featuring command blocks (rectangles) and report blocks (rounded rectangles) that can be executed by clicking on them. MicroBlocks operates similarly, but these command controls can interact with the physical world through the microcontroller. You can click a block to turn on an LED, motor, or play music, or receive inputs from buttons, temperature sensors, accelerometers, etc. MicroBlocks scripts provide students with the programming interaction they expect with the real world.

Other block-based physical computing environments (like Snap4Arduino and Scratch micro:bit extensions) support real-time control by running programs on PCs and sending commands to the microcontroller; however, when not connected to a computer, the microcontroller does nothing.

With MicroBlocks, your code runs directly on the microcontroller. As you program, the program is incrementally compiled, downloaded, and stored in flash memory, so even if the microcontroller is not connected to a computer and is powered by batteries, it still works. This portability and autonomy make MicroBlocks projects suitable for wearable products, pocket-sized devices, or outdoor deployments for collecting sensor data.

A New Method for Microcontroller Programming: MicroBlocks

When MicroBlocks detects that the board is connected to your computer, it will automatically import the corresponding libraries. For the BBC micro:bit, two libraries are automatically added: basic sensors and LED display. When you connect external components (like NeoPixels or distance sensors), you can manually add libraries to support these components. The above image shows two core categories (input and pins), along with basic sensors and LED display.

These libraries make physical computing very easy. You can use them with just a few clicks:

A New Method for Microcontroller Programming: MicroBlocks

For sensors and actuators outside the board, simply enter the pin number associated with the connected component. For example, attach buzzer to pin (14) will connect pin 14 to play music or even songs on the speaker; the built-in example is the students’ favorite Harry Potter theme song:

A New Method for Microcontroller Programming: MicroBlocks

Different categories of blocks use different colors to make it easier to distinguish the functions of the blocks. Core categories include:

  • Control: “when”, conditions and loops, delays, broadcasts, comments

  • Operations: math, expressions, logic

  • Variables: create local or global variables, set or change them

  • Data: list and string functions

  • My Blocks: create your own block functions.

A New Method for Microcontroller Programming: MicroBlocks

Beyond these core functionalities, the remaining blocks and libraries are typically related to physical computing. Actuators serve as outputs, while sensors serve as inputs. Communication can occur through wired and wireless methods (including I2C, SPI, peer-to-peer radio, HTTP, Wi-Fi, and infrared remote control). The above shows an interesting built-in example of RadioTexting.

A New Method for Microcontroller Programming: MicroBlocks

One of the coolest features of MicroBlocks is the ability to plot data in real-time! It allows you to visualize sensor outputs, making it possible to create algorithms. The image shows the data curve of the micro:bit accelerometer while a person is walking. Real-time plotting enables the creation of a “step counting” library.

Turning Microcontrollers into IoT Devices

How do we teach the next generation to create smart, truly interconnected devices? IoT vendors love to boast about “end-to-end” security, but this means data from devices goes directly to the cloud, disabling interoperability between devices on the local network. What happens if internet access is interrupted? Or if the cloud provider goes out of business? Who else has access to your data?

Fortunately, MicroBlocks provides a simple HTTP library and a Web Thing library compatible with Snap!, as well as the Mozilla WebThings gateway. Students can easily create their own IoT devices, using HTTP as a ubiquitous layer for interoperability, control, and monitoring. These free open-source tools offer complete privacy and standard web security, without relying on big tech giants or needing to apply for cloud subscription licenses, enter credit card information, or sacrifice privacy by sharing data.

To see some simple web devices made using BBC micro:bit, Citilab ED1, and the WebThings gateway, refer to the section below on “Building Web Things with MicroBlocks”.

Low-Cost Boards with High Educational Value

A New Method for Microcontroller Programming: MicroBlocks

What hardware do you program with? Any that can run the MicroBlocks virtual machine (VM). MicroBlocks comes with some commonly used precompiled VM firmware that can be installed on microcontroller boards through menu commands. 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, ESP32 (Tensilica Xtensa)

Other boards of these hardware types are usually easy to add. For example, dozens of ESP8266 boards can run the precompiled NodeMCU VM, although some boards may use different pins for the user LED.

Partially Supported Chips and Development Boards for MicroBlocks

A New Method for Microcontroller Programming: MicroBlocks

Due to the length of the original text, it will be published in two parts. The community will also detail the use of MicroBlocks on different development boards soon.

  • Read PS/2 keyboard with micro:bit

  • TileCode: Direct programming on MakeCode Arcade devices

  • Creative DIY project sharing (3)

Leave a Comment