PinMode on 1.8.1. Using an alalog pin.

Has the definition of analog pins changed?

pinMode(A11,INPUT);

Used to compile now it doesn't.

Ignore this. My brain was in neutral. Forgot to reset the board type! (I thought the project file would set this?)

richardtheboffin:
I thought the project file would set this?

Nope, because it's not a project file :wink: It's just the plain sketch :slight_smile:

And if you want to use it as a analog in there is no need for setting the mode.

if you want to use it as a analog in there is no need for setting the mode.

But explicitly setting the mode does reinforce what the code is actually supposed to do when you, or someone else, reads it. Even better, give the pin a meaningful name and use it in the pinMode() and read() functions.

But explicitly setting the mode does reinforce what the code is actually supposed to do when you, or someone else, reads it.

No, it doesn't. It says that you are clueless, setting the nature of the digital side of the pin when you are using the pin as an analog pin.

PaulS:
No, it doesn't. It says that you are clueless, setting the nature of the digital side of the pin when you are using the pin as an analog pin.

But maybe OP wants to use it as a digital pin :wink:

PaulS:
No, it doesn't. It says that you are clueless, setting the nature of the digital side of the pin when you are using the pin as an analog pin.

Are you saying that setting an analogue pin as INPUT using setMode() somehow prevents it being used as an analogue input ?

UKHeliBob:
Are you saying that setting an analogue pin as INPUT using setMode() somehow prevents it being used as an analogue input ?

No. I am saying that pinMode() is useless for analog pins. Calling the function on a pin that has digital and analog capabilities affects only the digital nature of the pin. If the pin has an analog sensor connected to it, and is read using analogRead(), then the pinMode() call did nothing useful, and does NOT indicate that you know what you are doing, or what pinMode() is doing.

Digital pins, including the A* ones, default to INPUT. Would you advocate not bothering with pinMode() for them too when they are used as inputs, use of INPUT_PULLUP aside, of course.

UKHeliBob:
Digital pins, including the A* ones, default to INPUT. Would you advocate not bothering with pinMode() for them too when they are used as inputs, use of INPUT_PULLUP aside, of course.

No. I always use pinMode() when I am using the pin as a digital pin.

PaulS:
No. I always use pinMode() when I am using the pin as a digital pin.

Why ?

UKHeliBob:
Why ?

Habit, I guess. I just like to make sure that I set the output pins as output. The input pins usually have switches attached, and I need to set the mode to INPUT_PULLUP.

Habit, I guess

Well, one of my habits is to use pinMode() on all pins that will be used as inputs or outputs. I find that it helps my thought processes when getting down to writing programs as does appropriate variable names.

I think that in practice we are not too far apart in our habits.

UKHeliBob:
I think that in practice we are not too far apart in our habits.

Except when it comes to using a pin that can be used as an analog pin or a digital pin. It does nothing to the analog nature of the pin to call pinMode(). When you are using the pin as an analog pin, diddling with the digital nature of the pin makes no sense. In fact, it indicates to me that you don't understand the dual nature of some pins, or what pinMode() does, and does not do.

PaulS:
It does nothing to the analog nature of the pin to call pinMode().

pinMode(Ax, INPUT_PULLUP) has an effect on the reading (on a Mega with nothing attached to A0 or A1).

void setup() {
  Serial.begin(250000);
  pinMode(A0, INPUT_PULLUP);
}
void loop() {
  static unsigned long lastTime;
  unsigned long topLoop = millis();
  if (topLoop-lastTime> 1000) {
    lastTime = topLoop;
    Serial.print(F("PUP "));
    Serial.print(analogRead(A0));
    Serial.print(F(" STD "));
    Serial.println(analogRead(A1));
  }
}
PUP 1019 STD 846
PUP 1020 STD 850
PUP 1019 STD 853
PUP 1020 STD 855
PUP 1020 STD 855
PUP 1020 STD 856
PUP 1018 STD 855
PUP 1020 STD 856
PUP 1020 STD 856
PUP 1020 STD 856
PUP 1019 STD 856
PUP 1019 STD 856
PUP 1020 STD 857
PUP 1020 STD 857
PUP 1019 STD 857
PUP 1019 STD 857
PUP 1019 STD 856
PUP 1019 STD 856
PUP 1019 STD 857

