Exploring Microcontrollers

Overview

Microcontrollers are basically a miniature personal computer on a single integrated circuit (IC), which are sometimes referred to as a "chip" or "microchip". They generally include a processor, memory,  and input/output peripherals. Microcontrollers are used for all sorts of applications. They can be found in vehicles, robots, office machines, medical devices, satellites, vending machines, and home appliances, among other devices.

Microcontrollers are frequently found as 8-bit or 32-bit chips. This refers to the amount of bits of data a central processing unit (CPU) can process or move in and out of memory at a time. In recent years, microcontroller development boards have become very popular among artists, designers, engineers, students, and hobbyists. These boards include a microcontroller as well as various analog and digital input and output pins. Some well known boards include Arduino, micro:bit, Raspberry Pi, and Adafruit Circuit Playground Express. Common programming languages for popular development boards include C++, Python, and JavaScript. There are also block-coding options these days.

How computers work

Binary, Bits, and Bytes

To get started, we should really understand how a computer works, considering a microcontroller is just a simple computer. Modern computers are digital due to the way the electrical signals go out. Below, you see the difference between an analog signal and a digital signal. It is necessary to have a very consistent, precise current, making digital signals the way to go. A digital signal is only allowed to take on a limited number of possible values. Computers use binary, a base two number system, meaning the digital signal is either on or off. Other examples of digital systems include our numbering system and our alphabet. The "digits" that make up our numbering system consist of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. We call our numbering system a base 10 system because it is consists of ten different options. We can make large and small numbers using these digits, but the numbers we make can only use these ten values. When we look at our alphabet, there are 26 possible values, spanning from A through z.  

The binary signals in our computers and other electronic devices come from silicon chips. They act like tiny switches (think of a light switch). They are easy to make, inexpensive, small, reliable, and millions of them can fit into a small area. While it may be hard to imagine how numbers, music, images, and text can all be represented by a bunch of "on" and "off" signals, this is how all of our digital devices work!

These on/off values are represented by what is known as a bit, which is short for BInary digiT. All of the processing done by a computer is done with bits. Any information that is stored or manipulated is stored as a sequence of these on/off values. Bit patterns are depicted as either a 1 (on) or a 0 (off). 

One bit is so small that computer memory generally organizes them into groups of eight bits. Each eight-bit group is called a byte. It can store 2^8 or 256 different values, making it appropriate for storing things like single characters or text. With 256 possible combinations, it is possible to assign one to each letter, digit, punctuation mark, etc. Sometimes more than eight bits are required for some data. When this is the case, information is stored using multiple bytes. The larger a file is, the more bytes it requires to tell the computer everything it needs to communicate to work. Take a look at the chart below. You will recognize some of the quantities. As computing advances, the amount of bytes needed for a program or file to operate continues to get larger and larger. It will not be long before you are out looking at a new smartphone that has a capacity in terabytes instead of gigabytes.

Computer Languages

There are many different programming languages out there today. All languages have syntax and semantics, computer or human. When learning any language, syntax relates to the structure or grammar of the language. That is the rules that define whether or not a sentence has been constructed properly. Semantics refers to the meaning assigned to the symbols, characters, and words. You have spent years learning about syntax and semantics in English classes. Here, we will take a brief look at how these relate to programming languages, mainly focusing on block-coding, with a touch of text-based languages thrown in. 

Text-Based Programming Languages

Just so you can see a few different programming languages and their syntax, there is an image below showing the "Hello, World" program in C++, C#, Java, and Python. The "Hello, World" program is commonly used for the first program a person makes. All four have the same exact output or result when run, but you can see that each has its own syntax and semantics.

One thing you may have noticed is the code above is not in binary (1s and 0s). While these are popular programming languages and give the computer the information needed to complete tasks, the computer cannot actually read these languages. A compiler is needed to translate the source code or the code written by the programmer into machine code, made up of the 1s and 0s the computer can read. This is as deep as we will go with this. If you are interested in learning more, there is tons of information available on these subjects, or consider joining our coding club here at school.

Block-Based Code

When you code with blocks, in the background, the blocks are putting together text-based code, but you do not see it. The example below shows the exact same code - block-code on the left and Python on the right. Block-code is perfect for beginners. The shape of the blocks eliminates most syntax errors. For the most part, blocks that should not be together will not fit together. 

In some cases, you still may get an error. See the example below. It is worth noting that even though blocks may fit together, it does not guarantee that your semantics are correct - meaning you can put a lot of blocks together, but it does not mean the program will work the way you intended. Errors are caused by things that cannot be read by the computer. If blocks are not ordered correctly or if you have the wrong type of block, the program may function incorrectly. 

These examples of block-code are all from Microsoft MakeCode. Many programs have block-coding capabilities, as it is a great way to learn programming concepts and logic. A few others include Tinkercad Circuits, Scratch, and Google Blockly. Block-code is intended for beginners, not professional use. In the next section, you will be following a tutorial to program a micro:bit microcontroller using Microsoft MakeCode. Then, you will complete a variety of challenges using the skills that you learned. Happy programming!  

Tutorial: Coding a micro:bit

The tutorial below will walk you through making a virtual dice circuit. You will be using block code for this tutorial. micro:bit microcontrollers can also be coded in Python and JavaScript. Follow the directions in the tutorial precisely. You will learn how to add code to make the micro:bit perform basic functions. In this case, how to put code together - certain blocks fit together and others do not. This is how syntax is handled in block coding.

Exploring Microcontrollers: micro:bit


Challenge 1: Streamline Digital Dice Code

For this challenge, you will be duplicating your code from the Exploring Microcontrollers tutorial (the digital dice) in Microsoft MakeCode and find a way to make the code more efficient. Your adjusted version of the program must function as defined below. Instructions on how to streamline the program will not be provided. It will be up to you to figure out how to make the micro:bit work as defined in the requirements.

Requirements:

Challenge 2: Thermometer

For this challenge, you will be creating a new project in Microsoft MakeCode and using the skills you learned through completing the Exploring Microcontrollers tutorial. You will need to make a program that will function as defined below. Instructions on how to make the program will not be provided. It will be up to you to figure out how to make the micro:bit work as defined in the requirements.

Requirements:

The conversion formula is: ( °C × 9/5) + 32 = °F

*  Do not forget about the order of operations!  (PEMDAS)

** Practice working with the order of operations: Get 24 Game

*** Click here for an example of how to use the order of operations using blocks

Test your program with the following values:

Your design should work like this: 

Challenge 3: Magic 8 Ball

For this challenge, you will be creating another new project in Microsoft MakeCode and using the skills you learned through completing the previous assignments. You will need to make a program that will function as defined below. Instructions on how to make the program will not be provided. It will be up to you to figure out how to make the micro:bit work as defined in the requirements.

Requirements:


Possible statements:

Your design should work like this: