Should be a simple question but after some searching, it seems unanswered. I have the Mega 2560, when I do an analogRead or anything else of pin 4, how does the program know to use analog 4 instead of the PWM 4? It has never gotten it wrong, just has me wondering.
Because analogRead() can only be done on the analog inputs and the IDE has descriptions of every Arduino how the numbers on the PCB correspond to the pins of the chip.
Specifically, pins_arduinio.h tells the software what physical pin goes with each pin as referenced by the software.
There are several arrays:
for position 0 in the array (which is labelled D0 on the board), one array defines the port it is in (A-L say)
one array defines the pin in the array (0-7),
one array defines whether the pin has a timer associated with it (for PWM outputs),
and there may another array, or just a line of additional code, that ties in the A0-A15 assignments also.
The array for a Mega I think is 54 positions, but could be expanded to cover all 84 or 86 actual IO pins.