Arduino Forum

Using Arduino => Programming Questions => Topic started by: lamachine on Sep 13, 2019, 11:09 pm

Title: Analog pin numbers don't match standard pinout Mega2560
Post by: lamachine on Sep 13, 2019, 11:09 pm
I am trying to read and print all of the analog inputs to serial.  I struggled with using a variable for the number following analogRead(A(x)) so went to the pin numbers for reading.  The pinout map said A0 corresponds to 97 and A15 to pin 82. 
When I put ground on a particular pin, it does not appear in the place I expect...

This was with ground connected to A15, but the zero shows up in analogRead(85) position.  If I move it to pin zero, it shows up in analogRead(86)

97=249, 96=278, 95=284, 94=259, 93=261, 92=270, 91=256, 90=254, 89=249, 88=241, 87=243, 86=240, 85=0, 84=121, 83=229, 82=240,


Code:
// analog pins are A0 = 97 to A15 = 82

void setup(){
    Serial.begin(9600);  // set up serial
}

void loop(){
 
    for (byte j = 97; j > 81; j = j - 1){
            Serial.print(j);
            Serial.print('=');
            Serial.print(analogRead(j));
            Serial.print(", ");
            delay(1000);
            }
            Serial.println();
   
} // void loop
Title: Re: Analog pin numbers don't match standard pinout Mega2560
Post by: blh64 on Sep 13, 2019, 11:36 pm

The A0...Ax notation are just constants defined in one of the standard header files
...\mega\pins_arduino.h

Code: [Select]

#define PIN_A0   (54)
#define PIN_A1   (55)
#define PIN_A2   (56)
#define PIN_A3   (57)
#define PIN_A4   (58)
#define PIN_A5   (59)
#define PIN_A6   (60)
#define PIN_A7   (61)
#define PIN_A8   (62)
#define PIN_A9   (63)
#define PIN_A10  (64)
#define PIN_A11  (65)
#define PIN_A12  (66)
#define PIN_A13  (67)
#define PIN_A14  (68)
#define PIN_A15  (69)
Title: Re: Analog pin numbers don't match standard pinout Mega2560
Post by: Wawa on Sep 13, 2019, 11:38 pm
I am trying to read and print all of the analog inputs to serial.  I struggled with using a variable for the number following analogRead(A(x)) so went to the pin numbers for reading.
No need to put 'A' with the pin number.

analogRead(0); // the compiler knows to use A0.


This works
Code: [Select]

  for (byte j = 0; j <= 15; j++) {

    Serial.print(analogRead(j));

Leo..
Title: Re: Analog pin numbers don't match standard pinout Mega2560
Post by: lamachine on Sep 14, 2019, 12:05 am
Solved, thanks Leo!  -- Bob O
Title: Re: Analog pin numbers don't match standard pinout Mega2560
Post by: johnwasser on Sep 14, 2019, 12:54 am
The pinout map said A0 corresponds to 97 and A15 to pin 82. 

When I put ground on a particular pin, it does not appear in the place I expect...
WARNING: The Arduino IDE does not use chip pin numbers because different chip packages put the functions on different pins.  The TQFP package ATmega2560 has ADC0 through ADC15 on pins 96 through 82 (PORTF bit 0 through 7 and PORTK bit 0 through 7).  On the CBGA package those are A3, B3, A4, B4, C4, A5, B5, C5, A6, B6, C6, A7, B7, C7, A8, and B8 (the letter is the row and the number is the column). 
Arduino pin numbers are arbitrary and mapped to a bit in a port.  On the Arduino MEGA 2560 the A0 through A15 pins are given numbers 54 through 69.  You can write your loop:
Code: [Select]
   for (byte pin = A0; pin <= A15; pin++) {
           Serial.print('A');
           Serial.print(pin - A0);


This will display the pin names (A0-A15) instead of the arbitrary pin number (54-69).
Title: Re: Analog pin numbers don't match standard pinout Mega2560
Post by: david_2018 on Sep 14, 2019, 04:07 am
A more generic method that should work regardless of which arduino board is used, or the actual pin numbering (whether it be ascending, descending, or non-sequential):

Code: [Select]

const byte analogPins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15};

for (byte i = 0; i < sizeof(analogPins) / sizeof(analogPins[0]); i++) {
  Serial.print("Analog Pin #");
  Serial.print(i);
  Serial.print(" = ");
  Serial.println(analogRead(analogPins[i]));
}

Title: Re: Analog pin numbers don't match standard pinout Mega2560
Post by: johnwasser on Sep 14, 2019, 03:39 pm
Quote from: david_2018 (http://"https://forum.arduino.cc/index.php?msg=4306160")A more generic method that should work regardless of which arduino board is used, or the actual pin numbering (whether it be ascending, descending, or non-sequential):
Are there any Arduinos where the analog inputs are not sequential and/or not ascending?
Given that the pins_arduino.h file defines NUM_ANALOG_INPUTS, the most generic method would be:
Code: [Select]
void printAllAnalog()
{
  for (byte i = 0; i < NUM_ANALOG_INPUTS; i++) 
  {
    Serial.print('A');
    Serial.print(i);
    Serial.print('=');
    Serial.print(analogRead(A0+i));
    Serial.print(' ');
  }
  Serial.println();
}


void setup()
{
  Serial.begin(115200);
  while (!Serial);
}


void loop()
{
    printAllAnalog();
    delay(1000);
}



This will show 6 values on the UNO, 8 on the Nano, and 16 on the MEGA.
Title: Re: Analog pin numbers don't match standard pinout Mega2560
Post by: slipstick on Sep 14, 2019, 03:50 pm
No need to put 'A' with the pin number.

analogRead(0); // the compiler knows to use A0.
I realise that works but I don't like people doing it. For a newcomer to Arduino it's really confusing when a number like 0 or 4 may refer to different things depending on how it's used. When someone says "Connect to pin 2" there's only one 2 marked on the board but that may not be the one they mean. Nasty.

Steve