Scratch Your Itch To Program A Microcontroller

One of the fun things about “old school” computers is that it was fairly easy to get kids into programming them. The old Basic interpreters were pretty forgiving, and you could do some clever things easily with very little theory or setup. These days, you are more likely to sneak kids into programming via Scratch — a system for setting up programs via blocks in a GUI. Again, you can get simple results simply. With Scratch or Basic, complex things have a way of turning out complex, but that’s to be expected. If you want to try a Scratch-inspired take on microcontroller programming, check out MicroBlocks. It will work with several common boards, including the micro:bit and the Raspberry Pi Pico. You can use it in a browser or download versions for Linux, Windows, Mac, or even Chromebooks.

You can see a video below about the micro:bit version from a year ago. The tool is advancing, so you’ll find many new features compared to the video, but it will still give you an idea of what’s happening.

We were impressed with the range of blocks and libraries including like I2C, SPI, and NeoPixel. You can graph data easily. We were unable, sadly, to make the browser version work with an ESP32 S3 module we had sitting on the desk. The tutorials have videos and snippets of graphical “code” you can drag directly into the IDE.

We aren’t sure if it was a configuration issue or if the S3 isn’t supported. Or it could have been simple operator error. The web page says it can be “tricky” to get the initial firmware on the ESP32, and — for us — it was apparently too tricky. We even grabbed some firmware images from GitHub and flashed them. We also tried building the smallvm component from scratch, but getting it to build for the S3 took some modifications and uncovered some typos in the code base. It did work in the end and it worked as advertised for a “stock” ESP32 development board.

We get it. You don’t want to shower kids with cryptic error messages. However, there didn’t seem to be many options for troubleshooting or for a teacher or coach to just flash the image in some regular way. There was a GUI — we think — to flash the base firmware on GitHub but with little documentation.

But if you have a supported board, it looks great. We know that block programming isn’t the language of choice for most Hackaday readers. But if you are looking for a gateway drug for a friend or a child, this could well be it.

You shouldn’t write kernel modules in Scratch, either, but people have. If you’d rather stick with Python, you can do that, too.

