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?
Let's look at the ATmega328P datasheet commonly used in the Arduino, etc.
Figure 1. A GPIO. From page 59 of the datasheet.
The GPIOs are quite complex.
- The GPIO pin.
- The output buffer is used when configured as an output. It is disabled when configured as an input.
- 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.
- 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.
Figure 2. The ADC circuit from page 105.
- The analog inputs 0 to 7.
- The output of the multiplexor. This selects one of the inputs at a time (under program control) to be converted to digital.
- The signal is presented to a comparator.
- 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.