Microcontroller Interfacing –  Part 3

Microcontroller Digital Output Basics



This section goes over the basics of microcontroller output ports.  It will cover how they can be used to interface to the outside world, and some of the limitations.  A separate section will cover use of microcontroller I/O pins  as inputs.

Digital I/O Ports

Microcontrollers generally combine their output pins into 8 bit ports.  Op code instructions allow easy manipulations of the values as a byte.  Byte operations are convenient when all 8 bits are part of a data byte.  At other times you will want use each bit for a different specific purpose.  You might want one bit to control an LED, a couple more to control relays, etc.  There will be op code instructions that let you manipulate individual bits. If you program in C, Basic, or other high level language, the compiler will have instructions for controlling individual bits.

Most I/O pins on a microcontroller can be set as digital inputs or outputs.  You will want to configure them in the desired direction early in the software that is executed when the microcontroller is powered up or reset.  There will be special registers for this purpose.    

The individual pins of an output port can have one of two output voltages depending on how the bit is set in the port’s output register. If the bit is set to a “1”, the output pin will have a high voltage. The value of the voltage will usually be the same as the microcontroller’s supply voltage. Sometimes setting a bit to a 1 is also called setting it high, or just setting the bit. Depending on the system design, this will usually be 3.3V or 5V.  Some new microcontroller designed for low power battery applications run at lower voltages.

If the output pin is set to a “0”, the voltage at the pin will be close to zero volts.  This is also called clearing the bit or setting it low. 

The different voltages are obtained by internal transistors that switch the output pin to the supply  voltage or ground. Figure 3-1 is a simplified diagram of an output pin, arbitrarily labeled P0 for this example. 

The red transistor will be on when the output is set to a logical ‘1’. Output pin P0 will be connected to +5V in this example.   If the output is set to a logical ‘0’, the blue transistor will be on, connecting the output pin to ground, or 0V.  Only one of the two transistors can be on at a time.  Both transistors will be off if the pin is configured as an input and in some other special conditions, but one of the two will be on if the pin is configured as an output.

Driving Loads

An output pin not connected to anything is not of much interest.  To do something useful it must be connected to another device, referred here simply as a “load”.  A real world load could be an LED, a lamp, a transistor, or some other circuit element.

Generally there are two ways to connect an output pin to a load. The first is where the microcontroller supplies the current to drive the device.  The microcontroller is referred to as the source.  Current flows from the microcontroller power to the output pin, through the load and to ground. This configuration is shown in Figure 3-2a.  

Figure 3-1

The microcontroller pin can also “sink” the current as shown in Figure 3-2b. Here the current flows from the power supply through the load and through the output pin to ground. It is important that the load be connected to the same power supply line as the microcontroller, or you can destroy the IC.

Output Currents

Microcontrollers are somewhat delicate devices and the I/O lines can only carry a relatively small amount of current.  The current limit will depend on the type of microcontroller, and the specific pin.  There will usually be a maximum total current the pins of a single 8 bit port can handle, as well as a limit for all of the outputs for the entire microcontroller. Exceeding the limits will destroy the microcontroller.

To find out what the maximum currents are, you need to look at the data sheet for the microcontroller. PDF formatted data sheets can be downloaded from the manufacturer.  These can be quite large, several hundred pages for a fairly complex one.  Look for a section titled “Electrical Specifications” or something similar. Usually in the section there will be a table called “Absolute Maximum Ratings” or similar. You will find a table containing a number of specifications including:  

Figure 3-2




Output Source Current   



Output Sink Current   



Ioh stands for Current (I), output high.  Iol stands for current, output low.  Currents are traditionally referenced as  going into a pin. If the output is high, the current flows out, so it is given a negative number.  In some microcontrollers the maximum sink and source current limits for a given pin might be different.    You will also want to check the portion of the data sheet that covers the port you are using. Some ports have more drive capacity than others on some microcontrollers.  Exceptions will be spelled out there.

Some manufacturers such as Atmel will not give a single value for the maximum current allowed for output pins. Instead they provide a graph that plots the Voh vs. Ioh and Vol vs. Iol.  Voh stands for Voltage, output high, and Vol is Voltage, output low.  As a first approximation, Voh is equal to the supply voltage, and Vol  is zero volts. However, as the current increases, the voltage drop across the output transistor will increase. This results in the voltage being less than the supply voltage when the pin is set to a 1, and the output voltage being more than 0 volts when the output is set to a 0.

If you try to sink or source large amounts of current, the change in the output voltage can cause problems in your circuit operations. A simple and practical rule of thumb is to find the current when Voh is 90% of the supply voltage and use that as your maximum Ioh.   Similarly, find the current on the chart where Vol is 10% of the supply voltage and use it as the maximum Iol.  This will keep you out of trouble in most situations.

It should be noted that some microcontrollers have a few I/O pins that are open collector.  These don’t have the upper (red) transistor as shown in Figure 3-1. These will need external pull up resistors.


A microcontroller I/O pin can be set by the program to operate as an output. The output voltage will be close to the supply voltage when the output is set to a logical 1.  The output voltage will be close to 0 volts when the pin is set to a logical 0. 

An output pin can either source or sink the current to or from a load. It is important not to exceed the maximum current ratings, which can be found in the microcontroller’s data sheet.  

 Arduino  Tips

The I/O pins of the ATMega328 used on the Arduino Duemilanove can sink and source 40 ma per pin.  There is a maximum of 200ma total for the package.  About 25 ma should be reserved for the internal operation of the ATMega328 leaving about 175ma maximum current for the rest of the I/O pins.

Unified Microsystems  carries unique electronic equipment, modules and kits for engineers, electronic hobbyists and Amateur Radio operators.

© 2009 - 2016 Gary C. Sutcliffe



Created with the QTH.com SiteBuilder.