Welcome to the wonderful world of microcontrollers! From robots to smart TVs to sensor modules in your car, a ridiculous amount of things have embedded controllers inside. With a knowledge of microcontroller programming, you can create quadcopters, IoT devices, sensor networks, and all kinds of other cool projects.
As you go through the articles in this section, I’ll walk you through the basics of microcontroller programming, then on to driving motors and reading from sensors, communicating over USB, and everything else you’ll need to know to develop firmware for real devices.
In this article, I’ll tell you exactly what microcontrollers are, why you’d want to use them, and most importantly, how to get set up and begin programming them. Let’s get started.
What’s in a microcontroller?
A microcontroller, or MCU (Microcontroller Unit) is a tiny computer in a chip. It contains a processor, memory, peripherals (which we’ll define later) — just like any computer, only scaled down. You can program them, typically in C or assembly, and they store the program in internal non-volatile memory. There are lots of different kinds of microcontrollers: small, basic ones, more complex ones, special-purpose ones such as low-power devices and digital signal processors, and many more.
Micocontrollers are great because they’re so easy to use. Since everything you need is integrated into one IC (unlike a microprocessor, with which you’d need separate RAM and ROM chips), designing a system around a microcontroller is fairly straightforward. As we’ll soon see, all that’s needed to build your own MCU development platform is a microcontroller, power supply circuit, and a few other components. This simplicity makes microcontrollers (and designing microcontroller-based devices) very cheap.
Of course, the simplicity of a MCU isn’t free: microcontrollers also aren’t as powerful as more complex chips like microprocessors. If you’re hoping to run a full-featured operating system or bitcoin miner on your MCU, forget it. That being said, modern MCUs are powerful enough to be useful for a variety of tasks.
What is “firmware”?
Unlike software, which can easily be changed, updated, or otherwise modified after being written to a device, firmware is code that’s intended to be changed infrequently (or never) after being programmed onto a device. In the context of microcontrollers, “firmware” and “microcontroller code” can be used interchangeably.
What do I need to get started?
First, let’s talk about what you need to know. A basic foundation in C programming is going to be very helpful once we start writing firmware for our microcontroller. If you don’t know any C, I’d recommend you look around for a tutorial to learn the basics, or get a pdf or copy of K&R if you want to do a more in-depth study of the C programming language. Knowing some basic circuit theory will also help, but if you don’t know anything about circuits, you’ll be okay.
To program a microcontroller, you connect a special device called a programmer to specific pins of the MCU (its programming interface). The programmer also connects to a computer, which sends the compiled code to the programmer to push onto the MCU. So to start playing with microcontrollers, you’ll need three things: a microcontroller (with its programming interface accessible), a programmer for that MCU family, and a computer.
There are several ways to get a micro with an accessible programming interface:
- You can wire up a circuit on a breadboard. This is the cheapest option, as well as the best way to learn if you’re comfortable with breadboarding.
- If you’re afraid of getting your hands dirty, you can buy a MCU development board, or “dev board”. There are lots of good ones, try and find one featuring a PIC16 or PIC18 model.
- If you’ve done some PCB design, head over to the Simple Dev Board tutorial. This is a more advanced option — if you’ve never made a board before, you might want to try something simpler first.
For the programmer, you’ll want the PICkit 3, one of the programmers made by Microchip, the company that makes PICs. To write and push code, I’d suggest you download the MPLAB X IDE. You’ll also need a PIC compiler for the part you’re using, so grab the XC8 compiler. Both are free downloads.
Once you’ve set everything up, it’s time to write some firmware!