3
\$\begingroup\$

I am very curios what happens to pin when it is in input mode and output mode.

Question regarding input mode:

  1. Input reads voltage on pin or current?

  2. If it reads voltage, is it able to sink in current, in oher words act as a ground?

  3. If it can act as a ground, is there any internal resistor that limits the current?

Questions regarding output mode:

  1. While in output mode, and I pull pin high it outputs current, while it is in low can it sink in current, in other words does it act as a ground pin when it is low?

  2. While in output mode does it have any internal resistor that will limit the current?

Example MCU is Atmega328p

\$\endgroup\$
  • 1
    \$\begingroup\$ Regarding your output mode question and the questions 2/3 from input questions might depend on the microcontroller. It will probably help answerers if you specify the chip you're using or intend to use \$\endgroup\$ – FMashiro Jan 21 at 11:38
  • \$\begingroup\$ @FMashiro specified it: atmega328p \$\endgroup\$ – Anton Stafeyev Jan 21 at 11:39
  • \$\begingroup\$ @FMashiro also would be cool to know how to look for this information in datasheet \$\endgroup\$ – Anton Stafeyev Jan 21 at 11:42
  • 2
    \$\begingroup\$ "would be cool to know how to look for this information in datasheet" There is a functional diagram of the I/O ports in the datasheet. That, plus the electrical specifications, answer all your questions provided you have the knowledge background to understand electronic schematics and the components on it. (On the datasheet I found: page 85) \$\endgroup\$ – Oldfart Jan 21 at 11:54
  • 1
    \$\begingroup\$ @MituRaj the answer was complete and answered all of my questions. why shouldnt i accept it ? \$\endgroup\$ – Anton Stafeyev Jan 21 at 12:30
9
\$\begingroup\$

Atmega328p datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

Input reads voltage on pin or current ?

Voltage. However, for digital inputs, it is only a binary reading:

  • Voltage closer to VCC voltage value is treated as "high" input (a "1" in software)
  • Voltage closer to Ground is treated as a "low" input (a "0" in software)

For the specific of "closer", you'll have to refer to the datasheet page 322 where you can find "Electrical Characteristics > DC Characteristics":

  • VIH refers to the "high" input voltage reading
  • VIL refers to the "low" input voltage reading

Any voltage in between VIL and VIH thresholds will retain its previous value in software until it crosses VIL or VIH one more time. This mechanism is commonly referred to as "Schmitt Trigger" mechanism, more on that here: http://www.ti.com/lit/an/scea046/scea046.pdf

Page 609 of the datasheet shows graphs for "Pin Threshold and Hysteresis". You can see that VIL and VIH vary with temperature and voltage and you'll find information about the "hysteresis" zone of the schmitt-trigger.

If it reads voltage, is it able to sink in current, in oher words act as a ground? If it can act as a ground, is there any internal resistor that limits the current?

No, it is not able to sink current, inputs are high-impedance. It cannot act as a ground.

Datasheet page 323 shows you the "Input Leakage" current for both "low" and "high" states (IIL and IIH).

While in output mode, and I pull pin high it outputs current, while it is in low can it sink in current, in other words does it act as a ground pin when it is low?

The wording of "act as a ground pin" is a bit ambiguous. In one way, yes it is returning current into the ground path with very-low impedance so it can be considered ground at that moment. However, especially if you intend to use it as a permanent ground pin, be aware of the current limit called IOL (for "low" state).

The limits vary with output voltage and temperature and you can find graphs page 607 of the datasheet (in section "Pin Driver Strength"). In this section, you'll also find graphs for IOH which refers to the output current in a "high" state.

Make sure in your design to not exceed the IOL current values as you may well end up damaging your microcontroller pin. For that reason, it is not a "true" ground pin.

While in output mode does it have any internal resistor that will limit the current?

Most microcontrollers general-purpose outputs does not have current-limiting feature. In case you exceed the maximum output current for too long, the internal driver will fail and you won't be able to use this pin as an output anymore. This is the case for the Atmega328p

\$\endgroup\$
  • \$\begingroup\$ Thanks a lot for your answer! \$\endgroup\$ – Anton Stafeyev Jan 21 at 12:31
  • \$\begingroup\$ No problem! I hope this info helps. Are you working on an Atmega328p custom design? \$\endgroup\$ – Cisco25 Jan 21 at 12:47
  • \$\begingroup\$ yes, i am more of a programmer guy, and i dont know much of electric part of it. and now i need to implement a board my self basically starting from programmer, and reason i asked question is how really about pull up resistor and what would happen when when i attach it. will the current sink to ground basically. and since it is 10k resistor i wont excede llimitations of a pin. \$\endgroup\$ – Anton Stafeyev Jan 21 at 13:02
  • \$\begingroup\$ You're correct, you'll be safe with a 10K :) \$\endgroup\$ – Cisco25 Jan 21 at 13:05
3
\$\begingroup\$

Question regarding input mode:

1) Input reads voltage on pin or current ?

Voltage.

2) If it reads voltage, is it able to sink in current, in toher words act as a ground?

Yes.

(edit: Some unusual microcontrollers might even source current, which you have to sink in order to make the pin low)

3) if it can act as a ground, is there any internal reisistor that limits the current.

Yes. If there wasn't a resistance, then you wouldn't be able to create a voltage for it to measure, would you?

Read the datasheet for your particular microcontroller. Since I don't know your microcontroller, I'll use the ATMega328P as an example. Find the section called (probably) "DC Electrical Characteristics". It should tell you which voltage is allowed for a low and high input ("input high/low voltage"), and how much current the microcontroller might take ("input leakage current").

On the ATMega328P, a low input should be less than 30% of the supply voltage, and a high input should be more than 70% of the supply voltage. In between 30% and 70%, the input might read incorrectly. The maximum current that should be able to flow into or out of the input is 1 μA.

1) While in output mode, and i pull pin high it outputs current, while it is in low can it sink in current , in other words does it act as a ground pin when it is low.

Yes, that is the entire point. Supplying or sinking current is how it outputs a voltage.

2) while in output mode does it have any internal resistor that will limit the current ?

Maybe. Probably not. There will be internal resistance, but not on purpose. You should try not to use too much current from a GPIO pin.

On the ATMega328P (link above), the "absolute maximum ratings" table specifies that the device could be damaged if a GPIO pin supplies or sinks more than 40 mA. The "DC Characteristics" table gives you some guarantees: e.g. if the current is less than 20mA and the supply voltage is 5V, the low output voltage will be less than 0.8V. Notes 3 and 4 under the DC Characteristics table give some additional limits for each group of ports, and they also say basically: "This device is capable of sourcing/sinking more current, but we don't test it, so you'll have to find out for yourself."

\$\endgroup\$
  • 7
    \$\begingroup\$ I agree with most except for your answer 2) for input mode. In theory, the pin should not sink current. In pratice there is a small leakage. ATMEL specifies it will be 1uA at most. So, the answer is rather "No, except for a very small leakage current". \$\endgroup\$ – Codo Jan 21 at 11:47
  • \$\begingroup\$ @Codo Depends on the microcontroller. In general, they can sink current. In general, you don't care whether the current is "leakage" or whether it's deliberate. \$\endgroup\$ – user253751 Jan 21 at 11:49
  • 1
    \$\begingroup\$ For an input pin, you cerainly care how much is leaks. If it leaks a lot, it will pull the voltage up or down and render the input useless. \$\endgroup\$ – Codo Jan 21 at 12:35
  • \$\begingroup\$ @Codo The same will be true if the input is designed to take that much current. That's why you don't care whether it's leakage or deliberate. \$\endgroup\$ – user253751 Jan 21 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.