Microcontroller Interfacing –  Part 5

Microcontroller Digital Input Basics



This section describes the basic characteristics of a microcontroller I/O pin when it is configured as a digital input.

Input Pins

Most of a microcontroller’s I/O pins can be configured as an output or an input. Part 2 described the basics when a pin is configured as an output. This section describes it when it is configured as an input.  A special register will control if a pin is an input or output.  You need your program to set up the port direction registers as an early step when power is applied to the chip or it comes out of reset.

I/O pins are usually configured in groups of 8 bit I/O ports.  The program can read the port and will get a  value between 0 and 255 depending on the states of the input pins.  In assembly language programming there will usually be op code instructions that allow reading a single pin of a port. C compilers will usually implement single bit functions as well. I/O functions are not defined in standard K&R C, and each compiler handles them a little differently. Otherwise the programmer will have to read the entire port and mask off the other bits. 

A pin with a low voltage (ideally 0 volts) will read as a logical 0.  A pin with a voltage near Vcc (or Vdd) will read as a logic 1.  As a first approximation, any input less than half of Vcc will read as a 0, and any input over Vcc/2 will read as a 1.

There is a voltage band however, right around Vcc/2, where there is no guarantee what state will be read.  Refer to Figure 5-1. This shows the logic thresholds of a typical microcontroller. The X axis represents the supply voltage, Vcc  (Vdd on some data sheets).  The Y axis represents the voltage on an input pin. The black dashed line is where the input is 1/2 Vcc. 

For a given supply voltage if the input pin voltage is below the green line the pin will be read as 0. Above the blue line the input will read as 1. Between the green and blue lines is a “no man’s land” where it might read as a 0 or it might read as a 1.  If the signal is near Vcc/2, a little noise can cause the voltage to jump over to the other side, and cause an incorrect reading.  

Figure 5-1

As a general rule, you will want the input levels either near  0 volts or  the supply voltage to avoid incorrect readings. You will notice that when the supply voltage is low, the range for a 0 or 1 gets very small. With a low Vcc a little noise on an input can cause an improper read. For applications with noisy electrical environments, it is usually best to run the microcontroller near the high end of the allowable Vcc range. On the other hand, running at higher supply voltages will mean more power consumption. Those are the kind of tradeoffs that you will need to make as a circuit designer.

The actual levels and slope of the thresholds indicated by the blue and green lines of Figure 6-1 will depend on the microcontroller in question.  They will also change a bit depending on the device temperature.  Some special pins such as the reset pin might have different thresholds than regular I/O pins.

Referring again to Figure 5-1, the red line shows the input voltage equal to the supply voltage. You don’t want to subject inputs to voltages above Vcc or you can damage the IC.  For that matter, you do not want the input to go below ground (0V). That can cause the internal circuitry in the IC to latch up, and possibly draw excessive current and damage or destroy the IC.

Pull ups and pull downs

Sometimes you want an input to read as a 1 or 0 as a default. Suppose you have a sensor on a cable that plugs into your device. It is possible that the user will disconnect the cable. If the input pin is left floating, it might sometimes read as a 1, sometimes as a 0.  Your code might interpret this as changes from a sensor and not act the way  you want. 

Putting a pull up resistor will set the input voltage near Vcc and it will read as a 1.  A pull down resistor will bring the voltage near 0V, and it will read as a zero. Figure 5-2 shows pull up and pull down resistors. The switch, sensor, or other component that generates the normal 1 and 0 voltages must be able to over drive the resistor.

What value resistor should you use for these resistors? There are no hard and fast rules, but there are some guidelines.  A resistor with a relatively low resistance is called a strong pull up.  That is because it takes a lot of current to pull it down.  Alternatively, a high resistance pull up is a weak pull up because it will not take much to pull it down.

If your circuit is in an electrically noisy environment, some of that will get coupled into your circuit. If a weak pull up is used, the noise could be powerful enough to cause a false reading.  If you use a strong pull up, the noise risk is reduced, but the circuit driving the input must be able to handle the load, and the system’s over all power consumption will be higher. You have to understand the conditions your circuit will operate in, and make the proper compromises.

Some microcontrollers have internal pull ups.  You set a bit in an internal register to turn them on.  These are nice because you don’t have to add the pull up resistors in the hardware design, saving cost, board space and assembly time. The value of internal pull ups is usually pretty high (weak pull up), and the actual value often has a huge range.  Internal pull ups are handy, but evaluate the parameters in the data sheet before using them.

So, what values should be used for pull ups and pull downs?  There are no hard and fast rules, but generally anything under a few thousand ohms is a strong pull up.  Weak pull ups often reach 40-50KΩ.  I don’t like to go over 10KΩ unless I have a specific reason, like needing to keep power consumption extremely low.

Switching between Input and Output Mode

In some applications you may want a pin to be an input some of the time, and an output at other times. An example is where the microcontroller is communicating with another system or IC. Sometimes the microcontroller is sending data to another IC, and at other times the IC is sending data to the microcontroller.  I2C and Two Wire Interfaces are  common examples where an I/O pin is used as both an input and output.  

Figure 5-2

Arduino Tips

You can enable the internal pull up resistor on an Arduino pin with the following instructions:


pinMode(pin, INPUT);       // set pin to be an input

digitalWrite(pin, HIGH);    // turn on pin's pullup resistor


One thing to keep in mind is that some microcontrollers require one or more clock cycles after a direction change before the data on the pin can be trusted. Many designers have wasted a lot of time trying to figure out why their system does not work because they overlooked this fact.


Microcontroller pins can be used as inputs to sense conditions in the outside world. Digital I/O pins can only detect ON or OFF (1 or 0) states.  A high voltage (near Vcc) will read as a logical 1, and a low voltage (near 0V or Ground) will read as a logical 0.

Gotcha Checklist

1. Ensure signals applied to digital inputs are either near Vcc or ground.

2. Size pull ups and pull downs appropriately.

3. Allow time enough time when switching an I/O pin’s direction.

Arduino I/O Expander

© 2009 - 2016 Gary C. Sutcliffe



Created with the QTH.com SiteBuilder.