2
\$\begingroup\$

What is actually meant when someone says that a pin is digital and it cannot be used as an analog input? Are there any differences at the physical level, that is to say, the analog pin connected to an ADC while the digital pin connected to... what?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ A 'pin' is nothing - it just connects a PCB trace to a sampling cicuit on the IC. If the circuit simply compares against a threshold value, giving an above/below result (ie. 0/1), then the pin can be considered to be 'digital'. If the circuit instead distinguishes multiple input levels, the pin can be considered to be 'analog'. You can connect both types of circuit to the same pin, in which case it is analog and digital. \$\endgroup\$
    – EML
    Apr 12 '20 at 11:37
10
\$\begingroup\$

Yes, the analog pin is connected to the ADC, while the digital pin is ... not. The required connection simply does not exist.

In other words, all pins have digital input buffers; analog pins have an extra connection to the ADC.

\$\endgroup\$
2
  • \$\begingroup\$ But how is the input read on the digital pin? \$\endgroup\$
    – perencia
    Apr 12 '20 at 10:42
  • \$\begingroup\$ The pin is connected to a simple logic buffer that can only discriminate between two levels. The same thing applies to analog pins when they are not being used in analog mode. In other words, all pins have digital input buffers; analog pins have an extra connection to the ADC. \$\endgroup\$
    – Dave Tweed
    Apr 12 '20 at 10:45
8
\$\begingroup\$

Let's look at the ATmega328P datasheet commonly used in the Arduino, etc.

enter image description here

Figure 1. A GPIO. From page 59 of the datasheet.

The GPIOs are quite complex.

  1. The GPIO pin.
  2. The output buffer is used when configured as an output. It is disabled when configured as an input.
  3. The input uses a Schmitt trigger for good noise immunity. As suggested by the symbol in the buffer, the output will switch high when the input goes above the high threshold but won't switch low until the input goes below the low threshold which is considerably lower than the high. The difference is known as hysteresis.
  4. The internal pull-up resistor can be enabled or disabled by the PUD signal.

Note that this pin is digital there is no connection to the analog to digital conversion circuits. If you check on page 63 you will find a similar diagram with an extra tap-off labeled AIOxn which goes to the circuit below. GPIOs with that configuration can perform as digital input or analog input. Each of the analog pins goes to the analog multiplexor.

enter image description here

Figure 2. The ADC circuit from page 105.

  1. The analog inputs 0 to 7.
  2. The output of the multiplexor. This selects one of the inputs at a time (under program control) to be converted to digital.
  3. The signal is presented to a comparator.
  4. A 10-bit DAC generates a sawtooth ramp signal1 and when it exceeds the signal at (3) the comparator switches indicating to the conversion logic that the current count is the analog value.

1 I haven't confirmed the exact working of this so read the datasheet if you want to be sure.

\$\endgroup\$
2
  • \$\begingroup\$ Slight nitpick: It's actually a SAR ADC, not a ramp. I'd be very surprised to see a different type of ADC in any general-purpose micro. This datasheet doesn't say explicitly, but the timing diagrams give it away. Pages 209-210. \$\endgroup\$
    – AaronD
    Apr 13 '20 at 16:06
  • \$\begingroup\$ That's not a nitpick! As explained in my footnote I hadn't checked and I suspected that it might be successive approximation. What's the 'R' in 'SAR' for? \$\endgroup\$
    – Transistor
    Apr 13 '20 at 16:21

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.