Go Down

Topic: Basic Q: How can you connect a Digital Pin to both 5V *and* Ground?? (Read 3076 times)previous topic - next topic

JohnClavis

Forgive my dumb question, but I'm just starting to mess around with Arduinos and building a circuit using them, and I keep coming across descriptions about using the digital inputs in conjunction with switches... and every description includes a pull-up or pull-down resistor connecting the digital pin to +5V (if the switch is connecting to Ground) or Ground (if the switch is connecting to +5V), but the result is the same -- the digital pin is sometimes connected to +5V, and sometimes connected to Ground, depending on whether you're depressing the switch or not.

So based upon my very limited understanding of how an electric circuit works (electricity flows in one direction, from positive to negative, or from +5V to Ground), how the heck can you connect anything to +5V sometimes *and* Ground sometimes? That doesn't make any sense! If the pin is an input, you have to send electricity to it, so you'd connect it to +5V, right? And if the pin is an output, you connect it to Ground, right? You don't sometimes connect the anode of an LED to +5V, and sometimes to Ground! You don't do that with ANYTHING -- except, apparently, the pins of an Arduino! All the tutorials I read *show* me that you can connect a digital input to +5V for a LOW reading, and Ground for a HIGH reading (or whatever), but nobody ever explains WHY -- they just assert that it's so, and it goes against everything I understand about how electricity works. I can't sometimes connect an LED to Ground, and sometimes connect it to 5V, and expect it to do something in both situations!

From this website: "Often it is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K being a common value. " "Steer"? "Known state"? What the heck *is* an input pin, that it can be "steered"? What is it doing? Electrical circuits don't get "steered", they flow in a circle. You can't suddenly grab one part of the circle and flip it upside down and have electrons flowing backwards for one arc of the circle!

I'm assuming there's something special about a digital input on an arduino -- the fact that it's a logic gate, or high-impedance, or something -- that makes it capable of being treated in this way... I know it's not Arduino-specific... does the circuit oscillate quickly between sending out electrons and receiving them, such that it is okay with being connected to either positive or negative? It's just so bizarre and counterintuitive...

Anyway, if anybody could shed some light on this question, it'd really be appreciated... I've tried looking online for an explanation, but I guess I'm not picking the right keywords, because I keep finding helpful diagrams about *how* to wire the circuit, but everybody except me seems to already understand the underyling theory of *why* and *what* is going on, 'cause nobody's talking...

DVDdoug

#1
May 11, 2012, 02:43 amLast Edit: May 11, 2012, 03:03 am by DVDdoug Reason: 1
Quote
how the heck can you connect anything to +5V sometimes *and* Ground sometimes? That doesn't make any sense! If the pin is an input, you have to send electricity to it, so you'd connect it to +5V, right? And if the pin is an output, you connect it to Ground, right?
Digital electronics has two "states": 1/0, on/off, high/low.    Typically, 5V is a binary '1' or high, and 0V is a binary '0' or low.  Ground is the reference, so when you connect an input to ground, it's 0V or digital-zero.  If you connect an input to 5V, it's a digital-one.

It get's a little technical, because the voltages are not always exactly zero or 5V.   There are specs and standards for different part-families, and there is a "safety margin".  The output-specs are tighter than the input specs, so when a chip puts-out a "high" the voltage is always higher than the input needs to recognize it as high, and a "low"  is always lower (closer to ground or closer to zero) than the input needs to recognize it as low.

When we think about "high" or "low", we usually don't worry about current-flow.  We just want to know what the voltage-state is.   We do have to think about current-flow when we are deciding to use a pull-up or pull-down resistor, and we are trying to figure-out what resistor value to use.  Or, if we connect an LED to an output and we need to limit/control the current through the LED.

As you probably know, everything in the computer's memory is stored in binary (base-2, ones & zeros). That's all of the text, numbers, computer programs, etc.      Thankfully, it gets "translated" so users and programmers that program in high-level languages (C/C++, Java, etc.) don't have to worry about that too much.  But, sometimes you might want to convert a number in decimal (or hexadecimal) to create a particular binary number or "bit pattern" (or LED pattern) on the Arduino output.

If you connect an oscilloscope to a data bus (or address bus) line inside a computer, you will see it jumping like crazy between 0 and 5V (assuming a 5V bus).

If you could "hold" or "freeze" the data bus, you could look at all the data-lines and figure-out the binary nummber, but you wouldn't know what it means because you wouldnt know what the computer is doing at that microsecond....   On some systems, or some places in the system, the data/pattern is static, and you can measure/read the number.    Where I work, we do this all the time...  I can write a hexadecimal (base 16) 55, which translates to binary 010101... or hex AA, which translates to 101010... and I can test the data-lines with a 'scope or meter.

Osgeld

#2
May 11, 2012, 02:47 amLast Edit: May 11, 2012, 03:00 am by Osgeld Reason: 1
your over complicating it (though it can be a complicated subject)

inputs can be thought of like a light switch, you turn it off or on, but you are not part of the loop (well you could be but that would be bad), instead of a switch the inputs are connected to transistors, which take very little current to change states (because they are very high impedance)

