Analog Input

For this tutorial,
Can anyone explain to me about this ?
Details and Caveats

The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pullup resistor will be set, when switched back to an input.

The Atmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

I do not understand it.

Thanks

The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pullup resistor will be set, when switched back to an input.

if your code used
pinMode(D14, OUTPUT); to D19 (the analog pins)
you must use

pinMode(D14, INPUT);
digitalWrite (D14, LOW);

to make A0 thru A5 inputs for analogRead to work correctly.

The Atmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

Make two readings on an analog input, and ignore the first one.
The signal needs some time to settle at the sample & hold section before it is converted.

So, it means that I have to add the code in the void setup every time if I am dealing with analog pins ?

Why do it need to read twice ?

Thanks

So, it means that I have to add the code in the void setup every time if I am dealing with analog pins ?

No, the pins are type INPUT be default.
Your question had to do with using them as analog inputs after they had been setup as digital outpouts.

Why do it need to read twice ?

The signal needs some time to settle at the sample & hold section before it is converted.
There is an analog mux feeding the single sample & hold and ADC.
Experience has shown that taking two readings and using the second when switching between sources is needed for valid results, especially with large changes in signal levels.

Okay. So it means if I used it to be digital I/O, then later on I want to use it as analog input, then I have to add

pinMode(D14, INPUT);
digitalWrite (D14, LOW);

??

Correct ?

Yes.

Thanks alot. Appreciate it!

Is it ok to add :

pinMode(D14, INPUT);
digitalWrite (D14, LOW);

everytime when I am using analog pins ?
This is because sometimes I am not sure about the previous use of the analog pin. Or any ways I can check it ?

What do you mean analogRead working not correctly ? It's reading is inaccurate ?

This is because sometimes I am not sure about the previous use of the analog pin.

Why are you not sure, just look at the sketch and see. This does not mean in a previous sketch just the one you are running now.

It's reading is inaccurate ?

If you have a high input impedance source feeding the analogue input you "may" need to read twice. By high impedance we mean anything over about 100K, yes under these circumstances the reading will be either inaccurate or influenced by voltages on other analogue pins you have recently read.

Just sometimes I may not be using the board for too long time. So I have forgotten what is the sketch that I had uploaded previously.

Thanks

Vincent19:
Just sometimes I may not be using the board for too long time. So I have forgotten what is the sketch that I had uploaded previously.

I don't see what difference that makes when you put in a new sketch the arduino resets, once it is reset all pins are set by default as inputs. It doesn't matter about a previous sketch only the sketch you are using. I can't think of why the same pin would be used as a digital output and an analogue input in the same sketch, that is what is being talked about, nothing to do with previous sketches.

Oh..I get what you means. The arduino will be reset everytime a new sketch is uploaded. So if in previous sketch I am using analog pin as digital input/output pin, do I still need to define the analog pin to be input and digitalWrite the pin as Low if I uploaded another new sketch ?

No. After a reset, or a power loss, the sketch starts new. If your sketch never used the pins as digital outputs, you can just use them as analog inputs.

if your code used
pinMode(D14, OUTPUT); to D19 (the analog pins)
you must use
Code:

pinMode(D14, [b]INPUT[/b]);
digitalWrite (D14, LOW);

to make A0 thru A5 inputs for analogRead to work correctly.

Then I am confuse for this again. If the new sketch starts, so the analog pins will be back to default which is as input pins. So why should I add this anymore ? Getting real confuse now ><

"If the new sketch starts, so the analog pins will be back to default which is as input pins."
Yes. Stop there. Nothing else is needed.

Okay. Noted ! If I want to use the analog pins as output pins, so I have to write

digitalWrite(A0,OUTPUT )

If I were to use it as input pins, however I do not need to define it as its default is input.

Thanks !