# What is the purpose of a pull-up resistor in a microcontroller?

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:

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.

• It's unclear what's unclear to you. Do you struggle with the overall concept of the resistor and why it is there or do you have trouble understanding the "activation" logic that you described? Please elaborate. Commented Aug 16, 2015 at 16:22
• Take a look at iamsuhasm.wordpress.com/tutsproj/avr-gcc-tutorial and see if it helps you. Commented Aug 16, 2015 at 16:36

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.

The pull-up is used to keep the input from floating for certain input types.

For example a switch may connect the pin to ground when it is closed. When the switch is open, we need to connect the pin to a logical 1. The pull-up archives this. The pull-up could be external but using a micro with the possibility of internal pull-ups saves cost and board space.

There is something called a datasheet and it (and application notes and reference manuals from the manufacturer or IP owner in the case of ARM) should always be your first point of reference.

The pull-ups are described as so in the full datasheet:

As you can see, the pull-up is automatically disabled when the output is enabled or the global pull-up disable is active.