Go Down

Topic: Analog Input (Read 2043 times) previous topic - next topic

Vincent19

http://arduino.cc/en/Tutorial/AnalogInputPins

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

CrossRoads

Quote
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
Code: [Select]

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

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

Quote

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Vincent19

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

CrossRoads

Quote
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.

Quote

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.

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Vincent19

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
Code: [Select]
pinMode(D14, INPUT);
digitalWrite (D14, LOW);
??

Correct ?

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Vincent19

Thanks alot. Appreciate it!

Is it ok to add :
Code: [Select]
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 ?



Grumpy_Mike

Quote
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.

Quote
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.

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.

Thanks

Grumpy_Mike


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.

Vincent19

#10
Jan 29, 2013, 06:32 am Last Edit: Jan 29, 2013, 06:35 am by Vincent19 Reason: 1
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  ?

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Vincent19

#12
Jan 29, 2013, 07:01 am Last Edit: Jan 29, 2013, 07:06 am by Vincent19 Reason: 1
if your code used
pinMode(D14, OUTPUT); to D19 (the analog pins)
you must use
Code:

Code: [Select]
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 ><

CrossRoads

"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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Vincent19

Okay. Noted ! If I want to use the analog pins as output pins, so I have to write
Code: [Select]
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 !

Go Up