Alternative use of pins

I can't find any clear information at all on whether it is possible to use special pins like those for serial communication and analog input for "normal" digital I/O.

For example, on Serial - Arduino Reference it reads:

All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.

That vaugely implies that maybe I can use those two pins for digital I/O if I don't need them for serial communication, if I dare to read the unwritten information between the lines.

Same goes with the analog input pins. It seems I can assign the as digital inputs, even though they are presented as analog, all I have to do is define their tasks in the sketch. It looks as if I even can make them digital outputs, in spite of the fact that they are labelled analog inputs!

But the information on alternative use of pins is hard to find, if there even is any out there(?)

The analog pins can certainly be used as digital pins.

You can use the serial pins too except that they have pull-ups connected internally. It would be OK to use them as outputs, not quite as useful to use them as inputs.

Nick, when you say they have internal pullups on them, couldn't that be disabled? Or does the arduino code keep re-enabling that (or are we talking about the 32u4 connected to them?) If serial is never invoked in your code, I don't understand why the pins would be pulled up. But, wouldn't simply setting them as inputs and writing zero to them disable the pullups?

I'm not sure about the TxD pin (pin D1) but the RxD pin (pin D0) would be kept high by the Atmega8U2 chip via a 1K series resistor, because the 8U2 is trying to send to the main processor what it is receiving from the USB port, which, if nothing, will be a HIGH state.

As a test I uploaded this sketch to my Uno:

void setup () { }
void loop () { }

I measured 5V on both D0 and D1, and 0.5V on D2 onwards.

Checking for current I got 5 mA on D0 which would be consistent with a 1K resistor. On D1, I got 0.15 mA which is consistent with a 33K resistor, which the 8U2 chip may well use (as an internal pull-up) to make sure it doesn't interpret high impedance data as random rubbish.

Neither of those can be changed by programming the Atmega328P chip because they are external to it.

Setting the pins to OUTPUT however would override the pull-ups. Then you could drive them LOW or HIGH at your leisure.

SimLego:
Same goes with the analog input pins. It seems I can assign the as digital inputs, even though they are presented as analog, all I have to do is define their tasks in the sketch. It looks as if I even can make them digital outputs, in spite of the fact that they are labelled analog inputs!

On the Uno, all 6 of the analog input pins can be used for digital output. However, on some of the non-official Arduino machines that are programmed with the Arduino IDE that have more analog input pins, those extra pins cannot be used for digital output.

Michael, are you positive about that? I ask because I have made more than a couple "non-official" arduinos for my own purposes and as long as I have the proper variants and cores for them (in my case I had to make some of them), I have no trouble using the extra analog pins for digital input. The only reaosn that I could see not being able to is if you did not have the right pins_arduino.h for your variant. And if you didn't have that, you couldn't use the extra analog pins as analog pins either. In fact, you couldn't use ANY of the extra pins at all.

Retroplayer:
Michael, are you positive about that? I ask because I have made more than a couple "non-official" arduinos for my own purposes and as long as I have the proper variants and cores for them (in my case I had to make some of them), I have no trouble using the extra analog pins for digital input. The only reaosn that I could see not being able to is if you did not have the right pins_arduino.h for your variant. And if you didn't have that, you couldn't use the extra analog pins as analog pins either. In fact, you couldn't use ANY of the extra pins at all.

No.
Some non official arduino's use a diffrent processor. It is that that determins if you can use some analog pins as digital ones. This also applies to some official arduino boards that do not use the AT328 processor.

However, on some of the non-official Arduino machines that are programmed with the Arduino IDE that have more analog input pins, those extra pins cannot be used for digital output.

...or, indeed, as digital inputs.
The Nano is an "official" board with A6 and A7 brought out to pins. These two are connected only to the analogue input mux, and can be used only as analogue inputs.

My "non-official" variants are using other processors (Attiny2313, Atmega1284, ATmega32, etc..) Unless we are talking about non-AVRs, I don't get it. All that is defined right in pins_arduino.h for your variant. And if it were necessary, you make a different core for your variant.

Do you have an example? I am just perplexed by this. It isn't a limitation of any AVR that I am aware of and as I said, the pin mapping is all in the variants files so I don't think it is a limitation in Arduino either (at least not in 1.0.3 which is what I started with.)

{edit} Oh. Thanks for the example!

Retroplayer:
Nick, when you say they have internal pullups on them, couldn't that be disabled?

Another approach would be to use the pins as inputs in situations where you want the internal pullup anyway (eg. to a switch which has the other side wired to ground). In that case the pull-up voltage won't worry you.

Retroplayer:
My "non-official" variants are using other processors (Attiny2313, Atmega1284, ATmega32, etc..)

My reply specifically addressed the Uno (and similar variants, like the Duemilanove) not all the boards with different processors in them. The original question needs to be clarified if you are asking about Leonardos, Megas, and so on.

Yep. I was completely unaware of the difference in the LQPF version of the ATmega328. Learn something new every day!

AWOL:
..or, indeed, as digital inputs.
The Nano is an "official" board with A6 and A7 brought out to pins. These two are connected only to the analogue input mux, and can be used only as analogue inputs.

Thanks for the example. I couldn't remember which processors had the A6 and A7 that could not be used for digital I/O, but I knew I had seen in in following various posts.

As I best recall The Uno SMD board also used the LQPF version of the ATmega328, but not sure they brought out the two extra analog only pins to extra pads or pins. Can't find a link to the SMD schematic on the product page.

Lefty

retrolefty:
As I best recall The Uno SMD board also used the LQPF version of the ATmega328, but not sure they brought out the two extra analog only pins to extra pads or pins. Can't find a link to the SMD schematic on the product page.

Lefty

I've a SMD Uno. There are no extra pins, but there appears to be 2 unconnected pads by the chip. From the dot, anti-clockwise, they are pads 3 and 5.

According to the Uno R3 schematic, there are no pullup resistors on pins 0 and 1. It is normal for TTL serial signals to idle in a high state (+5V). The 5mA current limit that Nick sees on one pin is because there are 1k series resistors in the TX and RX lines leading from the mega16u to the mega328p. The other smaller current limit is probably the internal pullup on the RX line of the mega328. While the mega16u would be continually trying to drive the pin high as Nick says, I believe that the mega328 pin could easily override the 5mA driving force when in output state.

Thanks for this info. I have a project on an Arduino Pro Mini where I was 2 pins short (everyone appreciates an audible alarm and then a button to silence/acknowledge it!).
Now I will repurpose all 6 of the analog inputs as digital inputs and leave pins 1 & 2 available for serial communications in the future (Should I need it). That's puts me up 2 pins now!

Peter