All About Microcontrollers

See also Dan O’Sullivan’s notes on microcontrollers. My notes draw heavily from Dan’s.

A microcontroller is a small, inexpensive computer, usually used for sensing input from the real world and controlling devices based on that input. Most electronic devices you use today have a microcontroller in them of some form or another. Microcontrollers are easy to use with simple sensors and output devices, and they can communicate with desktop computers fairly simply as well. When you’re building some form of customized sensor or output device, using a microcontroller is an excellent way to separate the customized part of your project from the part that’s best done on a desktop computer. They’re also very useful for when you’re designing a simple interactive device that doesn’t need the full power of a desktop computer, but does need to be smaller or cheaper.

Like any other computer, a microcontroller has to have input ports to detect action by a user, and output ports through which it expresses the results of its programs. The pins sticking out of the microcontrollers are the inputs and outputs. Other devices, like light, heat, or motion sensors, motors, lights, our sound devices, are attached to these pins to allow the microcontroller to be sensitive to the world and to express itself.

There are several different levels of microcontrollers and microcontroller systems. Some are very small, chip-size devices to which you have to connect your own electronics. Others are larger, composed of several components and ports for input output, ready to plug right into other devices.

Higher level microcontrollers will have a simple hardware interface to other devices (usually a plug or a couple of wires), and a simpler programming language, if at all. They will also usually be the most expensive of microcontrollers, because someone else has done the work for you.  Higher level controllers must be attached to a personal computer via serial or USB to operate.  Lower level microcontrollers will require more work, both in terms of hardware connections (you will have to build your own circuits to interface them to other devices), and in terms of programming (you will need to use a lower level programming language like C or assembler). However, lower level processors are generally cheapest and most flexible in terms of what you can make them do.

Some typical examples of microcontrollers at various levels:

High Level: Gainer

a gainer controller

a gainer controller

What is it: a microcontroller interface module that allows you to build your own sensor and actuator interface circuits and control them from a regular multimedia programming environment.

Programming language: Actionscript, Max/MSP, Processing. The modules themselves are pre-programmed, and speak an open-source serial protocol. They can be addressed via a series of application programming interfaces (APIs) for the different programming languages.

Hardware interface: USB

Cost: Starter kits range from $130 for a basic interface kit to $530 for the works

High Level: Phidgets

a Phidget controller

a Phidget controller

What is it: a series of interface modules that allow the user to connect sensors, motors, lights, MIDI devices, and more with little or no electronics work.

Programming language: C/C++, Java, Actionscript, etc. The modules themselves are pre-programmed, and speak a proprietary USB serial protocol. They can be addressed via a series of application programming interfaces (APIs) for different programming languages.

Hardware interface: USB

Cost: Starter kits range from $130 for a basic interface kit to $530 for the works

Mid-level microcontrollers are usually programmed by connecting a serial cable from a PC on which the code is written to the microcontroller itself. Beyond that, no additional hardware is needed.

A variety of mid-level modules and low-level microcontrollers.

Middle Level: Wiring and Arduino

Arduino module

Arduino module

What is it: electronic module with microcontroller, power supply, USB-to-serial interface, input/output interface pins

Programming Language: Wiring, a simpler variation on C that’s very similar to Processing.

Hardware Interface: simple digital and analog circuits interface to I/O pins.

Cost: Wiring: $80.00. Arduino: $30.00. Both can be obtained from Sparkfun in the US.

Like other mid-level microcontrollers, Wiring and Arduino connect directly to the programming computer. Unlike the others, though, they have a built-in USB-to-serial interface, so they connect right to your USB port. Beyond that, no additional hardware is needed.

Both the software and hardware for Wiring and Arduno are open-source, with plans available online. So if you like working at a low level, you can use these as a bridge to get there. They’re both based on the Atmel family of microcontrollers.

There are several variations on Arduino, some produced by the original team, and some produced by other makers. The varying designs reflect a variety of different possible applications, or the maker’s personal tastes. A list of a few Arduino variations can be found on the Arduino site.

Middle Level: NetMedia’s BX-24 Microcontroller module

BX-24 module

BX-24 module

What is it: electronic module with microcontroller, power supply, serial interface, memory, input/output interface pins

Programming Language: BX BASIC, very close to Visual Basic.

Hardware Interface: simple digital and analog circuits interface to I/O pins.

Cost: $50.00

Lower level microcontrollers lack some of the basic circuitry of the mid-level modules. Notice, for example, that the PICs above is missing the clock crystal that the BX-24 above has (the long tube at the bottom of the BX-24). Likewise, some of the other chips, such as the voltage regulator, external EEPROM memory, and serial port buffer, are also missing. These must be added by the user if they’re needed.

