This article is part of MicroPython for Embedded Systems in the MicroPython for Embedded Systems
There are currently around 600 programming languages to choose from, so picking the one that’s right for you can be pretty difficult. But if you’re looking for a language that’s incredibly popular, has a low barrier of entry, and can easily be integrated with a wide variety of other languages, then Python is arguably your best bet right now. Python is the second most in-demand programming language as of 2020, and it might even end up taking the crown from JavaScript one day in the near future.
But while Python can be used for anything from web hosting and software development to business applications and everything in between, it can’t run on microcontrollers, which somewhat limits its capabilities. Luckily, an intrepid programmer took care of this little issue a few years back when he came up with MicroPython. Just as its name suggests, MicroPython is a compact version of the popular programming language that was designed to work hand-in-hand with microcontrollers.
In this tutorial, we’re going to teach you everything you need to know about microcontrollers and discuss the benefits of using MicroPython over other boards. There’s quite a bit to unpack here, but before we jump into the nitty-gritty of things, let’s take a trip back in time and see where the idea behind MicroPython came from.
MicroPython History and Overview
The year is 2013. Damien George, an undergraduate at Cambridge University at the time, launches a Kickstarter campaign that promises to bring Python to microcontrollers, which would finally allow for quick and painless hardware programming. The crowdfunding campaign was a huge success from the get-go. Damien was only asking for a rather modest sum of £15,000 to turn his proof of concept into a working product, complete with reference hardware. However, by the end of the Kickstarter, the programmer managed to raise close to £100,000 for the project thanks to over 1,900 backers.
The project evolved over the years and Damien’s reference hardware eventually become the Pyboard (see figure), a small electronic circuit board that runs MicroPython on the bare metal. While you don’t necessarily need a Pyboard in order to use MicroPython, the microcontroller is one of the best and easiest to work with when it comes to hardware programming.
The board is currently available in a few different versions, with prices generally ranging between $20 and $35, though some are a bit more expensive. If you’re not looking to invest in a microcontroller just yet, you can go to MicroPython Live to play around with a board for free in an online environment.
MicroPython offers some very exciting possibilities for hardware-programming experts and beginners alike. That’s because, unlike regular Python, MicroPython can seamlessly integrate with circuits, buttons, sensors, LCD displays, and various other electronics. Not only that, but MicroPython requires far fewer resources and doesn’t have to rely on an operating system because MicroPython itself acts as the OS for the Pyboard or any other microcontroller.
Essentially, all you need to set up a MicroPython project is compatible hardware and some coding skills. The latter may seem like a problem if you’re not a coder. But don’t worry, because MicroPython is an open-source project that’s supported by a very passionate and helpful community. If you have a specific project in mind, you can generally expect to be able to find code libraries and tutorials from developers that can help you bring it to life. Usually developers use services from providers such as Bluehost or AWS to host their open-source projects, so you will have access to them at any time.
MicroPython may have started as a one-man effort, but nowadays the project is supported by a large community of programmers, hobbyists, and even major organizations like the European Space Agency, who helped fund Damien’s work.
Physical Computing
Whereas regular Python is one of the best scripting languages for software programming, MicroPython is perfect for anyone interested in physical computing. The term physical computing can refer to lots of things when used in a broad sense, but as far as this tutorial is concerned, we’re primarily going to use it to describe the interactive systems and devices created with the help of MicroPython. Regardless of which hardware programming project you plan to tackle, you’ll need to take into account the following three main elements:
1. Input: This can be a button, sensor or anything else that allows you to give the microcontroller a command or a signal.
2. Processing: The microcontroller itself, which process the input and delivers one or multiple outputs.
3. Output: Can come in the form of any device that sends data from the microcontroller to another device or directly to the user.
In addition to the three elements described above, you will also need some type of power source and, depending on the project, wires that connect everything together.
What Exactly is a Microcontroller?
A microcontroller can be described as an integrated circuit that controls a device or a system. You can look at it as being the equivalent of a small computer that’s less powerful than a regular desktop computer, but much more compact. Because of their small size, microcontrollers can easily be embedded into a wide variety of systems, including air-conditioning systems, medical devices, home appliances, radios, vending machines, vehicles, and even robots, to name just a few examples.
Unlike regular computers, microcontrollers don’t require an entire board of chips to get the job done. Instead, they come in the form of an all-purpose chip that contains a processing unit (CPU), memory, and one or multiple I/O (input/output) ports. Similarly, a microcontroller doesn’t need a front-end operating system either, because they come complete with specialized software known as firmware. In the case of MicroPython boards, this software comes in the form of a small subset of the Python standard library.
How Does a Microcontroller Work?
The basic concept is similar to that of a regular computer. The microcontroller receives data via its I/O ports and processes it using its CPU. Since a microcontroller doesn’t have a permanent storage unit like a hard drive or an SSD, all of the received data is temporarily stored inside the microcontroller’s built-in data memory, which you can view as the equivalent to the RAM sticks found in your computer. The processor then accesses the information and deciphers it using a set of instructions stored in the program memory. Depending on what the input requires, the microcontroller subsequently delivers one or multiple types of actions (outputs) using its I/O ports.
Microcontrollers can be found in systems of all shapes and sizes. But while a single microcontroller can handle certain small devices all by itself, it won’t be able to power more complex systems. However, multiple controllers can be programmed to work in conjunction with one another to achieve that result, with each unit having to control only a specific feature or a small component of the larger system.
In many cases, microcontrollers work alongside a central computer. However, they can also be programmed to communicate only with other microcontrollers or to operate individually inside the same system.
What are the Core Elements of a Microcontroller?
As mentioned earlier, a microcontroller only has three core elements, so let’s take a closer look at each one of them:
Central processing unit (CPU)
The processor is the part of a computer that performs operations and executes instructions. It’s essentially the brain of a regular computer and the same concept applies to microcontrollers, too. Like any CPU, the microcontroller’s processor performs basic logic, arithmetic, and I/O operations, but can also perform data-transfer operations. These come in the form of instructions, which are communicated to other components that are part of a larger system.
Memory
The memory stores data received by the CPU via the microcontroller’s I/O ports. The memory comes equipped with a certain set of instructions that are used to respond to the information received from the processor. A microcontroller works with two types of memory, each of which performs a different function.
Data memory stores information while instructions are being executed by the CPU. Meanwhile, program memory stores information related to the instructions themselves. Data memory is temporary and disappears once the microcontroller is no longer connected to a power source. Program memory is non-volatile and remains on the device even after its power source has been removed.
I/O Ports
The input and output ports allow the microcontroller’s CPU to interface with the outside world. I/Os are used by the microcontroller to interact not just with human users, but also other information processing systems. The microcontroller’s CPU gathers data via its input ports and uses output ports to send instructions or signals externally.
In addition to the three core elements detailed above, microcontrollers generally rely on other components that connect to the I/O ports. These components act as peripherals that create a bridge between the processor and various devices. A few examples include the system bus, digital-to-analog converters, analog-to-digital converters, and serial ports.
Why Choose MicroPython Boards Over Other Microcontrollers?
Microcontrollers have existed long before Damien George came up with the idea for MicroPython, so why not go with one of those instead? Well, there are plenty of good reasons, the most obvious of which has to be accessibility. Regular Python is known for being extremely friendly to newcomers, and the same can be said about MicroPython. If you want to learn the basics of hardware programming and start working on your own projects as soon as possible, MicroPython is for you.
Another big advantage of MicroPython is that you can interact with it using the intuitive REPL (read-eval-print loop) environment. The REPL prompt allows you to quickly execute commands, change your code on the fly, and import scripts from the built-in filesystem.
In addition to benefiting from MicroPython’s ease of use and rapid feedback, you would also be able to take advantage of the fact that this software implementation is fully compatible with normal Python. If you already know your way around Python, you won’t have any issues wrapping your head around MicroPython. If you don’t, you’ll be able to quickly pick it up because, thanks to Python’s ever-growing popularity, there are tons of resources online, which isn’t the case with many of the more obscure programming languages.
Final Thoughts
Microcontrollers have seen a massive surge in popularity these last few years and that trend is very likely to continue for the foreseeable future. These amazing little circuit boards can be used to power all manner of devices and are being used more and more as an educational tool in schools worldwide. In other words, now is definitely a great time to learn about microcontrollers and hardware programming in general.
If you’re a beginner who doesn’t want to be overwhelmed by all the tech-heavy jargon floating around these days, we recommend you start by learning as much as you can about MicroPython, because doing so will also give you a lot of insight into microcontrollers. We hope our tutorial managed to shed a bit of light on the topic. There’s always more to learn, though, so don’t hesitate to check out the documentation section on the official MicroPython website for additional information.
This article is part of MicroPython for Embedded Systems in the MicroPython for Embedded Systems
>> Electronic Design Resources
.. >> Library: Article Series
.. .. >> Series: MicroPython for Embedded Systems