Are you sending a balloon with a small payload to near space? Need a small bitof computing power to read your temperature sensors and accelerometer and logthe data to an SD card without using too much power? A microcontroller is justwhat you need. Would you like to build your own small robot or a cuteinteractive toy for your niece? Theres a microcontroller application there,too. Im sure that youve seen a million interesting projects online, wondered,Howd they do that? and gotten the answer: a microcontroller. Without theircapable microcontroller brains, the homegrown 3D printing scene would benowhere. Microcontrollers are at the center of an emerging culture of peoplebuilding the previously impossible.
The goal of this book is to get you building projects with microcontrollers andwriting your own firmware (or using libraries from other people) in C. Ive chosen the Atmel AVR series microcontrollers to focus on because they have a fantastic free and open toolchain, easily available programming hardware, and many of you probably have one or two already on hand in the form of Arduinos. A large part of the collaborative hacker community uses these chips, so its asgood a starting point as any. The ATmega168 chip family that well be using isright now the sweet spot in price-per-functionality, but it is not hard to portyour code to smaller and cheaper if you want to or move over to other AVRchips if you need to.
I picked the C language because its pretty much the standard for programmingmicrocontrollers. Its just at the right point, for my taste, in terms of beingabstract enough to read but low-level enough that turning an individual bit onor off doesnt require subclassing or overriding anything. Your C code willcompile down to something that is nearly as efficient as the best-writtenassembler, but its a heck of a lot easier to maintain. Theres also a ton ofcode examples out there on the Web for you to look at and learn from. (Thatsaid, if you really want a good feel for how the hardware works, teach yourselfAVR assembler when youre done with this book.)
On the other hand, this book is really a book about programming and usingmicrocontrollers in general. Though the particular naming conventions and someof the implementation details are different across different brands ofmicrocontrollers, the basic principles will be the same. More on this in just aminute.
Software Type or Hardware Type?
In a class on programming microcontrollers that I taught at my localhackerspace, I discovered that the students would identify largely aseither hardware types or software types. Some people coded JavaScript forweb applications all day, while others worked in electrical andmachine shops. One guy had never seen a for
loop, and another didnt knowthat the red wire is the positive side of a battery pack. Everyone had something to learn, but it was almost never thesame thing for everyone.
In building your microcontroller projects, youre going to need tothink both like a software type and a hardware type, even if only one of theseinitially comes naturally to you. Attimes youre going to need to debug code algorithms, and at othertimes youre going to need to figure out exactly whats going onelectrically when that button is pushed or that motor is energized.This need to put on two different hats, sometimes even at the same time,characterizes microcontroller and embedded applications.
Throughout this book, therell be some concepts that are too obviousto you, but which may be entirely perplexing to others. Ill be swapping mysoftware-type and hardware-type hats accordingly. In the end, youllbecome familiar enough with both worlds that youll be able tonavigate the middle ground. Youll know youve reached embedded-design nirvanawhen you begin coding with the hardware. Then youll have become a microcontroller type!
Manifesto!
And so we come to my sincerest goal in writing this book instead of simplyanother blinky-LEDs-on-an-Arduino manualto turn you into a truemicrocontroller type. Although the Arduino environment is good for getting peoplehooked on microcontrollers, its a cheap high. Arduino/Wiring goes to greatlengths to abstract away from the microcontrollers hardware. Here, I want toteach you about the hardware because its usefulso getting further awayfrom it wont help. (My friend Ash once described working with the Arduinoenvironment as being like knitting in boxing gloves.)
I dont think that the built-in hardware timer modules are something to beabstracted away from. I believe the timers should be understood thoroughlyenough to be abused to create a small AM radio transmitter that can play theMario theme song within a room using nothing more than a wire or your finger asan antenna (in
More seriously, many of the hardware peripherals inside the AVR arecommon to most microcontrollers, from the prehistoric 8051 or the tiniest PICor ATtiny chips, through the MSP430s and the ATmegas, to the mighty XMega andARM chips. These hardware peripherals have been developed and honed over 40years of microcontroller design development, and theyre not going away any timesoon because they have been designed to be helpful to getting your projectrealized. The microcontroller hardware has been designed by very cleverengineers to solve your problems. My goal in writing this book is to show youhow common problems are solved. You need to learn thehardware, and apply the hardware, to love the hardware.
Although every microcontroller design implements things a littlebit differently, once youve seen it here, it will make sense there. Everymicrocontroller that Ive ever come across is programmable in C. Almost all of what you learn workingthrough this book is transferrable to other chips and other architectures,because what youre learning here is the way things work rather than anabstraction wrapped around the way things work, designed to protect you from theway things work. Some of what you learn (for instance bitwise binary manipulations in )might seem boring, but in the end it will give you simple and direct access tothe common hardware bits that are put there to help you, and the techniques willwork with any brand of microcontroller that you choose to use.
In short, almost none of the time you spend learning about how to createprojects on the AVR in C will be wasted. Yeah, its a bit harder than justreusing someones shields and code. Yeah, you might need to stop sometimes andleaf through a C programming book or an electronics text (or just look it up onthe Net). But when you find out that you need more processing power, or adifferent set of peripherals, you can just buy yourself a $8 chipin place of the $4 one you were using and bring most of your code, and moreimportantly your knowledge, along with you.
This book is meant to be the red pill, and I sincerely hope that you find itworth your time once youve seen how deep the rabbit hole goes.
You Will Need
Before we get too much into detail about the AVR chips and what they can do foryou, let me provide you with a shopping list. Order this stuff now so that youcan be ready to start programming chips in a few days when the delivery truckshows up.