Electronic – the purpose of a pull-up resistor in a microcontroller

avrmicrocontrollerpullup

I'm reading about the AVR microcontroller. In a chapter on I/O ports I've stumbled accross a concept that I have trouble understanding: the pull-up resistor.

Here is a quote from the textbook:

PORT register role in inputting data:

There is a pull-up resistor for each of the AVR pins. If we put s into bits of the PORTx register, the pull-up resistors are activated. In cases in which nothing is connected to the pin or the connected devices have high impedance, the resistor pulls up the pin. See figure 4-4. If we put 0s into the bits of the PORTx register, the pull-up resistor is inactive.

And the associated diagram:

pull-up resistor diagram

Now, bear with me; it is my understanding that we control the data direction of the ports by writing 1s or 0s in the DDRx register. So if I want to read data from a port I clear the DDRx register (0s) and get my data from the PINx register. Conversely, if I want to write data I set the DDRx register (1s) and write to the PORTx register.

Okay? So the book gives assembly-code examples of how we activate the pull-up resistors of a port. We do this by writing to the PORTx register without setting the DDRx!

What is the function or desired effect of doing this? It is not clear to me, even after having read half of the book.

Best Answer

A pull-up resistor does two things:

  1. Prevents the pin from floating

    Floating CMOS inputs can result in increased power consumption, or in some cases even destruction of the device. A pull-up holds the input to a known good state.

  2. Defaults the input to a high value

    This is useful when dealing with open-drain outputs that work in a wired-OR/pull-down fashion such as interrupt lines, or even with simple buttons.

It also causes the input to source a small amount of current for detection purposes, but this usage is much less prevalent than the previous two.