19 thoughts on “Scratch Your Itch To Program A Microcontroller

  1. What about an artist who needs a specific microcontroller function that is simple but just works? Or someone who wants to use an AI based microcontroller for their specific use but does not even program at all? Or even just an adult who is new to programming? Or a visual learner? Or someone that does program but wants to create a fairly simple overall program quicker than they could physically type it all out by just dragging a few things around instead of writing code to get something at least testable?

    Would be nice if it could also build in some error checking at least which this seems to lack at the moment.

    Anyway, every time you see one of these apps or programs posted here, it seems like there are always assumptions made about how the target audience is always a minor who has never programmed before or is still learning.

    Not everyone who is good with things like hardware is also good at software programming and not everyone who is an adult, let alone a programmer knows how to program multiple higher level languages.

    1. I studied electrical engineering, so I greatly appreciate getting things done without getting stuck in the software. MicroBlocks is my fave microcontroller tool (I volunteer to teach it as well). If a board isn’t supported by MicroBlocks then I wouldn’t want it. ;)

      It’s a dream come true for Makers of any age who just want to build and create things or instrument the world with sensors to understand things better. I used MicroBlocks to build a “smart Christmas tree” for my granddaughter and she loved to push buttons to change ornament LED colors and patterns.

      There’s even “boardie” available now if you don’t have real hardware, to get a taste. But we do recommend using real hardware. Just try it. :)

    2. AI at this stage can only write very basic code for a few select microcontrollers. From what I have seen it can’t handle much that you wouldn’t find a tutorial for and even then it can have mistakes.

      It really doesn’t take much to learn enough Arduino to write the type of programs you are on about and current AIs can’t really write complex code for microcontrollers. So anything complex, like art projects tend to get, with loads of motors or leds, aren’t well suited to be written by an AI. Current AI also doesn’t seem to be much good at writing code to use things that aren’t built into the microcontroller like sensors or other peripherals.

      Just using an AI to write code also doesn’t really teach you anything, creating it using a blocky language like scratch or even something like node-red is better since you actually have to think about what you are doing and learn something. AI at this stage will not replace programmers, if an artist has a complex program they need written they will still need to get someone else to write it.

  2. > We know that block programming isn’t the language of choice for most Hackaday readers

    Well, I was taught “structured programming” with blocks like Scratch uses drawn at the blackboard, back in the 90s at a community college.

    We had if, while…do, do…while,for,switch case,input and output blocks. The only one missing was “evil” go-to

    So I was very pleased surprised when I saw my kids using Scratch some years ago. Same blocky programming approach, however made easier for them

      1. Taught by good faith C/C++ teachers, that intended new programmers not falling in the bad habit of messy/hard to follow code.
        Decades later I learnt by experience that a goto here or there (i.e. during error checking part of a function) may indeed help making precisely that goal:not messy code.
        And I appreciate having such teachers at that time.

  3. Great to see this article about MicroBlocks on Hackaday!

    A distinguishing feature of MicroBlocks is liveness: the fact that blocks and scripts can be run immediately as you work, right on the microcontroller. You don’t need to compile and download your program to test it; just click on a block or script and it runs. Liveness helps beginners, especially young ones, get results right away and it helps them stay engaged as they work. Liveness also makes it easy for more experienced users to quickly build understanding about sensors and other devices.

    Unlike some live systems — such as the Scratch extensions for microcontrollers — MicroBlocks programs execute right on the microcontroller. Once programmed, you can disconnect the microcontroller from the laptop, plug in a battery, and the program runs autonomously. That’s great for both fun things (e.g. light-up clothing and jewelry) and useful things (IoT applications). My seventeen-unit condo building’s heating system is controlled by an ESP32 programmed in MicroBlocks.

    MicroBlocks has full support for a handful of boards used in K-12 education around the world, such as the micro:bit and Raspberry Pi Pico. The firmware for those boards can be installed directly from the MicroBlocks editor (webapp or downloaded app) without additional tools. However, for users willing to download the free PlatformIO command line tools, MicroBlocks can be installed on over 40 boards. (See Adding a new board starts by adding a fews lines to the platformio.ini file and running PlatformIO to compile and install.

    MicroBlocks has not yet been tested on an ESP32-S3 but it has been tested by a community member on a Lolin S2 mini.

    MicroBlocks includes libraries for the DFRobot HuskyLens and the Elecfreaks Smart AI Lens, products that combine a camera with a dedicated vision recognition system.

    MicroBlocks is a non-profit with an educational mission. The software and associated educational materials are free and open-source. We hope you find it useful.

    1. Yes I did get the S3 running although I wasn’t sure I did it right. And I don’t think I got all the reserved pins correct? But it wasn’t that hard to do, but probably harder than your target audience is willing to do lol

      1. Great that you got MicroBlocks running on the S3. Congratulations on being the first person to port MicroBlocks to a RISC-V processor. :-)

        Once you’ve got the basics working for a new board, a few tweaks to the source code (mostly in ioPrims.cpp) are needed to define the pin mapping, including the user LED, user buttons (if any), and any built-in features such as a speaker, NeoPixels, etc.

        You’re right, porting MicroBlocks to a new board is not something we expect anyone in our target audience to do.

        However, once support for a new board has been added to the code base, folks who are comfortable with the command line — including many Hackaday readers — can build and install the MicroBlocks virtual machine on over 30 “community supported” boards. Those boards are in addition to the ~10 fully supported boards on which precompiled firmware be installed directly from the MicroBlocks editor.

        Although the primary target audience of MicroBlocks is younger learners and their teachers, we also have a smaller secondary audience of technical users who find MicroBlocks useful for rapid prototyping, exploring new sensors and other hardware, and general tinkering. Many of our community supported boards were contributed by folks in that group.

        FYI, I’m the lead developer of MicroBlocks. I hope people find it useful and fun!

  4. No software module >one page of code for compliance with Boeing hardware engineers’ software standards?

    Each module SHALL
    1 perform a simple intended function.
    2 avoid performing any adverse or benign unintended functions.
    2 provides adequate warning in event of failure.

    Software engineering vs computer programming?

    1. Next you are going to complain that Thomas the Tank Engine doesn’t meet FTA rail safety standards?

      It’s for kids, learning the ABC of programming: if-then-else, loops, variables, etc. The English literature course comes later.

  5. It should be made clear to newcomers in the MCU world that, in order to code using C, a lot of the time there’s not one IDE that does all for you. You have to install and organize a series of tools. Then to find out out that this version is not compatible with the other version, that this tool has a certain incompatibility with the new version of the other but if you install the previous it is ok. That you have to be familiar with the usage of these tools. Then to realize that, if u want to use a display, you have to build the driver under the same path where a hundred parameters might have an issue. The “then” being days browsing forums.
    Low-cost boards sometimes may contain high human-hour costs.

    It is understandable why a lot not-young would prefer this simplistic tool which executes code in the board within minutes, having also a portable version.

  6. Nice to see MCU tools akin to Apple Swift Playgrounds and MIT App Inventor for mobile devices. Hopefully Scratch based tools provide developers the option to export of projects to more traditional development environments.

    1. Actually I represent the type of user described by Alc in the comment above this one. Why fumble and bumble with the hoards of tools out there (except maybe PlatformIO that rocks!) when someone else will do that for you and deliver a playground of a hardware-abstracted and super-intuitive method to be creative? In this fast-growing space of physical computing (including most of IoT), you can participate whether young or old or in between. The need to “export” something from MicroBlocks back into the old cumbersome ways is not for me. If MicroBlocks wanted to attract industry prototypers or serious scientists instead of young learners, it could just change the “wallpaper” of the MicroBlocks IDE design and make it look like a metallic “low code” environment (i.e., user-friendly functions/libraries that are human-readable for first-timers). I’d actually love to see someone do that so we could begin to reduce the stigma that we should be going back to using less-friendly bare-metal tools. Yes, someone has to do the hard work of building a virtual machine for a new MCU/board in order for MicroBlocks to work, but fortunately, it doesn’t have to be me. I encourage both creative and super technical folks to try MicroBlocks!

      1. I can see the huge benefits in programming on a higher abstraction level. We’re currently developing industrial components using the ESP32-S3 programmed in C.

        We are booting one core in bare metal mode to avoid the timing jitter introduced by FreeRTOS installing hooks on the ISR vectors and generally slow task switching (around 15 microseconds). To that extent we have developed our own PicoKernel that handles all the state machines in our system. Each state machine is like a task running in parallel with other state machines. The state machines also synchronize via events.

        The bare metal core running PicoKernel handles all the timing critical stuff like running smooth FOC of a BLDC motor, and the FreeRTOS core runs all the non timing critical stuff like application, CAN-bus, WiFi and Bluetooth etc.

        The biggest performance boost is the way task switching is handled. In a traditional RTOS like FreeRTOS the way each task remembers the history of program execution is by preserving and restoring all the CPU registers, stack pointer and program counter at each task switch.
        For each register you need 4 read and write operations:
        – Read register content
        – Write to current task preserve memory
        – Read from new task preserve memory
        – Write to register

        In case of the ESP32-S3 there are a maximum of 132 registers to be preserved and restored at each task switch: PC,SP,ALU status,64AR,FP status,16FR,4MAC,44Q. That’s a total of 528 read & write operations (in the old ESP32 it’s less because it doesn’t have the 44 Q-registers for the AI acceleration instructions).
        In contrast the history or memory of a state machine system is the current state of each state machine in the system. A state change takes 1 write operation where you overwrite the old state pointer with a new state pointer. That makes task switching very fast.

        Our PicoKernel takes up 300 bytes of code when compiled including the central event handler and support functions like signal event, start or stop timer and a tick ISR (timers are delayed events). The C source code fits on one sheet of A4 paper and the support functions fit on the back side.
        In contrast the FreeRTOS takes up 17kB of code memory and a lot of source code files.

        It would be nice if we could combine the high abstraction level graphical state machine programming of the state machines with equally high abstraction level graphical programming of the small mainly linear pieces of action code triggered by the events in the state machines.

        Could MicroBlocks fill that purpose?
        My main concern is: How does the speed of execution performance compare to compiled C-code?

        I can see that you have included event driven programming in MicroBlocks, but it doesn’t seem to include any support for state machines. I’m afraid that global events without states won’t cut it in our applications and I’d really hate going back to manually coding event handlers. It’s so nice to just design the state machines on a graphical level and then leave the execution of them to the PicoKernel. It also removes a lot of “spaghettiness” from the code and makes it simple.

        It would be really nice if we could program the action code on a graphical level as well, without taking a large performance hit.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.