Lower level microcontrollers typically require an external hardware programmer in order to be programmed. Typically, the programmer is attached to the PC via a serial, parallel, or USB cable, and the microcontroller is placed in the programmer to re-program it. Some programmers have cables to allow them to connect to the circuit in which the microcontroller is embedded in order to reprogram it. This is called in-circuit programming.

Low Level: Atmel’s AVR microcontroller

What is it: Microcontroller chip

Programming Language: Assembler, C, BASIC, Wiring

Hardware Interface: requires programmer to make power and timing circuitry in order to operate; thereafter, I/O circuits are similar to BX-24 and other middle and low-level circuits.

Cost: $1.00 – $15.00 depending on model used.

The Atmel controllers are the controllers at the heart of the Wiring, Arduino, and BX-24 modules.  There are a variety of controllers in the AVR family with different features.  Some feature more I/O pins, some feature more memory, some can speak USB natively, and so forth.  There is a good open source C compiler for the AVR chips, AVR-GCC.  It’s the basis for both the Wiring and Arduino development environments, and available on Windows, OSX, and Linux.  Objective Development has a nice package of the AVR-GCC for OSX users.

Low Level: Microchip’s PICMicro microcontroller

What is it: Microcontroller chip

Programming Language: Assembler, C, BASIC

Hardware Interface: requires programmer to make power and timing circuitry in order to operate; thereafter, I/O circuits are similar to BX-24 and other middle and low-level circuits.

Cost: $1.00 – $15.00 depending on model used.

Things to consider when picking a microcontroller:

Costs

How much do I want to spend? The higher the level, the higher the cost. But if it reduces the time taken between setting up and expressing yourself, it may be worth spending the extra money.

Time

How much work do I want to do? a higher level controller will generally minimize the amount of work you do to build your interface to the world. Lower level controllers will take more work before you have things working.

What programming languages/communications protocols/electronics do I already know? All other things being equal, pick a system whose components you know something about.

What’s the knowledge base like? Most microcontrollers have several websites and listserves dedicated to their use and programming. Quite often, the best ones are linked right off the manufacturer’s or distributor’s website. Check them out, look at the code samples and application notes. Read a few of the discussion threads. Do a few web searches for the microcontroller environment you’re considering. Is there a lot of collected knowledge available in a form you understand? This is a big factor to consider. Sometimes a particular processor may seem like the greatest thing in the world, but if nobody besides you is using it, you’ll find it much harder to learn.

Expandability/Compatibility

What other components is the microcontroller compatible with? Can you add on modules to your microcontroller? For example, are their motor controllers compatible with it? Display controllers? Sensors or sensor modules? Often these modules are expensive but they just snap into place without you making any special circuitry. If your time is worth a great deal, then these modules are a good buy. Sometimes even if you know how to build it with a lower level controller, a higher level system is worth the cost because it saves building and maintenance time.

What do I have to connect to? Are you connecting to a MIDI synthesizer? A DMX-512 lighting board? A desktop computer? The phone system? The Internet? Different microcontrollers will have different interface capabilities. Make sure you can connect everything together. Sometimes this requires some creative combinations of controllers if no one controller can speak to all the devices you want it to speak to.

Physical and Electrical Characteristics

How many inputs/outputs do I need? Every system has a certain number of ins and outs. If you can, decide how many things you want to sense or control before you pick your controller.

What kinds of inputs and outputs do I need? Do you need analog inputs and outputs, for sensing changing values, or do you only need digital ins and outs, for sensing whether something is on or off?

What kind of power is available to me? Does it need to be battery powered? Does it need to match the voltage of another device? Does it need to draw very little amperage?

How fast do I need to process data? Lower level processors will generally give you more speed.

How much memory do I need? If you’re planning some complex data processing or logging, you may need a microprocessor with lots memory, or the ability to interface with external memory.

How small does it need to be? A lower level controller generally lets you build your own circuitry, allowing you to reduce the size of the hardware you need.

The Economics of Microcontroller Development

One of the first questions that people usually ask when they learn that low-level and mid-level controllers can do many of the same things is, why the difference in cost? Why does a BX-24 or a Basic Stamp cost $50 and up, while a PIC or an SX micro costs less than $10? Furthermore, why are mid-level controllers’ development software free, while the software for low-level controllers is not? The answer, as you might expect, lies in the aims of the people selling them.

