How to toggle analog pin status between Digital Output and Analog Input?

Hi,

I've been running into a situation where an analog pin is needed to be operated as a digital output for half of a loop and as analog input for the rest...It's straightforward for the first part by using pinMode (A0, OUTPUT), but how to turn the analog pin (now as digital) back to analog input?

Tried pinMode (A0, INPUT), doesn't seem work... which makes sense, as effectively it turns the pin into a digital input pin...

I'm wondering what's the best way to turn it back to analog?

Thanks a lot in advance!

M

Maximxn:
Tried pinMode (A0, INPUT), doesn't seem work... which makes sense, as effectively it turns the pin into a digital input pin...

Actually it should work:

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

(From here, although I think they meant to write "input" there at the end, i.e. input with pullup)

I think you have to do a digitalWrite(pin, LOW) first too, otherwise the pullup resistor will cause you problems. Try this.

pinMode(pin, INPUT);
digitalWrite(pin, LOW);//turn off pullup
analogRead(pin);//discard this reading
//should be ok to make a proper analogRead now

I see! Thank you ingendel and KenF for your help! It makes a lot sense now.