Why are switches followed by a Resistor?

Hello guys,

why is this so? I mean if i want to check the switchstate via digit. input, why cant i put my +5v source in line with switch and directly connect it to the input?

why is there a resistor after the switch to ground, parallel to the digital input?

(im talking about the arduino starter kit projects)

is it because otherwise it would be a simle bypass, draining too much current and possibly damaging the input?

As always, a picture would help readers understand what you're asking, but it sounds to me you're asking about pull up or pull down resistors, which are explained here.

You could put up your question with a little more clarity. I am assuming that there is a resistor connected between the digital pin and ground. In that case it is called a pull down resistor. Digital input pins are sensitive to noise and give random readings if nothing is connected to them. So usually pull down resistors are used to keep the digital pin at ground whenever the switch is off. When the switch is on, 5v reaches the pin and reads a logic 1 or HIGH state. It is not necessary that you need to use an external pull down resistor. AVR microcntrollers come with internal pull up resistor. That fixes the state of the pin to high state. In that case, you have to provide ground through the switch and change the code accordingly. To enable the pull up resistor use the following:

pinMode(13,INPUT);
digitalWrite(13,HIGH);

Here, the pin 13 is is set as INPUT and then using digital Write as shown above activates the pull up resistor.

pinMode(13,INPUT);
digitalWrite(13,HIGH);

Is the "old" way, and although it still works, the "new" way is simper:

pinMode(13, INPUT_PULLUP);

FYI, aside from pull-up/down resistors, you'll sometimes see a resistor in series with the I/O pin and switch. That's usually just a safeguard in case you do something dumb -- like connect two outputs together -- or an attempt to limit fault currents or static discharge, etc.

Just read this
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
The mistake you are making is in thinking if nothing is connected to an input it is a zero. This is wrong it floats giving a random value.

it floats giving a random value.

Although there was a thread recently where the value of a floating pin wasn't changing when the writer needed it for random number generation.

So i figured out its a pull down resistor. And it makes perfect sense, so thanks everybody.

If i setup a pin as input, but even having no wires connected to it would be the same as connecting it to ground with no pull down resistor, right?

When do you use pull ups and when downs to adjust the logic level? I see that up makes it possible to provide lets say +5v and down a "perfect" 0. But both at once wouldnt work?

JimboZA:
As always, a picture would help readers understand what you're asking, but it sounds to me you're asking about pull up or pull down resistors, which are explained here.

...neither of which are needed on Arduinos. In 99.99999% of cases you can use the Arduino's internal pullup.

If i setup a pin as input, but even having no wires connected to it would be the same as connecting it to ground with no pull down resistor, right?

Wrong.
Have you read that link? It answers your questions.

JimboZA:

it floats giving a random value.

Although there was a thread recently where the value of a floating pin wasn't changing when the writer needed it for random number generation.

Yes I read that. The poster was wrong in his assertion but we never got to the bottom of it.

Well I suppose just because a pin is floating, it doesn’t have to change; I guess that depends on the Elders of the Laws of Thermodynamics.

I think it depends upon the ambient environment. The reason a floating input is 'floating' is that there is nothing tiying it to a given state (V+ or GND), so it can act like an antenna. If there is enough noise in the environment, it can pickup enough signal strength to drive it in to the 'ON' state, without any control.

123Splat:
I think it depends upon the ambient environment. The reason a floating input is 'floating' is that there is nothing tiying it to a given state (V+ or GND), so it can act like an antenna. If there is enough noise in the environment, it can pickup enough signal strength to drive it in to the 'ON' state, without any control.

Not only is reading a floating pin subject to random return values due to external 'noise' but also simply from capacitance coupling of other signals running close to the pin's input circuit from PCB tracing and even internal chip circuitry.

Lefty

The word "random" has several meanings. When you are trying to get input from a button ( and not trying to build some kind of random number generator ), the key point is that if you don't have either a pull-up or a pull-down resistor, then the state of the input pin when the button device is open, is indeterminate. You cannot rely in your code on the input pin having a predictable state, that is what random means in this context.

Well, isn't that what most everybody has been saying?

I am just reading through threads and got confused by this? should it be pinMode(13,OUTPUT);?

JimboZA:

pinMode(13,INPUT);

digitalWrite(13,HIGH);

I.E. can you digitalWrite to an input?

matelot:
I am just reading through threads and got confused by this? should it be pinMode(13,OUTPUT);?

JimboZA:

pinMode(13,INPUT);

digitalWrite(13,HIGH);

I.E. can you digitalWrite to an input?

That's they way you enabled input pullups before the IDE 1.0 came out. However, given pin 13 on the Uno (and some others) is connected to the builtin LED which has a resistor attached, you would need to take that resistor into account when you are doing input.

So in some of my code that I originally wrote when I used the 0.22 IDE and then migrated to 1.0 looks like:

#if defined(ARDUINO) && ARDUINO >= 100
    pinMode (pin, INPUT_PULLUP);

#else
    // Pull up method for 0.21
    pinMode (pin, INPUT);
    digitalWrite (pin, HIGH);
#endif

    state = digitalRead (pin);
    value = (state == LOW);
    // value is true if the button was pressed.  Given it is an INPUT_PULLUP
    // the return value is inverted.

And random doesn’t necessarily mean changing. The state isn’t really indeterminate - it is a definite value when you read it. The problem is that it is unpredictable.

matelot:
I am just reading through threads and got confused by this? should it be pinMode(13,OUTPUT);?

JimboZA:

pinMode(13,INPUT);

digitalWrite(13,HIGH);

I.E. can you digitalWrite to an input?

Michael's answer is correct. I'm going to reiterate in case another point of view helps explain what's going on.

Each I/O pin on the AVR is controlled by a "port". AVR Ports are 8-bit pin controllers, meaning each port can control up to 8 I/O pins. There are three registers that define the pin's behavior (in general I/O context, which does not include whatever special function they may be capable of performing -- like serial or PWM). On the AVR, ports are given an alphabetical index (A-L), usually starting from Port A and incrementing to however many ports are necessary to control the number of I/O pins on the chip. Let's take an example of the three registers responsible for Port A:

DDRA: This register defines the I/O direction of the pin -- input or output.
PORTA: This register, when written to, has 8 bits that set the drive level of output pins -- high or low.
PINA: This register, when read, has 8 bits that correspond to the driven level of input pins -- high or low.

Because Atmel likes to be sneaky and give registers multiple purposes, PORTA and PINA also have alternate functions when used in their opposite contexts:

PORTA: For pins that are set as inputs, setting that pin's bit here enables the internal pull-up resistor.
PINA: Writing to this register when the pin is an output toggles its drive level (low->high, high->low).

So, in any version of the IDE, when you do this:

pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

... it sets the appropriate bit in the DDRx register (where x corresponds to the port representing pin 13) to make the pin an output, and then sets PORTx to drive the pin HIGH. Even though it's not an input pin, you can still read its level by doing this:

int  x = digitalRead(13);

... and you will get the level of the pin, which will be whatever you set it to with digitalWrite(). Now, if (again in any version of the IDE) you set the pin as an input:

pinMode(13, INPUT);

... this changes the bit in the DDRx register to change the pin to an input. If the bit in PORTx is still set, like would happen with this explicit sequence:

pinMode(13, INPUT);
digitalWrite(13, HIGH);

... you've set the pin to an input, and written to PORTx, which in input mode, turns on the pull-up resistor. As mentioned, in later version of the IDE, you can also accomplish that same thing like this:

pinMode(13, INPUT_PULLUP);

Behind the curtain, the end result is the same either way, and both are perfectly acceptable. There's no reason you have to use the new syntax. At least not yet -- there's no move to deprecate the old syntax that I know of. In fact, changing this behavior would require digitalWrite() to examine the state of the DDR register and refuse to work if the corresponding pin were an input, which seems pointless to me. It would only add overhead, making the function slower.