Can i use pin RX TX on Arduino Uno as reguler pin ? . For input and output pin , because two digital pin on my board doesn’t work . Thank’s
-
can you be a bit more specific? do you mean pins 0 and 1? What can't digital pin 2 do?Michele Cioccarelli– Michele Cioccarelli2020年06月17日 18:30:33 +00:00Commented Jun 17, 2020 at 18:30
2 Answers 2
Yes, you can use those pins like regular digital pins on the Uno. They are numbered 0 and 1. That is... provided you do not try to use Serial
.
-
2and the component wired to this pins can interfere with sketch upload. and if one of this two stops working then no serial upload will be possible.2020年06月17日 19:08:40 +00:00Commented Jun 17, 2020 at 19:08
TX can be used as a general input or output
The TX pin is normally an output from the Arduino microcontroller to a serial converter (RS-232 voltage converter or a USB-to-serial converter) that sends data to the host computer. In this usage, the only thing driving the pin is the serial hardware inside the Arduino microcontroller.
However, it is possible to re-configure the pin as a general-purpose digital output. You will first need to disable the TX serial function as described in this answer. Example code:
disable_tx();
pinMode(1, OUTPUT);
//...
digitalWrite(1, whatever);
It is also possible to re-configure the pin as a general-purpose digital input. Again, first disable TX. Example code:
disable_tx();
pinMode(1, INPUT);
//...
foo = digitalRead(1);
You can also use INPUT_PULLUP
to enable the internal weak pullup resistors, and even switch back and forth between pinMode
s. Conveniently, the TX/1 pin is on the standard Arduino header, where you can connect the rest of your circuit to read the output or drive the input.
Beware that TX is still connected to the serial converter. Electrically, this is not a problem because the serial converter only reads the TX value and never drives it; there is no contention. However, the signals you generate as general I/O will probably not be valid serial signals, and thus you will be unable to communicate with a host system the same time you are using TX for general I/O. In other words, don't bother trying to use Serial
the same time as general I/O.
You will still be able to upload programs to the Arduino, though this will send spurious signals to your external circuitry during that process, possibly causing contention. Unplug your external circuitry before uploading programs.
RX should not be used as general input or output
The RX pin is normally driven by the serial converter, and the microcontroller reads it as an input. This allows the host system to download programs and also to read Serial
in running programs.
You can't simply reconfigure the pin as an output, because then both the serial converter and the microcontroller will be trying to drive the pin, and you will have contention.
You also can't simply reconfigure the pin as an input. Whatever circuit that you are trying to read will be trying to drive that pin at the same time that the serial converter is trying to drive the pin, and you will have contention.
In theory, you have several options:
You could cut the trace between the serial converter and the pin on the microcontroller. This assumes that the trace is accessible, you know what you are doing, and you get it right. The downside is that you will never be able to upload another program to that Arduino board.
You could cut the trace as #1, and put a resistor across it (say 470 ohm). This requires even more skill than #1. When the serial converter is in contention to drive the pin, the serial converter will lose and the other thing will win.
In theory, the serial converter could use a tri-state output that can be disabled, or a weak open-collector output that can be over-driven. However, of all of the Arduino board schematics I have seen, none of them actually do this. Plus, you'd have to manually switch it on and off each time you want to upload a new program.
Design your own board that doesn't use serial.
None of these alternatives are actually practical, so your best plan is to never use RX for general-purpose input or output.
-
1Contention on the RX pin is generally not an issue: the Arduino Uno has a 1 kΩ resistor between the serial converter’s TX and the ATmega328P’s RX. It’s resistor RN4B in this Arduino Uno schematic.Edgar Bonet– Edgar Bonet2024年06月08日 20:04:21 +00:00Commented Jun 8, 2024 at 20:04
-
downvote is mine ("The answer is not useful")2024年06月09日 09:04:27 +00:00Commented Jun 9, 2024 at 9:04
Explore related questions
See similar questions with these tags.