Is there a way to set an Arduino pin as being both an input, and an output?
I was looking at tristates, I'm not sure if that is related, or if there is some other method.
Perhaps I can try switching states rapidly. on one pin. I'm just not familiar with how to do it, and I don't see anything about it on the Arduino site.
This is a function of other microcontrollers, such as PICs, but I'm not sure about AVRs and Arduino types.
2 Answers 2
The source code for the 1-Wire protocol is a great place to start when learning how to use a single pin for both input and output.
See also chap. 18. I/O-Ports in the ATmega328p datasheet.
Cheers!
-
Does it mater that the device isnt a 1-wire protocol item? Its NeoPixel Rings. such as adafruit.com/products/1643j0h– j0h2016年10月21日 20:16:35 +00:00Commented Oct 21, 2016 at 20:16
-
4@j0h: Why do you need to connect that as an input? The output from those is usually only useful for chaining them together.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2016年10月21日 20:25:08 +00:00Commented Oct 21, 2016 at 20:25
A tri-state pin is not both an input and an output, rather each pin can be in 1 of 3 states (High, Low, High Impedance). These are typically used on a bus, where many similar devices are connected.
AFAIK the Arduino does not support tri-state, but it is possible to implement a bus. This was done by using open collector/drain devices with a resistor pull-up.
This was a common method of implementing computer busses (more than 40 years ago with RTL logic), but suffers from slow speed, and low fan-out, due to capacitance.
It is still used for low speed short busses such as I2C and 1-wire.
In practice all devices are set as Input. When a device wants to communicate it is configured as Output.
-
The three states are "High", "Low", and "High-Impedance"; a tri-state pin is always an output.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2016年10月22日 01:56:05 +00:00Commented Oct 22, 2016 at 1:56
-
@IgnacioVazquez-Abrams True - brain fade on my part.Milliways– Milliways2016年10月22日 01:59:36 +00:00Commented Oct 22, 2016 at 1:59
-
1All the I/O pins of an AVR-based Arduino are four-state: not only do they support the three states you mention, they also have a fourth
INPUT_PULLUP
state, which is a weak pull to Vcc. You can connect a pin to an open-collector bus directly and then just switch betweenINPUT
(i.e. HiZ, to either read or write 1) andOUTPUT LOW
(to write 0).Edgar Bonet– Edgar Bonet2016年10月22日 10:48:57 +00:00Commented Oct 22, 2016 at 10:48