[SOLVED] Using analog pin as digital pin

Hi,

I need to use an analog pin as a digital pin to control a LED (output), as all my digital pins are used up.

This is explained here, http://arduino.cc/en/Tutorial/AnalogInputPins.

The analog pins also have pullup resistors, which work identically to pullup resistors on the digital pins. They are enabled by issuing a command such as digitalWrite(A0, HIGH); // set pullup on analog pin 0
while the pin is an input. Be aware however that turning on a pullup will affect the values reported by analogRead().

Is there any problem to use this pin as an output, and not affect my analog readings?

Thanks

I don't understand your question. You mean if there is a problem if you are using one as an analog input and other as digital output?
I think that there is no problem.

I don't understand your question. You mean if there is a problem if you are using one as an analog input and other as digital output?

All my digital pins are used up, so i am hoping to use a analog pin as a digital pin to control a LED.
I have an accelerometer and other devices that use the analog pins to read their state using analogRead. It says in http://arduino.cc/en/Tutorial/AnalogInputPins that setting the pin to an input can affect values using analogRead(), so does setting it as an output also affect the analog readings?

You should take a look at these. MCP23017 They can really make life a lot easeyer for big port requirement projects.

@drksam: You miss the link, don't you?

@Jamal:I don't read that. What I read is about the same pin. If you are using 2 different pins (one to read and other to write) you shouldn't have problems.

I'm pretty sure it only affects the pin where you enable the pull-up. i.e. Enabling the pull-up on pin A1 won't affect the analog readings on A2.

If you are using a particular pin as an analog input, don't enable the pull-up on that pin.

You also don't need to enable the pull-up when using the pin as an output (such as driving an LED).

Nope just a suggestion :wink:

Thanks guys for the clarification.

@drksam: This: MCP23017 suggestion. I think that the word "these" will be a link to some page with the information about the accelerometer.

OK, then.

@Jamal: You can even use the same pin to a analog input and digital output, but then you must be careful about:

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.

Right?

Ok, thanks

If you don't have more questions about this, please rename the subject of the first post to "[SOLVED]...", to help other people in the future with the same question.

DVDdoug:
You also don't need to enable the pull-up when using the pin as an output (such as driving an LED).

That turns out to be meaningless, as the definition of an internal pull-up is nothing more nor less than to write the port HIGH whilst it is defined as an input. If defined as an output, you write it LOW or write it HIGH and it is what you write to it; if you wrote it (HIGH) as a pullup and then simply set it to be an output, it would in fact automatically be HIGH at that point.

In terms of the original question, on some processors, there are some analog pins that cannot be used for digital pins. For example, the Nano brings out pins A6/A7 that are analog input only. Similarly on the Teensy 3.1, pins A10-A14 are analog input only.

OK i see your point but its not an accelerometer. The MCP23017 http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf is a port expander that's why i thought OP may have some use for it.

drksam:
OK i see your point but its not an accelerometer. The MCP23017 http://ww1.microchip.com/downloads/en/DeviceDoc/21952b.pdf is a port expander that's why i thought OP may have some use for it.

Ho! Is this IC!!!
I'm sorry, I believe that this answer was to me (for my last comment). When I wrote:

about the accelerometer.

From the "MCP..." (I don't know why) I thought that it was an accelerometer (maybe I saw some AD accelerometer with these letters, or others that remember me these in his part number - I used in the past ST accelerometer but I don't have in the top of my head the part number for the AD brand).

Yes, I know this chip! I used it in the past (both the I2C and the SPI) to control graphic displays when the MCU don't have enough output pin.

Cheers!