An I/O line is basically a pin (a wire) attached to a Microcontroller chip. I/O stands for input/output, and that's exactly the function these little pins serve. An I/O line could be used to blink a light, read a push button, make beeping noises come out of speakers, etc.. Modern microcontrollers can have anywhere from 4 to 132 or more pins dedicated to such general I/O functions. These lines are digital in nature. They can have either of two states: high (Often 5 Volts) or low (0 Volts).

A group of 8 I/O lines is often referred to as a PORT. Each port has a couple of locations in the microcontroller's memory by which to configure it. Such locations are known as Special Function Registers or SFRs because they configure the various on-chip features of a microcontroller. Every I/O line can be used as either an input or an output depending on how it is configured. If it is configured as an output, it can be used to control things such as LED lights. An LED can be turned on by simply writing a bit to a special function register in the program. That same LED can be turned off by clearing the bit.

A general schematic for connecting an LED to a microcontroller:

 ____________
| Micro-     |I/O (Port A, Bit 0)     LED   Resistor
| Controller |-------------------------D|-----/\/\----,
|____________|                                        |
                                                      _
                                                      .
                                                     GND


It's important to note that the I/O lines on some older microcontrollers might not be able to source enough current to directly run an LED as connected above. Instead, the I/O line should be connected to the base of a transistor, and the transistor can then be used as a switch to turn on the LED. This is especially true of very old microcontrollers from the late 1980's, but it is rarely an issue today. Some microcontollers can sink enough current, but they cannot source enough. In that case, the LED would need to be connected to Vcc (5 Volts) instead of ground. To light the LED in that case, the corresponding bit would have to be cleared instead of set.

In the C programming language, the code to turn on the LED in the above circuit is likely to look something like this:

PORTA|=1; // Set bit zero of port A

And the instruction to turn it off would be:

PORTA&=~1; // Clear bit zero of port A

There have been questions about the code presented above. In C, the operator |= means "Bitwise OR the left value with the right value, and store the result in the left value." This is used to set one or more bits of the port. The operator &= means "Bitwise AND the left value with the right value, and store the result in the left value." It is used to clear bits of the port. The Tilde operator is a bitwise NOT. So, that second line of code translates to English roughly as "Let PORTA equal PORTA but not 1."

If a different bit of the port is used, a value besides one will be required in the expression:

Bit #   Value
0       1
1       2
2       4
3       8
4       16
5       32
6       64
7       128
...

Configuring an I/O Line as an Input:

Often there is a way to specify whether an I/O line will use one of the microcontroller's internal pull-up resistors or not. The pullup feature is only valid when the I/O line is configured as an input. A pullup resistor will cause disconnected (a.k.a. "Floating") I/O lines to be intrepreted as high by a program. An application where this feature is useful is when a switch is connected between the I/O line and ground. If the switch is open, the corresponding bit in a special function register will be a one. If the switch is closed, the corresponding bit will read zero.

A general schematic for connecting a switch to a microcontroller:

 ____________
| Micro-     |I/O (Port A, Bit 0)     Switch
| Controller |__________________________/_____________
|____________|                                        |
                                                      _
                                                      .
                                                     GND

Example code:

if(PORTA&1)
 {
  // switch is open
 }
else
 {
  // switch is closed
 }

The voltage threshhold between high and low on an input line is often roughly 1.3 Volts (TTL levels), and there is sometimes a little bit of hysteresis added in the form of Schmitt Trigger circuits. In projects where I/O lines are left disconnected, they should be configured as inputs with pullups because it saves electricity. This is due to the sort of transistor circuit employed in most microcontrollers to perform I/O functions.

Log in or register to write something here or to contact authors.