connect a input to +5 and the transistor switches on, connect it to ground and it switches off  (it may be backwards I forget how they are arranged in the chip, doesnt really matter for this)

Since the transistor takes very little current to reach one state or the other due to its input impedance, if you left it unconnected it would wildly fluctuate as it picks up electrical energy from the air (RF interference, body capacitance whatever ... it actually makes an ok random number) so you need to "steer" (yea I dont like that term either) the pin to one state or another by using a pullup or pull down resistor

edit: and no you will find this type of setup on many digital electronics throughout the ages

war_spigot

If there was no resistor(pull-up or pull-down) to +5V or to ground, the input pin would be "floating."  That means electrical background noise could make it be read as a "1" or a "0"(read posts above me).  The resistor to +5V or ground keeps that pin at that voltage(+5V or 0V, "1" or "0") through a resistor until the button is pressed.  This keeps it from changing states erratically.  When the button is pressed, it makes a short to +5V or ground, and the current/voltage from the other portion(the pull-up or pull-down) of the circuit is negligible.

DVDdoug

#5
May 11, 2012, 03:20 amLast Edit: May 11, 2012, 03:24 am by DVDdoug Reason: 1
P.S.

Quote
Forgive my dumb question, but I'm just starting to mess around with Arduinos and building a circuit using them...

So based upon my very limited understanding of how an electric circuit works...
This stuff is complicated...  As a hobbyst you want to jump into electronics and start doing fun stuff right away, and that's cool.    But as a point-of reference, if you were taking Electronic Engineering at a university, you probably wouldn't get around do learning about microcontrollers 'till your 3rd or 4th year!   You'd be learning a bunch of basic-boring stuff first.  As a hobbyist, you need to learn some of the basics, but you can learn a little at a time as you go-along, and as you need to know it.

JohnClavis

#6
May 11, 2012, 03:41 amLast Edit: May 11, 2012, 03:48 am by JohnClavis Reason: 1
THANK YOU!!!!! LOL I totally get that; thank you for the light switch metaphor, and for the aside about RF interference, etc. This was exactly what I needed. You made my day!

(And thank you to everyone else for your responses, as well! They were very informative, but Osgeld hit the nail on the head in terms of what was perplexing me. I kept thinking the input pin was being connected to a conventional circuit, so it didn't make sense that you'd be connecting it to + and - in rapid succession, because, well, what's the other end connected to?? But now I realize that it's an isolated "sensor", almost, that registers whether it's being exposed to a + or a - charge...

your over complicating it (though it can be a complicated subject)

inputs can be thought of like a light switch, you turn it off or on, but you are not part of the loop (well you could be but that would be bad), instead of a switch the inputs are connected to transistors, which take very little current to change states (because they are very high impedance)

connect a input to +5 and the transistor switches on, connect it to ground and it switches off  (it may be backwards I forget how they are arranged in the chip, doesnt really matter for this)

Since the transistor takes very little current to reach one state or the other due to its input impedance, if you left it unconnected it would wildly fluctuate as it picks up electrical energy from the air (RF interference, body capacitance whatever ... it actually makes an ok random number) so you need to "steer" (yea I dont like that term either) the pin to one state or another by using a pullup or pull down resistor

edit: and no you will find this type of setup on many digital electronics throughout the ages

edit: I bolded the key line -- that one doesn't connect the pin to +5V or Ground because you're making it part of a conventional circuit, but in order to expose it to a change in electrical potential that will trigger a HIGH or LOW reading... right?

edit 2: and then I expanded on my "Thanks to everybody else" aside...

JohnClavis

P.S.

Quote
Forgive my dumb question, but I'm just starting to mess around with Arduinos and building a circuit using them...

So based upon my very limited understanding of how an electric circuit works...
This stuff is complicated...  As a hobbyst you want to jump into electronics and start doing fun stuff right away, and that's cool.    But as a point-of reference, if you were taking Electronic Engineering at a university, you probably wouldn't get around do learning about microcontrollers 'till your 3rd or 4th year!   You'd be learning a bunch of basic-boring stuff first.  As a hobbyist, you need to learn some of the basics, but you can learn a little at a time as you go-along, and as you need to know it.

There's no need to talk down to me, Doug.  XD Just because I'm not familiar with a concept doesn't mean I won't grasp it once it's been presented to me. And I'm quite aware of what 1s and 0s are, thanks. LOL I carved them by hand on my Commodore 64!

Osgeld

#8
May 11, 2012, 06:27 amLast Edit: May 11, 2012, 06:37 am by Osgeld Reason: 1
Quote
that one doesn't connect the pin to +5V or Ground because you're making it part of a conventional circuit, but in order to expose it to a change in electrical potential that will trigger a HIGH or LOW reading... right?

correct

Quote
and for the aside about RF interference, etc.

see, the inputs are so sensitive (like micro-amps, maybe less, I am not digging through the datasheet atm) that a little electrical noise coming off of your LCD monitors backlight, the vacuum cleaner, or even some potential difference tween your finger and the circuit, can trigger a change, so if you want them to not give you garbage you gotta yank them one way or another and drive them by pulling the other way.

IE you have them pulled to +5, or logic 1 and want to signal a change you gotta ground it to ground or logic 0