Basic Questions re using Arduino pins

(I'm not sure if this is the correct forum for these questions, but it seemed closer than any of the other choices.)

Two pretty basic questions:

  1. I understand that digital pins 0 and 1 (on the Uno) are used for serial communication. Does this mean that those pins simply cannot be used for other I/O purposes? If the sketch does not open the serial monitor, can I use those pins for other purposes, or will it still cause a conflict when I upload the sketch itself through the USB cable? If the pins can be used for other purposes, do I have to use any special code to do so, or do I just refer to them like any other pin?

  2. I have read in numerous places that the analog pins can be used for digital input or output, but I haven't found anything that tells me how to do so. Is any special code necessary to configure the pins for digital I/O or can I just do a digitalRead or digitalWrite which refers to (for example) pin A1?

Thanks.

Rob Rothman

I'll answer (2).... yep, you just use normal digital commands and address the pins as Ax. You can, I believe, also number them from 14 up, since the last pin on the opposite side was 13 where the LED is.

You can, I believe, also number them from 14 up, since the last pin on the opposite side was 13 where the LED is.

Well, not on a Mega. That's why there are aliases defined (A0, A1, etc.)

If the sketch does not open the serial monitor

That's always the case. The sketch can't open an application on the PC. The user of the PC has to do that.

can I use those pins for other purposes

If you do not use the serial port (not monitor), then the pins can be used for other purposes.

do I have to use any special code to do so, or do I just refer to them like any other pin?

No. They are just regular pins.

will it still cause a conflict when I upload the sketch itself through the USB cable?

If there is hardware attached to the pins, while a sketch is being uploaded, the hardware can interfere with the communications.

Until you understand all the complexities of using RX and TX pins, best avoid them.

Some of this is board-dependent: They may have pull-ups attached. They have a USB-serial device attached via resistors. During boot/reset the lines are sensed by the bootloader to determine whether to download a new sketch. The bootloader may leave the TX pin as an OUTPUT. Serial.begin()/end() may change the configuration of the pins - On the Due they become detached from normal I/O functions.

Any other hardware on them will potentially interfere with ability to bootload, bootloading and serial I/O will interfere with the other hardware...

Anyway generally this means you have to decouple any extra hardware(*) from them at reset time, which can be tricky and just not worth it.

(*) The exception is when routing TX to an extra serial serial terminal such as a serial enabled LCD display - normally this works but you get garbled text when uploading a new sketch, which you can live with.

Sorry, but I'm a little confused as to the answers re digital pins 0 and 1. In order to avoid a problem, do I need only disconnect the other hardware from those pins when I upload a sketch (which is not a problem), or would it be necessary to disconnect the other hardware every time I power up or reset (which would not be practical)?

I think I may have enough pins (including using some analog pins for digital I/O) without the need for using digital 0 or 1, but it would generally complicate the design (e.g., a group of wires coming from the same I/O device would have to be split between the two sides of the Arduino board, so I couldn't use the same connector for all of them). It's not a major problem, but is a little inconvenient and makes it more likely that something will get plugged into the wrong pin. Bottom line, if there is a relatively simple way to use pins 0 and 1, it would simplify things (and being a simple person, I like simplicity). :~

Thanks.

Rob Rothman

RobRothman: In order to avoid a problem, do I need only disconnect the other hardware from those pins when I upload a sketch

Yes

RobRothman: (I'm not sure if this is the correct forum for these questions, but it seemed closer than any of the other choices.)

  1. I have read in numerous places that the analog pins can be used for digital input or output, but I haven't found anything that tells me how to do so. Is any special code necessary to configure the pins for digital I/O or can I just do a digitalRead or digitalWrite which refers to (for example) pin A1?

On Arduinos using the ATmega328 (or 168) chip the analog inputs A0..A5 are perfectly valid as digital pins too. Some boards though (not the Uno) bring out A6 and A7 as well - these two are analog inputs only, are only present on the surface mount ATmega328 and cannot be used as digital pins - they are inputs that only connect to the ADC multiplexer.

On the Mega all analog input pins are digital too.

On the Due the analog input pins are digital too.

As a follow-up to this thread, I wanted to report that I attempted to use pin 0, disconnecting everything from the Arduino when I uploaded the sketch, then disconnecting the USB line and reconnecting the other hardware after uploading and before running the sketch. The sketch does not otherwise use any serial communication.

I wanted to use pin 0 to read a NO pushbutton switch. One side of the switch was connected to 5V power, the other side to pin 0 and also to ground through a 10k pulldown resistor. The Arduino kept reading pin 0 as HIGH even when the button was not pushed. :~

After confirming that the switch was not defective, I then tried to do the same thing with one of the other pins -- it worked just fine (at the cost of a modicum of inconvenience in running wires to the Arduino from other components).

I guess I've learned my lesson: The Uno doesn't really have 20 usable I/O pins, it only has 18.

Rob Rothman

Can we use digital I/O pins as analog ?

Arman5592: Can we use digital I/O pins as analog ?

No.

RobRothman: I wanted to use pin 0 to read a NO pushbutton switch. One side of the switch was connected to 5V power, the other side to pin 0 and also to ground through a 10k pulldown resistor. The Arduino kept reading pin 0 as HIGH even when the button was not pushed.

Did you try wiring the switch the other way - so it pulls the pin to GND when the switch is pressed and uses the internal resistor pinMode(0, INPUT_PULLUP) to hold the pin HIGH ?

...R

Reply #7:

I guess I've learned my lesson: The Uno doesn't really have 20 usable I/O pins, it only has 18.

Wrong.

Pin pulled high from USB/Serial chip - 1K vs 10K to GND.

Wire the switch to connect to Gnd when pressed. Will properly read as LOW, and HIGH otherwise. Can be used outputs also, just with slightly less drive as the 1K pullup has to be overcome.