Trying to test the Analog Inputs on a Nano, so thought reading the values of all 8 inputs without any termination should return something near 0, and then the inputs with INPUT_PULLUP on each Analog Input should return something close to 1023. However, INPUT_PULLUP has no effect on the value read, which is around 230 whether or not the pull-up is specified.
Do I have a misunderstanding of how INPUT_PULLUP and AnalogRead work?
An Analogue pin is designed to read in a voltage between 0 and 5v.
If nothing is connected to an ADC pin, then its called 'floating' where random values are read.
If you applied a Pull Up , which mean connecting the pin to +5v via a resistor then you get a totally false reading.
The typical way to test out an ADC port is with a 10k pot or similar, connected between 0v and +5v with the slider to the input pin, you should then see a reading between 0 and 1023 as you move the slider.
a 10k pot or similar, connected between 0v and +5v with the slider to the input pin,
How is the potentiometer with the slider all the way to +5V any different than an input with an internal pull-up to 5V? Should be supplying 5V to the input pin either way, shouldn't it?
Yes, that's what I'd expect. Since D1 drops the USB's 5V a little, reading should be a little less than 1023. With INPUT_PULLUP on some input pins, still get about 200 - 300 on those pins. Once I figure out how to post code I'll add it...
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600); // Initialize the connection to Serial Monitor
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
pinMode(A0, INPUT); // Test Input
pinMode(A1, INPUT_PULLUP); // Test Input
pinMode(A2, INPUT); // Test Input
pinMode(A3, INPUT_PULLUP); // Test Input
pinMode(A4, INPUT); // Test Input
pinMode(A5, INPUT_PULLUP); // Test Input
pinMode(A6, INPUT); // Test Input
pinMode(A7, INPUT_PULLUP); // Test Input
}
// the loop function runs over and over again forever
void loop() {
int inputRead;
int chan;
for (chan = 0; chan <= 7; chan+=1){
inputRead = analogRead(chan);
Serial.print("Input ");
Serial.print(chan);
Serial.print(" is " );
Serial.println(inputRead);
} // End For
Serial.println("====================");
} // End Loop
and the results from Serial Monitor...
Input 0 is 223
Input 1 is 1018
Input 2 is 783
Input 3 is 1019
Input 4 is 772
Input 5 is 1022
Input 6 is 766
Input 7 is 550
Without pull-ups, get this...
Input 0 is 220
Input 1 is 232
Input 2 is 231
Input 3 is 253
Input 4 is 282
Input 5 is 292
Input 6 is 289
Input 7 is 229
See my error...
was using
pinMode(0, INPUT);
instead of
pinMode(A0, INPUT);
Inputs 2, 3, 6, & 7 look odd...
A2, 4, & 6 are higher than expected 200 - 300.
A7 is lower than expected. Is something different about A7 internally?
rickso234:
Yes, that's what I'd expect. Since D1 drops the USB's 5V a little, reading should be a little less than 1023.
D1 and USB 5V are irrelevant and misleading in this context.
The supplied voltage is Vcc, be it 4.8 or 5.2v, The ADC works on Vcc **and divides it up into 1024 steps.
** correction in the case of the Atmega 328 chip it has its own internal reference, so is similarly unaffected by variations in the supply voltage.
Checking on the Analogue Pull Up , seems it is permissible, though think it would be very rare that anyone reading in an analogue voltage would want to use a pull up.
Would be interesting to know of any practical applications doing that..
If using INPUT_PULLUP on analog inputs, the equivalent resistance value is 30K-50K. However, the input impedance should be <= 10K when taking analog readings for stable and accurate results. Especially when switching to a different input, it will take time for the internal ADC capacitor charge or discharge and settle. A common solution is to add a 100nF capacitor from the analog input to GND to effectively lower the input impedance. Also, doing a double read (don't use the first) will give more accurate results.
rickso234:
Trying to test the Analog Inputs on a Nano, so thought reading the values of all 8 inputs without any termination should return something near 0, and then the inputs with INPUT_PULLUP on each Analog Input should return something close to 1023. However, INPUT_PULLUP has no effect on the value read, which is around 230 whether or not the pull-up is specified.
Do I have a misunderstanding of how INPUT_PULLUP and AnalogRead work?
I know you are trying to check the operation of your analog inputs.
You have found that the PULL-UP does not work on a pin in analog mode.
That's good, other wise the PULL-UP would distort your analog input voltage going to the ADC by applying an offset. #10 and we are still talking about something that sounds SOOOO logical to do.
As suggested the way to check your analog input is with a pot.
I can't find the reference right now, but I was under the impression that one should not call pinMode on pins that will be read with analogRead. Have I imagined that?
TomGeorge:
You have found that the PULL-UP does not work on a pin in analog mode.
Well, that's not true.
It works, but it is an approximately 30k pull-up. No more, no less. At such a relatively high impedance, it may require a capacitor to hold the value whilst analogRead samples it, such as to read a LDR.
ChrisTenone:
I can't find the reference right now, but I was under the impression that one should not call pinMode on pins that will be read with analogRead. Have I imagined that?
The only interesting situation, is if you set it as an output. I don't think analogRead alters pinMode.
ricky101:
** correction in the case of the Atmega 328 chip it has its own internal reference, so is similarly unaffected by variations in the supply voltage.
That would be 1.1V reference that is in all AVR's?