pin mode number question

Hi,

This is probably a very newbie type, so forgive me if it's already asked.

I bought a board from Amazon, "Nano V3.0, Nano Board ATmega328P 5V 16M Micro-Controller Board ", from LAFVIN. When I read an analog signal from A1, I used a pin mode #1, and it works. But according to this diagram:


The pink color numbers are to be used inside the sketch code, and it should be #15. I tried #15, and it didn't work.

What I meant it works is that the voltage I measure going into it using a volt meter matches the value that I read converted back to analog (from 10 digits number).

So this is very confusing to me, and I wonder if I did something wrong. Maybe I should try again, but ask here instead because I did try many times before.

Varying the input voltage clearly shows a varying/matching reading values if I use analog read on that #1.

Thanks.

i've never seen the analog pins with those numbers. I usually see analog pins numbered: A0, A1, A2, ...

try analogRead (A1), it looks like you're referring to ADC1

Thanks, I checked and A2 is defined as 16. So my question remained the same.

and it should be #15. I tried #15, and it didn't work.

Using 15 will only work if you are using an analogue pin as a digital one. A1 is in fact a short hand for 15 you can use either if you want to run this as a digital pin.

The numbers 14 thru 19 can be used if using for digital I/O, for analog input use A0 thru A5 (or just 0 ~ 5);

analogRead(A1);
analogRead(1);
digitalRead(15);
digitalRead(A1);

14, 15, 16, 17, 18, 19.
A0, A1, A2, A3, A4, A5.
PC0, PC1,.......PC5.

Using 1 will also work with analogRead(), because analogRead makes the assumption that you are referring to an analog pin, and not a digital pin.

You mentioned pinMode(), that should not be necessary because A1 is set to input by default, but you must use A1 or 15 for pinMode(), using 1 will set the mode of digital pin 1.

nam20nguyen:
When I read an analog signal from A1, I used a pin mode #1, and it works.

The compiler is clever enough to use pin A1 instead of pin 1 when you ask for an analogRead.
But when you ask for a digitalRead/write, it will try to use the TX pin instead.

14-19 works on an Uno/Nano, but not on a Mega.
Best to use the right name, to stop confusion (also for the reader of the sketch).

const byte sensorPin = A1;

Leo..

Grumpy_Mike:
Using 15 will only work if you are using an analogue pin as a digital one. A1 is in fact a short hand for 15 you can use either if you want to run this as a digital pin.

Wow, thank you, that completely explains it. Is there a document somewhere that I missed? I jumped into it too fast to start out with a book or a guide. Maybe that's why.

As you can see the diagram, too many numbers and things and I read the specs and no mention of sketch pin number to use.

JCA34F:
The numbers 14 thru 19 can be used if using for digital I/O, for analog input use A0 thru A5 (or just 0 ~ 5);

analogRead(A1);

analogRead(1);
digitalRead(15);
digitalRead(A1);

Isn't analogRead(A1) the same as analogRead(15) and will cause error?
Or is there some magic translation between analogRead(A1) and analogRead(1)?

I did Serial.println(A1) and it shows 15.

I learned a bit, but so confused now. I haven't programmed in C for a long time, so I don't know how this works. I thought A1 is 15, that's it, but maybe it's not the case.

If analogRead(15) automatically converts to analogRead(1), then it doesn't work, since I tried that.
Not to be argumentative. I am abit at lost here.

Then the above comment about pinMode, appears to say pinMode of #1 is not for A1, but for 15 and for digital, even more confusing to me.

Maybe it's time to point me to a complete article/guide/document about this? I need to reset my knowledge about this.

Thanks.

I was so intrigued by this new information, I did some search and I found these two links:

int analogPin = A3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}

and this:

int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}

It seems to me passing either 3 or 17 to analogRead() would give the same value. The code may check and subtract 14 as needed. Would that also work with pinMode?


So it appears for analog read/write, pin mode is not used at all.

The whole confusing dilemma is the result of this line in wiring_analog.c:

if (pin >= 14) pin -= 14; // allow for channel or pin numbers

Of course, it can't work with pinMode(). Then which pin would pinMode(14, INPUT) refer to? 0, or 14?

So it appears for analog read/write, pin mode is not used at all.

Correct, except you can use pinMode to set the pull up resistor if you want it to be enabled when you do an analogue read.

If analogRead(15) automatically converts to analogRead(1), then it doesn't work, since I tried that.

That is because it doesn’t do that, for an analogue read you can use either A1 or 1 and the compiler sorts it out. For a digitalRead you can use either A1 or 15 and the compiler sorts it out.

I haven't programmed in C for a long time, so I don't know how this works.

This is not a C thing, it is just using functions that the Arduino IDE automatically loads in for you. This code is contained in the digitalRead, analogRead, and so on functions that you use. This is using functions to make things easier for a beginner. There is a lot of effort into making this pin numbering abstraction consistent across different types of models of Arduino.

The use of these functions leads some people to think the Arduino uses its own special language rather than C. Think of it as having a library permanently loaded in. Read the reference page that you can access from the help menu in the Arduino IDE.

1. Given the following legends on the PCB of NANO:

RX0, TX1, D2, ..., D13, A0, ..., A7

2. Legends of Step-1 are equivalent to the following while executing digitalRead()/digitalWrite() instructions in association with pinMode():

D0, D1, D2, ..., D13, A0, ..., A5
==> 0, 1, ..., 13, 14, ..., 19

3. The execution of this code: pinMode(AX/X, OUTPUT); allows to use the legends of Step-2 as the arguments in the following commands:

digitalWrite(AX, value); //AX = A0, ..., A5 (PC0-PC5); DPins: 14 - 19 value = HIGH or LOW
digitalWrite(X, value); //X = 0, 1, ..., 13, 14, ..., 19 //DPins: 0 - 19; PD0-PD7, PB0-PB5, PC0-PC5

4. The execution of this code: pinMode(AX/X, INPUT); allows to use the legends of Step-2 as the arguments in the following commands:

bool n = digitalRead(AX); //AX = A0, ..., A5 (PC0-PC5); DPins: 14 - 19
bool n = digitalRead(X);  //X = 0, 1, ..., 13, 14, ..., 19; DPins: 0 to 19 : PD0 - PD7, PB0-PB5, PC0-PC5

5. When using ananlogRead(AX/X) command, the following codes are valid:

int x = ananlogRead(AX);   //AX = A0, ..., A7; ADC channels: Ch0 - Ch7 (PC0 - PC5)
int x = analogRead(X);   //X = 0, ..., 7; ADC channels: Ch0 - Ch7

6. While performing analogWrite() command, the following instruction is valid to produce pre-defined PWM signals at the pre-defined DPins (Fig-1):

ananlogWrite(X, value);   //X = 3,11, 10,9, 5,6; value = 0 - 255

pwm328Nano.png
Figure-1:

Note: 'bool n = digitalRead(0);' and 'int x = analogRead(0);' are not the same though the numerical values of the arguments are apparently equal. From the function name, the compiler unambiguously understands what is what -- former one is 1-bit read operation from a digital port; the later one is the read operation of an analog signal from ADC channel number 0 (Ch0/ADC0).

pwm328Nano.png

Thank you all for taking time explaining to me in details how this all work. Those are very helpful.