Resources for Prototyping: The Basics of New Languages for Programming Microcontrollers
Microcontroller specifications have changed little over the last few decades, but the expansion of languages and supporting software has opened up many choices. Learn about several new languages available for microcontrollers in this guide.
Microcontroller specifications have changed little over the last few decades, but the expansion of languages and supporting software has opened up many paths for application development.
For years, programming microcontrollers involved a choice between two languages: C and Assembly. But with new languages now available for microprocessors, users face more decisions in their application design.
Which language is best? There's no clear answer because it depends on the application you're working on. To help you plan your development, here's a high-level exploration of different languages available for microcontroller use, as well as resources for further learning. You can also check out our previous article on languages for embedded systems, which covers Rust, Python, and VHDL and Verilog.
C and Assembly
Before new languages became available for microcontrollers, the two choices available were C and Assembly. Let's look at them briefly before covering new languages in depth.
C is a popular microcontroller language even though it was developed in the early 1970s. For use with microcontrollers, C functions well because it has the ability to handle memory allocation while also performing complex functions like if statements, loops, and mathematical expressions.
Assembly offers an alternative to C with a few advantages. Assembly is an older language that often uses code specific to the device, which is converted using an assembler utility program. Using this language, the user has direct access to the CPU where routines can be carefully adjusted for precise timing applications. Assembly offers better CPU usage, as well as further sets of instructions that C does not support such as bit manipulation with specific BIT instructions.
- Introduction to the C Programming Language for Embedded Applications
- Understanding Variables in C Programming
- Microprocessor Programming
- Assembly Language Tutorial
Python is a feature-rich interpreted language, known for its simplicity, clarity, and general-purpose adaptability. These features also make the language too large for microprocessors. However, with the help of a Kickstarter campaign in 2013, physicist Damien George was able to adapt Python into a smaller version, MicroPython, that is suitable for use on a microcontroller.
MicroPython code is open-source under the MIT license. The language runs on the bare metal of the microprocessor using a small subset of the standard Python library. It can operate using only 256k of space and 16k RAM, yet it is designed to be as fully compatible with regular Python as possible.
A diagram of the PyBoard, MicroPython's official microcontroller board. Image courtesy MicroPython
Originally designed to run on ARM-based systems including the BBC micro:bit, Adafruit Circuit Playground Express, and the MicroPython PyBoard, it has also been made available for other processors including the ESP8266 and ESP32.
MicroPython shines in prototyping complex projects. On the ESP8266, for example, creating IoT projects can be done with a few simple function calls to get a MicroPython project connected to a local Wi-Fi point and start streaming information to a MQTT broker.
- pyboard overview video
- MicroPython forum
- pyboard simulator
- Use MicroPython online
- Introduction to MicroPython and PyBoard, Part 1: How It Works
Blockly is an open-source programming library supported by Google, aiming to make code visually logical by using drag-and-drop blocks.
Rather than relying on words and phrases that make no sense in normal conversation, users place blocks into an environment and connect them together to define how a program flows. For example, instead of writing an IF statement, users drag the IF block into the workplace and resulting actions are also dragged inside the IF statement block.
Screenshot of Blockly interactive demonstration, courtesy of Google for Education
While this programming method is available on many platforms including the Micro Bit and the Adafruit Circuit Playground Express, it is not designed to be used for a final commercial product. However, it is very useful in prototyping stages, especially if a proof-of-concept is needed and learning a device's specifics would take too long (such as registers, configuration bits, and IDE configuration).
While it's often referred to as the "Arduino programming language", Arduino is actually programmed in good old C++ and uses a subset of libraries specific to the Arduino.
The differences in Arduino C++ from regular C++ include generic routines for microcontrollers, since standard library C++ functions can't operate on their limited power and memory.
The Arduino Uno board. Image courtesy of Arduino.
Using this smaller version of C++ makes this language highly accessible, as many designers are already familiar with regular C++. In fact, by using a compiler, code can be written in C++ and then interpreted into code that is usable by the Arduino.
The advantage of using Arduino C++ is that it is suitable for commercial environments, is faster than larger languages such as Python, and is highly portable. Compiling code on an ARM core originally for the Uno is automatically handled by the library with the use of generic functions.
BASIC is a language that first appeared in the 60s and saw its heyday with the rise of the personal computer in the 80s. BASIC (an acronym for Beginner's All-Purpose Symbolic Instruction Code) still remains in use still today due to its simplicity and interpreted nature, which allows a designer to focus more on the hardware and less on the software.
When used on a microcontroller, BASIC is interpreted which causes a performance penalty. However, the language has an advantage in that it is highly portable. Any code written in BASIC will work on any device that has a BASIC interpreter.
Image courtesy of Charles R. Hampton.
Two examples of BASIC being used on microcontrollers are BASIC Stamp (produced by Parallax) and PICAXE which are produced by PICAXE and use Microchip PIC microcontrollers. BASIC is a good language for prototyping and even hardware development but should be avoided in commercial products due to its slow speed and energy consumption.
When working with microcontrollers in the past, a designer had to choose their preferred platform and then learn the language it uses. With advancements in languages and software, these days the language can decide which platform a designer uses. If a designer is looking for a specific feature, such as GPIO support, networking, ability to communicate over SSH, or others, then the choice of language and systems can be important factors in finding the best development path.
The language that users choose is important, but it's always good to remember that there is no such thing as “the single best language” or “the single best microcontroller”. In the end, the best choice is whatever the application calls for.