Image of eBits Academy fundamentals illustration

Microcontroller - A Mini Computer?

  • March 08, 2023
  • |
  • Jonas Rothausen

The microcontroller is like a blank canvas for implementing ideas for all kinds of gadgets or programming small, fun games. Microcontrollers also have many practical uses we encounter every day, perhaps without thinking about it. A microcontroller (MCU) is a small computer system on a single integrated circuit, designed to control specific devices or perform specific tasks. It is an embedded system containing a processor, memory, and input/output peripherals. You can think of a microcontroller as a less powerful PC, because they have similar construction and structure. On a PC, however, the CPU and memory are separate parts, while a microcontroller integrates them into a single chip.


Figure 1: A microcontroller and a PC.

Just as we can connect a keyboard, headset, and monitors to a PC to input and output data, we can connect devices to microcontrollers. For example, we can connect buttons and LEDs.


Figure 2

We can also connect different kinds of sensors to measure anything we find interesting, such as humidity, temperature, or motion. Below is a list of some of these sensors, which we could use with a microcontroller.


All those LEDs, sensors and buttons are cool, but how do we actually use them with a microcontroller? We cannot simply expect the microcontroller to do what we ask. We need to instruct it to do what we want, in the right way! Let us find out how.


What Is a Microcontroller?


What is a microcontroller, and how is it made? To understand this, we need basic knowledge of some components found in a microcontroller, such as resistors, transistors, and diodes; see figure 3.


Figure 3: Symbols for a resistor, transistor, and diode.

If you are not sure what all these components are, we introduce them in our article on analogue circuits, right here - take a look if needed! Why are these components so important to a microcontroller? We can use them to build logic gates, which we learned about in this blog on digital electronics. Logic gates are important building blocks. They process digital logic levels; measuring a continuous analogue voltage requires an ADC, not just a logic gate. Let us illustrate this with one of the gates from the digital circuits article: the AND gate. Figure 4 below shows an AND-gate circuit built from the three components in figure 3.


Figure 4: An AND-gate circuit.
Voltage sources: Vin, A and B
Transistors: T1 and T2
Resistors: RA, RB and Rout

In the article about analogue circuits we learned that connecting 5 V to the collector of transistor T1 (Vin) in figure 4, with no voltage from A or B, gives no voltage at Vout. Changing A to 5 V still gives no voltage at Vout. The same applies to B. Only when both A and B receive 5 volts do we get 5 volts at Vout, because both transistors then conduct. This AND-gate circuit concept is demonstrated below.


Figure 5: Notice that we only get 5 V at Vout when both A and B have 5 V.

Replacing 5 V and 0 V with 1s and 0s gives what are known as truth tables. Remember that digital electronics is concerned with the logic levels high and low - 1 and 0 - ON and OFF.
Figure 6 is the truth table for the AND gate. This is the beginning of digital electronics and how microcontrollers understand things.


Figure 6: Truth table for an AND gate.

Digital electronics uses just two logic levels, 1 and 0, as shown in the truth table above. A truth table can be described by an equation. All logic gates have truth tables and corresponding equations. The mathematics used in digital electronics differs from ordinary mathematics and is called Boolean algebra. This is covered in the article on digital circuits.

Using these logic gates, we can build structures that add, multiply, or store digital information - in other words, work with it. These structures, such as ADDER, MULTIPLIER, and RAM, are found inside microcontrollers.

To communicate with, control, and use ADDER, MULTIPLIER, and RAM, we use a program. The program is stored in the microcontroller’s memory and can be transferred through programming pins.

 


Figure 7: ROM - Read-Only Memory

As illustrated in figure 7, the program used to communicate with ADDER, MULTIPLIER, and RAM can be viewed as lines of 0s and 1s. These lines might look like this:

 


Figure 8: Excerpt from figure 7.

Each line carries information to the microcontroller’s memory, so the microcontroller knows what is going on and can understand our instructions. Few people can readily make sense of these lines of 0s and 1s. Decoding and understanding each line would be difficult and time-consuming, so we have what is called an assembly language. Assembly language is for skilled programmers with a thorough understanding of the specific microcontroller’s design. Such people create easier-to-use programming languages so the rest of us can program more intuitively, connecting electronics with people.

In summary, instructing a microcontroller involves translating a programming language into 0s and 1s. Examples include C and C++, which are often used with microcontrollers.

These developers help us program microcontrollers more quickly and easily, because assembly code generally requires many more lines than C or C++ to achieve the same result.

To make things even easier, we can use software libraries when programming microcontrollers. A library is like a set of commands you can import to expand the tools available for programming. It lets the microcontroller understand certain shortcuts that are easier for us to use. For example, importing a maths library lets you use mathematical concepts conveniently in your code. There are many different libraries ready for your next project! There are also many well-made microcontroller tutorials online, such as those found here.

Only your imagination limits what a microcontroller can do! Their compact size, low power consumption, and low cost make them popular for many applications.

Simplified Logic Model: The 0V and 5V in the AND example are idealised logic levels. A real BJT circuit has voltage drops and load-dependent levels; measure the output and check the next input’s limits. Do not connect 5V to a microcontroller that is not 5V-tolerant.

Leave a comment

Please note, comments need to be approved before they are published.