Microcontroller is an integrated circuit chip that uses ultra-large scale integration technology to integrate a central processing unit (CPU) with data processing capabilities, random access memory (RAM), read-only memory (ROM), various I/O ports, interrupt systems, timers/counters, and other functions (which may also include display driver circuits, pulse width modulation circuits, analog multiplexers, A/D converters, etc.) onto a single silicon chip, forming a small and complete microcomputer system. Compared to computers, microcontrollers only lack I/O devices (input/output devices); when paired with I/O devices, they can form a complete system with certain functions.
Input devices can be as simple as a single button, and output devices can be as simple as a single LED indicator. They are small in size, lightweight, and inexpensive, with prices as low as a few cents per piece. Modern PCs are generally 64-bit, while common microcontrollers are currently 8-bit, 16-bit, or 32-bit. Note: The 8-bit, 16-bit, and 32-bit refer not to the number of pins on the chip, but to the number of bits in the internal bus width.
Common microcontrollers include:
(1) 51 series, which is commonly seen in university teaching. There are many microcontrollers based on the 51 core in the market, such as Atmel’s AT89, Silicon Labs’ C8051F, Hongjing’s STC, and the former Philips’ NXP 80C51. There are also many in Taiwan, such as Yilong, Huabang, Hetai, Songhan, Lingyang, Zhongying, etc.
(2) AVR series, divided into tiny AVR, mega AVR, and Xmega AVR. Tiny AVR is mainly used for applications with lower performance requirements and efficiency in small packages. Mega AVR is primarily for designs requiring additional peripheral circuits. Xmega AVR is mainly used in high integration and low power consumption applications. The Arduino that will be introduced below uses AVR microcontrollers.
(3) STM32 series, introduced by ST Company, is a 32-bit microcontroller based on the ARM Cortex-M core, with strong performance.
(4) MSP430 series, introduced by Texas Instruments, is a 16-bit ultra-low power chip characterized by fast computation speed, strong processing capability, and low power consumption.
(5) PIC series, introduced by Microchip Technology (Microchip Semiconductor), includes 8-bit, 16-bit, and 32-bit microcontrollers, featuring a powerful architecture, flexible memory technology, and communication methods covering SPI, I2C, UART, CAN, USB, Ethernet, etc., with internal integration of graphics and touch sensor controllers, making it suitable for developing and controlling peripheral devices.
(6) M68HC series, introduced by MOTOROLA, is one of the largest microcontroller manufacturers, characterized by fast speed, powerful functions, and low power consumption, equipped with flash memory and low clock frequency for strong anti-interference capabilities, making it very suitable for industrial control applications.
Microcontroller programming generally uses assembly language and high-level languages. Assembly language is like half machine language. What is machine language? It is a program that contains only “0” and “1” symbols, which can be directly recognized and executed by the CPU. Programs written in non-machine language editors must first be translated (compiled) into .bin files before the CPU can recognize and run them.
The familiar .exe files mainly contain various instructions that the CPU can directly execute, known as executable files. Since the instruction sets of CPUs with different architectures are different, an executable file cannot be used interchangeably across different architectures.
PCs typically use CPUs based on the X86 architecture, such as Intel’s I3, I5, I7, and AMD’s Ryzen:
Mobile phones typically use CPUs based on the ARM architecture, such as Qualcomm Snapdragon 855, Huawei Kirin, and Apple A14:
Programs written in “0” and “1” can be recognized by the CPU, but they are too difficult for humans. Few people can write programs using only “0” and “1”. Although I heard from my mentor that his roommate during his PhD could do this, he probably only wrote some simple programs. To solve this problem, someone invented assembly language, which uses certain string names to replace specific sequences of “0” and “1”. For example, the instruction to transfer a number is actually “0001”, but in assembly language, it is replaced with “MOV”, making it easier for humans to remember the instruction name. The following is a piece of assembly code that only uses two instructions, MOV and ADD:
A CPU architecture usually supports hundreds of instruction sets, and remembering the common twenty or thirty instructions is basically enough for assembly programming. The instruction set is extensive, with some instructions being very common while others are rarely used, and the length can vary, making it very complex. This is called Complex Instruction Set Computing (CISC). Thus, someone proposed Reduced Instruction Set Computing (RISC), which uses a smaller number of frequently used instructions with fixed lengths. Compared to the former, the latter supports various addressing modes, can continuously add instructions to achieve various complex functions, thus making the hardware increasingly complex and costly; while the latter executes faster, performs better in parallel processing, and has simpler manufacturing processes and lower costs.
X86 architecture uses a complex instruction set, while ARM architecture uses a reduced instruction set. In recent years, Apple’s Mac computers have begun to abandon Intel CPUs based on X86 architecture and switch to their self-developed A-series CPUs, which are similar to those used in iPhones but with stronger performance. The advantage is that their computers and phones can use the same apps. As we know, the app resources under iOS are extremely rich. The downside is that many of Apple’s apps are paid. So far, I have only bought two apps (not that I don’t support genuine products, but simply because I’m poor), one was a theft prevention app that cost around 12 yuan, as I had my MacBook and an iPad stolen one after another within a month, resulting in direct economic losses of up to 20,000 yuan, and the indirect losses are incalculable because the pain of loss is something I don’t know how long it will last. The stolen iPad had the positioning feature turned on, and its location was detected in a computer city, presumably preparing to be cracked. However, there are many stores in the computer city, and I couldn’t figure out which one it was. I pretended to find a lost iPad and asked how long it would take to crack it. The owner replied that it would take a few days to order parts. But a few hours after I got home, I received a notification on my phone that my iPad had already been cracked and was logged into my Apple ID. My iPad and iPhone share the same account, so I hurriedly changed my phone account password. If they change the Apple ID password again, my iPhone would also become a brick. After buying this theft prevention app, I found that it had bugs and called Apple customer service to return it. The other app was also bought for a few dozen yuan, which was a time signal app. The only time signal tower in the country is in Shangqiu, Henan, and the signal reception in Guilin is not very good; sometimes I can receive it, and sometimes I can’t. At first, I only knew that connecting an infrared transmitter to the phone’s headphone jack could control infrared appliances. I didn’t know that there was also an app that sends time signals through the phone’s amplifier and speaker because the speaker emits sound while the time signal is a radio signal. The signal frequency is completely different; the audio frequency range is 20-20kHz, while the time signal frequency is 68.5kHz. After research, I found that this app modulates the time information at 13.7kHz, and when the signal is transmitted through the amplifier circuit, it radiates radio waves. Since high-frequency signals emit harmonics along with the fundamental frequency, the fifth harmonic 13.7×5=68.5 exactly matches the time signal frequency! Since the energy of higher harmonics is smaller, when using this app for time synchronization, the phone must be at maximum volume, and the watch must be placed close to the phone speaker. To solve this problem, I connected the phone to a Bluetooth speaker to amplify the signal, but within a few seconds, the Bluetooth speaker emitted a burnt smell, resulting in a direct loss of 2,000 yuan… I won’t say more; the pain from years ago that I had almost forgotten is coming back to increase my indirect losses…
Returning to the main topic, even with a reduced instruction set, some still find it hard to remember, so high-level languages emerged, which are programming languages that are closer to natural language. Engineering and science majors generally learn C language. For example, the “if…then…else…” in C language is much easier to understand. A specific example is:
if (x > 0) then
i=i+1;
else
j=j+1;
This few lines of code implement the function that if x is positive, i counts up by one; if x is negative, j counts up by one. (Here, the case of 0 is not considered)
However, some people say, what if my English is not good and I can’t recognize a few words? Don’t worry; there are only a few words in C language. Just memorize them as symbols; you don’t need to learn English well.
In fact, some have developed Chinese programming languages, which I think are unnecessary and rather inconvenient. For example, in the previous example of “if”, should it be “如果”, “倘若”, or “假如”? It’s easy to get confused and make mistakes, so it is not popular. For instance, the following is a Chinese programming language called Yiyu Language, which makes my head hurt:
High-level languages can also be divided into procedural and object-oriented. Standard C language is a procedural programming language, while C++ and C# are object-oriented programming languages. What is object-oriented programming? In simple terms, it allows the definition of a data structure called a class, which can inherit and derive. There are various libraries of classes written by others online, and you only need to call these classes very simply to greatly reduce your programming workload. Without this, if everything had to be written from scratch, programmers would be in real trouble.
Besides C, C++, and C#, there are many other high-level languages, such as Java, VB, Pascal, Delphi, Fortran, VFP, PowerBuilder, PHP, ASP, Python, etc. Actually, I am not good at object-oriented programming; I used C++ for a while a few years ago.
In recent years, Python programming has become popular in primary and secondary schools, so I also took a look at Python. Bare-metal programming for microcontrollers is still mainly based on C language, while object-oriented programming is more common in computer programming. Therefore, many hardware developers are not very proficient in object-oriented programming; they may be better at assembly programming because they need to understand the underlying hardware, such as developing low-level drivers. In contrast, most software developers have little knowledge of hardware development and programming.
Additionally, I have found that whether one can program is a dividing line among electronic enthusiasts. There is a group of electronic enthusiasts born earlier who did not have the opportunity to learn computers back then. Later, when computers became more common, they no longer had the energy to learn. However, they have mastered analog electronic technology very solidly. The most impressive example is an old member of a forum, who should be from the 50s or 60s. He has published hundreds of original circuit diagrams on the forum, and some of the designs are very clever. If he is not very proficient in analog circuits, I think he could never reach this level. Other users even helped him collect and organize a collection of his original circuit diagrams. Another group is the opposite—mostly younger people who grew up with computers. The most impressive example is a person on the same forum who is referred to as a “child prodigy”. I found that he was very skilled in microcontroller programming but knew nothing about analog circuits. However, there are exceptions. I once collaborated with a friend who was still in high school and was called a “child prodigy” by online users. I was responsible for the circuit design and testing, while he was in charge of program development. I found that he was not only a programming expert but also very familiar with analog circuits and component selection, indicating that he had actually developed many products.
As a person born in the 70s, I am probably the dividing line between these two groups. I started to come into contact with computers during my undergraduate studies, but back then I only learned DOS and BASIC, and I hadn’t even used a mouse during my four years of university. Additionally, I studied physics education rather than electronics, so I didn’t understand electronics or computer programming much at that time. I attended high school in a rural town where the teachers didn’t manage us much, and we spent our days playing cards. During the three days of the college entrance examination, we even played cards until midnight. I passed three years in a daze, and when it came time to fill in my college application, no one guided me, so I thought that a junior college was better than a university. At that time, I didn’t know that there were specialized electronics majors to choose from; I was interested in electronics and had some exposure to it in physics classes, but I only learned about resistors and potentiometers. I went to town looking for someone to fix appliances but didn’t learn much. I felt that physics was the closest to electronics, so I chose physics as my major. When it came time to apply for college, I also estimated that my scores wouldn’t be high (unlike now, where you know your scores before applying; back then, we had to apply without knowing our scores). I thought that the first choice could drop 15 points for admission, so I ended up with physics + education = physics education, and that led to who I am now. However, during my graduate studies, I chose a circuit and system major, but later found that I was learning mostly theoretical stuff. I thought that what really suited me might be applied electronics or mechatronics.
Learning microcontroller programming requires first learning C language. I have only come into contact with the 51 microcontroller and AVR microcontroller (actually, once you are familiar with one type of microcontroller, transitioning to another is relatively easy; it’s just that the internal resources and register configurations are different, but otherwise, they are quite similar). I also had some exposure to ARM when it first became popular. Whether it’s C language or microcontroller programming, I learned these on my own, and I haven’t really used them in my work. It’s been over a decade, and I have probably forgotten most of it. Perhaps due to a lack of confidence, I have been delaying writing about microcontroller programming, which was initially planned to start with commonly used electronic components and end with microcontroller programming.
Recently, I have served as a judge in two primary and secondary school maker competitions and found that the way students learn programming is through graphical programming, which allows even those with no programming background to quickly get started. I have taught third-grade elementary school students, which has dispelled my concerns that friends without a C language foundation would find it difficult to understand the content I write about microcontroller programming, so I decided to start introducing microcontroller programming from graphical programming.
Graphical programming for microcontrollers requires an open-source hardware platform and a graphical programming software platform. First, let’s talk about what open-source hardware is:
Open-source software is widely understood; it means that the source code is open, and anyone can modify and distribute it under the terms of the open-source license. Open-source hardware means disclosing circuit diagrams, materials lists, PCB component layouts, firmware, and other materials, especially the firmware inside the chip, which acts like a bootloader, allowing users to upload their user programs to the chip easily without having to write a lot of basic code. If you don’t understand what a bootloader is, you can compare it to the BIOS of a computer; it is a program stored in the ROM chip of the computer motherboard responsible for basic input/output, hardware power-on detection, operating system boot loading, etc. If the BIOS program is damaged (for example, by the CIH virus), then the computer will not function beyond lighting the power indicator.
Common open-source hardware platforms include:
Arduino, micro:bit, controlling board (esp32), Raspberry Pi, and others.
(1) Arduino uses Atmega32u4 and Atmega328p eight-bit CPUs; it appeared earlier, has high freedom, and is suitable for high school and above users.
(2) The controlling board 2.0 is a hardware widely used in primary and secondary schools, developed with the participation of the Maker Education Expert Committee, based on the Espressif ESP32 WiFi chip, with rich onboard resources. The ESP32 integrates WiFi and Bluetooth communication modules, suitable for IoT product development.
(3) Micro:bit was developed by the BBC in 2015 and uses the ARM architecture nRF51822 microcontroller, making it more entertaining and suitable for elementary to junior high school students, with rich onboard resources. It supports local and online IDE editing.
(4) Raspberry Pi
Secondly, there are graphical programming platforms that provide a graphical program editing environment (IDE). Common graphical programming platforms include mixly, scratch, mind+, mblock, programming cat, kittenblock, makecode, mPython, etc.
(1) Mixly was developed based on Blockly by a team led by Professor Fu Qian from the Faculty of Education at Beijing Normal University. I have met with Professor Fu several times and was honored to be invited to his office for a visit, where he gifted me some of the boards he developed. Many of his views on primary and secondary school maker education left a deep impression on me. Mixly is characterized by a good user experience, supports many hardware types, and frequently updates, as evidenced by his statement that he would still be writing programs at midnight when I visited his office at 10 PM.
(2) Scratch was developed by the Massachusetts Institute of Technology and is very popular in primary and secondary schools in China. My son learned Scratch when he was in elementary school, and the following “Guilin Tourism” is a Scratch project he created that won first place in the Guangxi Youth Creative Programming and Intelligent Design Competition: https://kada.163.com/project/2457407-2840414.htm
The following is the Scratch editing interface, but recently it seems that there was a sensitive incident in the Scratch community, and a notice was issued that it could not be used.
(3) Mind+ is developed by DFRobot’s Mushroom Cloud department based on Scratch. I heard that it is used quite frequently in primary and secondary school competitions recently. One reason could be that DFRobot has developed many related hardware products for schools to choose from. Another reason is that it is fully compatible with the Scratch sb3 file format. Additionally, it supports a wide range of hardware, including mainstream Arduino, micro:bit, controlling boards, etc., and also supports user-defined extension libraries, allowing for graphical programming as well as advanced language programming in Python, C, and C++.
(4) Kittenblock does not support the controlling board.
(5) mPython is developed by the Shengsi Technology team based on the official version of BBC PythonEditor, featuring hex, Python, and Blockly programming functionalities. It does not have a real-time mode and cannot show real-time effects through animation stages.
I used Programming Cat for a while, but it supports relatively few hardware types and may be more suitable for younger children to play with animation stage programming.
Makecode supports micro:bit but has limited support for Arduino.
In summary, I personally recommend Mixly and Mind+, as both have good functionality and professionalism, and support a wide range of hardware, with some differences in style and characteristics depending on personal preference.
Today I have introduced these basic concepts. Next time, I will start introducing specific graphical programming. If interested friends can buy a set of Arduino hardware online, with prices ranging from dozens to hundreds of yuan, the more sensors and actuators included, the more expensive it is. Alternatively, you can buy a controlling board, which costs 99 yuan alone, but you need to pair it with an expansion board for external sensors and actuators, and you may need to buy additional sensors and actuators; otherwise, the number of experimental projects will be limited. Overall, I have three suggestions: First, it’s best to buy fool-proof connectors to avoid burning components by plugging them in incorrectly, which is important for younger children. Second, it’s best to have WiFi and Bluetooth capabilities, enabling you to develop IoT features. Third, the graphical programming platform must support this hardware, meaning that the programming platform has a support library for this hardware, whether official or third-party. It seems that Arduino does not have wireless capabilities and requires additional wireless communication modules. The controlling board needs an additional expansion board, and the sensor and actuator kits that match the controlling board seem to be relatively expensive, as their main sales targets are schools. You don’t necessarily have to buy their matching sensors and actuators as long as the connectors are the same; you can find a set of sensors and actuators with the same connectors that are more cost-effective. There are also other hardware developed with ESP32 that come with wireless communication capabilities and built-in IO expansion sockets, such as the one shown below, which costs around 150 yuan for the control board.