Pages: [1]   Go Down
Author Topic: Analog Input  (Read 1649 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Correct ?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks alot. Appreciate it!

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


Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34579
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34579
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  ?
« Last Edit: January 29, 2013, 12:35:39 am by Vincent19 » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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 ><
« Last Edit: January 29, 2013, 01:06:17 am by Vincent19 » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 27372
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 338
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: