Skip to main content

Section 3.1 Introducing microcontrollers and MicroPython

The first chapter introduced the fundamentals of electrical circuits; the second introduced the Python programming language. In this chapter we'll bring the two together using microcontrollers: tiny computers that you can program to interact with circuitry. And when you combine a little bit of code with a little bit of circuitry, some pretty amazing things are possible.

Subsection 3.1.1 What is a microcontroller?

A microprocessor is a computer chip containing a processor which can execute code. A microcontroller is similar to a microprocessor, except that it additionally contains special circuitry designed to sense and control the outside world.

Microcontrollers are used to build embedded systems, which is a fancy term for what you get when you put a computer into something that isn't a computer. Pacemakers, automobiles, thermostats, and dishwashers are all examples of embedded sytems: they all contain microcontrollers running specialized software to perform particular tasks. Thanks to modern manufacturing technology, microcontrollers can be produced very inexpensively. Most cost only a few dollars, and many are even less than a dollar when purchased in bulk.

Physically, a microcontroller generally consists of a tiny bit of silicon on which the circuit has been built, embedded in a package of black plastic. Below is a picture of a microcontroller where the package has been carefully etched away to expose the silicon:

Figure 3.1.1. An 8-pin PIC12F microcontroller with the plastic package etched away. Image from Silicon Exposed blog 1  (CC-BY).

The silicon has tiny wires leading out to metal contacts on the edge of the package known as pins. At least one pin is designated for power (typically 5 or 3.3V) and one for ground; together these supply power to the chip. The remaining pins are used as inputs and outputs to measure and control voltages.

Once upon a time the microcontroller pins were actually pointy and were placed into special sockets or through holes in a circuit board. Today, most microcontrollers are surface-mount, meaning that the pins sit flat on the circuit board and may not stick out beyond the edge of the package at all. But the terminology remains.

The microcontroller is usually soldered onto a printed circuit board (PCB), which provides the additional circuitry necessary to use it. This often includes a USB connector, a voltage regulator, and reset and programming buttons. Off-the-shelf circuit boards with microcontrollers are often called development boards, because they are often used to prototype and develop a new product before a custom circuit board has been manufactured.

Subsection 3.1.2 Introducing MicroPython

MicroPython 2  is a minimal version of the Python language designed for microcontrollers. It supports nearly all of the standard Python syntax, but is designed to run with very little memory. MicroPython also includes a set of Python objects and functions for controlling the various features of the microcontroller, which we'll explore in the rest of this chapter.

Microcontrollers are traditionally programmed in C or C++, and a well-optimized program written in C can run ten or a hundred times faster than an equivalent Python program. But since the microcontrollers we'll use in this course run at over 100 MHz (100 million operations per second), even rather inefficient code runs fast enough to work well.

The primary restriction with MicroPython is that you can't use large amounts of memory. If you're just writing code, this generally won't be a big deal, but if you start trying to load or manipulate images or other large datasets, you may have a problem.

siliconexposed.blogspot.com/2011/03/microchip-pic12f683-teardown.html
micropython.org/