Inputs

One thing that often puzzles beginners is the way inputs work, here we look at pulling up, pulling down and floating.

 
 

The simplest form of input to a processor like an Arduino is single logic level input, it can be read in software and is the most fundamental was of getting the outside world into a program. However unless you connect it up correctly there can be unexpected results.

 

The care and feeding of arduino inputs

What is an input?

The simplest form of digital input reads a logic level. That is the logic level wired to the input is transferred to the computer program when the program executes a read instruction. On the Arduino this is simplified by the use of the Pin level of abstraction. That is each individual input has it’s own number and that number is used in the read instruction. I say this is an abstraction because actually 8 inputs are read at the same time in the form of a bye and software behind the scenes presents this to the programmer as just a single bit. The result of reading a digital input can be described in a number of ways, basically binary names such as, high / low, one / zero, true / false, open / closed, up / down and many more.

Background

An input is simply a feed into some switching circuits and it has to be connected to something to give a reliable and meaningful reading.


Many beginners think that if you connect nothing to an input it will read as a low, this is not true. It will certainly read something but without any connection, the input is said to be floating. In practice this usually results in reading a succession of highs and lows, seemingly at random. In fact what is happening is that the input is acting as an antenna and picking up lots of signals out of the air. This is mainly mains interference but can be any other signals. Also if you touch the pin then the situation gets worse and this often startles and amazes a beginner but this is perfectly normal behaviour.


So in order to detect the state of a switch we have to switch the input between +5V ( or the processors supply voltage) and ground. This can be done with a single pole change over switch (or SPCO sometimes called SPDT). This sort of switch will allow the common connection to be connected to either of the other two connections but not both at the same time. The diagram to the let shows how this is connected to an arduino.


There are two major problems with a circuit of this type. The first is that these switches are normally of the break before make type. This means that one connection is broken before the other one is made, therefore for a small instance of time during the transition from one state to the other the input is floating and therefore can be subject to noise.  If you use a make before break type of switch things are even worse because there is a brief short circuit across the power supply and this causes heavy currents to flow and the loss of supply voltage. The other major problem with using a changeover switch is that the switch is more complex and hence expensive than it needs to be. Most switches simply consist of two connections with a push to make action, you can also get push to break but these are not as common.


In order to use a push to make switch we must use a resistor to make sure that the input is normally connected to a fixed voltage level.  For an Arduino it matters not if this is a logic high or logic low but traditionally in digital electronics this resistor is connected to the positive supply voltage. This was because it required less current to generate a logic high on the input than it did to generate a logic low. We say it is a pull up resistor because it pulls the input up to high. Then all the switch needs to do is to connect the input pin to ground, thus changing a logic high to a logic low.

The other alternative is a pull down resistor, here a
resistor is connected to ground and makes the input a logic low. Then the switch connect the input to to the positive supply and the input reads as a logic high. Both situations are shown in the diagram to the right.


Some people like to minimise the number of external components and so some systems have built in pull up resistors, non that I know of have built in pull down resistors. The processor in the arduino has a built in pull up resistor but by default it is not enabled. It is simple to enable it, just perform a digital write with a value of high after you have defined the pin to be an input in the setup() portion of your code. Or in Arduino 1.x there is an INPUT_PULLUP option for the pinMode() function.


So would you ever want to use an external pull up resistor when there are internal ones fitted? The answer is a yes, this is because the internal pull up resistors are only weak pull ups, that is the value is about 30K and it is not so strongly pulling up. This could be a cause of problems if you have a noisy environment or run long leads to the Arduino. In these situations a stronger external pull up resistor of say 3K or even 1K could reduce the circuits susceptibility to noise or interference.

 

Feeding

Any advantages with using a pull down resistor?

The only one I can think of is that with a push to make button or switch then the un-pushed state reads as low and the pushed state reads as high and some people find that more intuitive, it is not but beginners often think it is. If that is your concern you can simply define some new words:-

#define pushed LOW

#define unpushed HIGH

and use them in your code to make it more readable.

Any disadvantages with using a pull down resistor?

I would say yes to this. Suppose you have a switch on a long wire you have to run the signal and a power line with a pull down arrangement. This potentially is more dangerous than running a signal and ground. If the power line shorts out to anything you have major problems but if the ground shorts out to something it is more likely to be less of an issue.