What is the output of the code without the pinMode() statement?

void setup() {
  Serial.begin(250000);
  //pinMode(A1, INPUT_PULLUP);
}
void loop() {
  static unsigned long lastTime;
  unsigned long topLoop = millis();
  if (topLoop-lastTime> 1000) {
    lastTime = topLoop;
    Serial.print(F("STD "));
    Serial.print(analogRead(A0));
    Serial.print(F(" STD "));
    Serial.println(analogRead(A1));
  }
}
STD 644 STD 636
STD 538 STD 549
STD 505 STD 510
STD 483 STD 486
STD 468 STD 469
STD 458 STD 458
STD 451 STD 451

In this test one of 4 Ax gets an INPUT_PULLUP

void setup() {
  Serial.begin(250000);
}
void loop() {
  static unsigned long lastTime;
  static byte pullUp = A0;
  unsigned long topLoop = millis();
  if (topLoop - lastTime > 1000) {
    lastTime = topLoop;
    pinMode(pullUp, INPUT_PULLUP);
    Serial.print(F("PullUp on A"));
    Serial.print(pullUp - A0);
    Serial.print(F(":  A0 "));
    Serial.print(analogRead(A0));
    Serial.print(F("\tA1 "));
    Serial.print(analogRead(A1));
    Serial.print(F("\tA2 "));
    Serial.print(analogRead(A2));
    Serial.print(F("\tA3 "));
    Serial.print(analogRead(A3));
    Serial.println();
    pinMode(pullUp, INPUT);
    if (++pullUp > A3) {
      pullUp = A0;
    }
  }
}
PullUp on A0:  A0 1018 A1 831 A2 691 A3 618
PullUp on A1:  A0 580 A1 1019 A2 814 A3 692
PullUp on A2:  A0 551 A1 614 A2 1019 A3 809
PullUp on A3:  A0 785 A1 707 A2 698 A3 1020
PullUp on A0:  A0 1019 A1 843 A2 731 A3 720
PullUp on A1:  A0 646 A1 1018 A2 828 A3 741
PullUp on A2:  A0 593 A1 640 A2 1019 A3 832
PullUp on A3:  A0 796 A1 716 A2 705 A3 1020
PullUp on A0:  A0 1019 A1 845 A2 735 A3 725
PullUp on A1:  A0 652 A1 1019 A2 831 A3 743
PullUp on A2:  A0 597 A1 643 A2 1019 A3 833
PullUp on A3:  A0 796 A1 716 A2 705 A3 1020
PullUp on A0:  A0 1019 A1 844 A2 733 A3 723
PullUp on A1:  A0 650 A1 1019 A2 830 A3 743
PullUp on A2:  A0 594 A1 640 A2 1019 A3 832
PullUp on A3:  A0 796 A1 716 A2 705 A3 1020
PullUp on A0:  A0 1020 A1 845 A2 734 A3 724
PullUp on A1:  A0 651 A1 1019 A2 830 A3 744
PullUp on A2:  A0 595 A1 641 A2 1019 A3 833

pinMode(Ax, INPUT_PULLUP) has an effect on the reading (on a Mega with nothing attached to A0 or A1).

Who suggested using INPUT_PULLUP on an analogue pin was acceptable ? The discussion is about using INPUT.

PaulS:
No. I am saying that pinMode() is useless for analog pins.
Calling the function on a pin that has digital and analog capabilities affects only the digital nature of the pin.
If the pin has an analog sensor connected to it, and is read using analogRead(), then the pinMode() call did nothing useful, and does NOT indicate that you know what you are doing, or what pinMode() is doing.

I can not see any INPUT_PULLUP restriction in those claims.