Companies that make mid-level microcontroller modules (NetMedia for the BX-24, Parallax for the Basic Stamp, Basic Micro for the Basic Atomm) are in the hardware business. They make their money by selling you modules. They typically serve a fairly low-volume customer who buys sometimes one, sometimes a dozen, but seldom more than a hundred modules at a time. In order to keep that customer coming back, they also develop proprietary development environments, so that you can’t reprogram their modules with the other guy’s software. However, so you won’t get upset about that, they give it away for free. It’s their belief that the unique combination of microcontroller, supporting circuitry, and development environment that they offer makes for the best way to use microcontrollers, and it’s worth a premium price. For beginners, they’re often right.

The open source hardware movement is making some interesting changes in this model through projects like Gainer, Wiring, Arduino, and others. Open source hardware makers recognize the value in mid- to high-level programming environments for lower level controllers as well as the desire of some users to go low-level once they understand more than the basics.  They also see the value in several groups working together on compatible systems.  Though the companies making these modules are in competition with one another for customers, they are also in collaboration to make their hardware and software better.  It can sometimes be a difficult collaboration, but in the long run, everyone’s tools get better as a result.

Open-source mid-level tools are  promising because they combine the best of both worlds. They let you learn at a mid or high level, then when you know what you’re doing, you can take the low-level guts of the device (an Atmel controller, in the case of these two tools) and gain the cost benefits of working low level. There are more hybrid open-source projects like this coming online all the time. A number of them were presented at the Sketching in Hardware workshop in 2006, and resource links were gathered online at that link, if you’d like to learn more.

Companies that make low-level controllers, on the other hand (Microchip, Atmel, Ubicom), are used to selling in very high volumes. They sell controllers to major manufacturers who put them in things like cars, toys, light switches, microwave ovens, and more. Their customers need to shave off every spare expense in order to make a profit on the end product. These customers willing to train engineers in low-level coding, to develop their own software tools, and so forth, in order to get a high-volume, low-cost chip. In fact, the low-level processors are the hearts of the mid-level processors. Check out the big chip on a Basic Stamp 2; it’s a PIC. Low-level manufacturers don’t spend a lot of time making development environments, so they can specialize on making a wide variety of good chips. They’re used to dealing in such large volumes, in fact, that a few chips more or less is no big deal. Most of them even offer a limited number of free samples on their websites, for prospective customers.

So what if you’re a beginner or small guy who wants to use low-level chips? You probably don’t have the time or expertise to develop your own compiler for it, and you’d rather not have to use the assembly language of the controller, if you can avoid it. This is where third-party software developers come in. Companies like CCS, Microengineering Labs, Crownhilll, Hi-Tech, and others make and sell custom higher-level development environments for low-level controllers. They’re in the software business. Because the profit margin on low-level controllers is already so low, they choose not to develop extra hardware to sell you premium hardware, but instead, they make really good software tools so that it’s easy for you to program the low-level controllers. Many of them also make custom hardware programmers, that make it easier on the hardware end too.

So where does this leave you, the hobbyist or beginner with microcontrollers? What should you choose?

Using mid-level microcontrollers will cost you relatively little up front, in terms of peripherals. The software’s free, and a serial cable is about $5.00. The various components you’ll need to make a typical project will run you $50 – $75, maybe. The microcontroller will run you another $50. If you know your way around a circuit, and you’re reasonably sure you won’t destroy your microcontroller, this isn’t a bad way to go. You can always keep the project intact and re-use the microcontroller for other projects. You’ll save yourself time not having to learn how a hardware programmer works, or which compiler to choose, or how to configure it. For the beginner seeking immediate gratification, mid-level is the way to go.

The only downside is that if you want to build many more projects, you’ve got that $50 cost on top of any other project costs each time. And if you damage the microcontroller, there’s another $50. And another and another, until you learn to be careful with your circuits.

Using low-level controllers, on the other hand, is more of a hassle up front. You’ve got to choose a development environment and pay for it, choose a hardware programmer and pay for it, and learn to set all the stuff up. For a decent compiler and a decent but cheap programmer, you’ll probably shell out about $300.00. You’ll spend a lot of time early on cursing and wishing you’d bought a Basic Stamp 2, or a BX-24. The advantage comes a bit later on, once everything’s set up and you begin making projects. First, if you fry the chip, you’re only out a few dollars to replace it. It gets better the longer you continue making microcontroller projects. You don’t have to spend more money on the development environment, and the chips are cheap. If you can work a bit for gratification, and you’re in for the long haul, it’s a good way to go.

One Response to All About Microcontrollers

  1. Pingback: elctronics projects links 13 @ Electronics Projects