# How inputs really work?

Good day!

I've a couple of basic, but fundamental for me questions, and I will be really appriciated if someone drop me a line about them

• In a simplest demo DigitalReadSerial(http://arduino.cc/en/Tutorial/DigitalReadSerial) there is a pull-down resistor. I understand it purpose, but not clearly understand what current can flow in it while button is unpressed (is pin, configured as input have some voltage on it)? And why value is 10K, how it was calculated?

• When button is pressed in this demo it seems that pin is connected directly to 5V. Why Arduino is not short-circuit? Can I wire digital\analog input directly to 5V without any resistor?

• Next simplest demo AnalogReadSerial (http://arduino.cc/en/Tutorial/AnalogReadSerial). In one of extreme pot positions (it depends how I hook resistor to the circuit left or right) pin A0 seems to be connected direcly to 5V without any load. But connection between 5V and GND always loaded on 47K. Right?

P.S. Pages on I\O in "Foundation" section (http://arduino.cc/en/Tutorial/DigitalPins) are very clear, but these questions left in my head :)

Inputs to a modern microprocessor are essentially MOS Transistor gates. That means that they look like a small capacitor, with the micro able to sense the voltage on the capacitor. Essentially zero current is needed to maintain a voltage on the capacitor, and since it's small (a couple of picoFarads), not much current is needed to charge or discharge it either.

When you "short" the input to +5V, the capacitor is charged to 5V very quickly. If it were not for the pull-down resistor, the charge would STAY on the pin after the switch was opened again, and you wouldn't be able to tell that the switch had been released.

10k is a compromise between current consumption (when the switch is closed, the resistor is directly between GND and +5V) and noise resistance (if the resistor value was very high, things like fingertips and dust and moisture might have a lower resistance and could change the switch state as seen by the micro.)

Thank you for the reply, if I understand you right:

• When I directly connect +5V to digital pin in INPUT mode (as far as I understand this will not break anything), the current is very low (only to compensate this tiny capasitor leak), right?

• This capacitor is discharged to the ground using pull-down resistor, right?

• What will happen if I connect to +5V digital pin configured as OUTPUT?

Sorry for digging in such details, but this seems to be very big secret, because wasn't mentioned in any manuals I read :)

What will happen if I connect to +5V digital pin configured as OUTPUT?

If the output is set to a one then nothing. If the output is set to a zero you will instantly burn out the output pin.

pin A0 seems to be connected direcly to 5V without any load.

No A0 is connected to a tap on a resistor. The position of that tap depends on how far round the knob is twisted, so it could be connected directly to +5 or connected directly to ground, or anywhere in-between, depending on the angle of the knob.

this seems to be very big secret, because wasn't mentioned in any manuals I read

No secret - it's all so blindingly obvious (once you know ;) ) that it isn't even considered to be mentioned.

So, if this right:

• When configured as INPUT (analog or digital), pin has high resistance and can be directly connected to +5V (will read HIGH) or GND (will read LOW) without any harm to IC, right?

• When configured as OUTPUT pin has low resistance and can both sink (LOW state, provide GND) and source (HIGH, provide +5V). It is bad idea to connect

HIGH to GND or LOW to +5V, in both cases it will be short-circuit.

Is this model right? :)

When configured as INPUT (analog or digital), pin has high resistance .........

Right

When configured as OUTPUT pin has low resistance ........

Yes.

Just don't talk about a pin as having a resistance, it isn't really a resistance it is an impedance, same sort of thing but it is dynamic in nature where as resistance is always fixed.

Thank you for the response and the tip about impedance.

Just one step left:

• When I twist pot in AnalogReadSerial demo, my analog input get's connected from GND to 5V and voltage in between. In exteme positions pin is directly connected to GND or 5V. As I know from this thread it is safe to connect both to the INPUT. Right?

• If we can provide LOW for INPUT by connecting it directly to GND, why we need pull-down resistor, that's the question that really blows my head :)

In exteme positions pin is directly connected to GND or 5V.

Yes

You need a pull down resistor because if you connect a pin to nothing it floats. That is it picks up noise and reads randomly zero and one. It has to be fixed either by direct connection or by a resistor pulling it. If you connect it to ground and then have a switch connecting it to 5V you have got a short circuit when the switch is closed. You can have an input switch without any resistors only if you have a change over switch that connects the input to ground OR 5V. Most toggle switches are change over switches but push buttons tend to be just make / break.

If we can provide LOW for INPUT by connecting it directly to GND, why we need pull-down resistor

Imagine that you don't have a pull-down with your input connected directly to ground, and a switch connected from 5V to your input pin. See now?

It seems I finally figured it out:

• When button is not pressed it will work with or without resistor the same way. There is no current between input and GND.

• When switch is closed, without the pull-down the +5V will be connected directly to GND (short circuit), using pull-down resistor we limiting current, but let INPUT sense voltage.

Current is 5V/10kOhm = 0,5mA, right?

With switch which is able to connect to 5V or GND, pull-down is not needed.

Is this right?

Is this right?

yes :)

Thank you all, it seems to be the simple questions, but I'd like to understand everything to the end before burning something :)

but I'd like to understand everything to the end before burning something

And after you burn something it is important to know what went wrong as well. ;D

Section 13 (pg 75) of the ATmega48PA / ATmega88PA / ATmega168PA / ATmega328P datasheet shows the logic that is attached to the general I/O pins if you really want to get into it.

Unfortunately you have to download the 566 page version to see it, the summary does not show it.

http://www.atmel.com/dyn/resources/prod_documents/doc8271.pdf

It shows the internal pullup resistor that can be enabled/disabled (and no Pulldown, I read one forum listing that went on for pages over that), the register for writing the output and how it is setup to be read back in, the tristate control so it is not driven and can be used as an input, the input register, and a sleep control that pulls input pins low to keep them from floating. Another figure shows a little more circuitry being added for pins with alternate functions. Good stuff!

and a sleep control that pulls input pins low to keep them from floating

Is this an automatic feature or something you have to enable? The document was not accessible, unfortunately.

I'm using a 2x2 button pad and can't seem to accurately read presses.

Are you using the keypad library? I have a 4x4 matrix and the library code reads it very accurately. I had problems waking from sleep, couldn't get an interrupt generated. Solution was to write out 0's on the column lines before going to sleep so a row keypress could create a low for an interrupt, then writing back as ones on wakeup so the button scan code worked correctly. If the row pins are used for interrupts, they will not be clamped in sleep.

I should have specified; the buttons are not in matrix formation. There's a common 5v pin for all buttons and a grounding pin for each button.

Is it possible to somehow use interrupts with this configuration? I've been using a for loop to detect presses, but there seems to be a 1-3 second delay.

Section 13 (pg 75) of the ATmega48PA / ATmega88PA / ATmega168PA / ATmega328P datasheet shows the logic that is attached to the general I/O pins if you really want to get into it.

Unfortunately you have to download the 566 page version to see it, the summary does not show it.

Thank you for pointing that out. Arduino does a great job in isolating us from such low level details, but despite that datasheets are useful :)

Lensflare, Yes you can. Why not try this first: use the keypad library, and tell it you have a 1x4 keypad (or 4x1, whichever works)? Use the keypad demo in the playground. It works really well. If you want to add interrupt wake from sleep, I can help you with that after you get the basic keypad working. I did the same approach for a remote control - got the keypad working, got it sending via RF, got it to sleep, got it to wake using an interrupt.

Alright thanks, I'll try that. How does the library read presses? The board I'm using (http://www.sparkfun.com/commerce/product_info.php?products_id=9277) has diodes that prevent voltage from going from one of the input pins to the common pin. Would this interfere with the Keypad library?