0

I recently created a circuit to read the value from two sets of buttons using only two analog pins. It involves setting one pin HIGH and reading the input on the other pin and then swapping. With both pins set to OUTPUT I can still read in values with analogRead() without having to keep switching pin modes.

My question is why is that the case? What is the point of setting a pin to INPUT if OUTPUT can still read in values? Are there any disadvantages of doing so?

  • Just don't expect this to work when changing to some other board. E.g. the Due or Teensy. Other than that there's probably no disadvantage. – Gerben Aug 26 '15 at 18:43
1

The reason for this is because analogRead() has absolutely nothing to do with what mode the pin is in. That mode only has any meaning in the scope of digital operations - i.e., when using digitalRead() and digitalWrite(). When using analogRead() the Arduino code switches the pin into analog mode first and then reads from it.

  • Why do the digital operations require a pin mode to be set while analog pins do not? – James Coyle Aug 26 '15 at 17:22
  • 1
    Because the digital operations are different to the analog operations. When you do an analogRead the IO pin is isolated from the digital IO circuitry, so the pin mode (which affects the digital IO circuitry) has absolutely no effect on the operation. – Majenko Aug 26 '15 at 17:23
  • It's like your car keys. You use them for your car, but you don't use them for your bike, yet both of them are in the garage, and they both come out of the same doorway. – Majenko Aug 26 '15 at 17:29
1

My question is why is that the case? What is the point of setting a pin to INPUT if OUTPUT can still read in values? Are there any disadvantages of doing so?

If you do a digitalRead you can read the current latched state of an output pin (it doesn't change it to input). In other words, you read what was last written to it.

If you do a digitalWrite of HIGH to an input pin it sets the internal pull-up. If you write LOW it disables the internal pull-up. If it switched it to output for you, you wouldn't be able to enable or disable the pull-up. (Note: this applies to older versions of the IDE, newer ones have a pinMode setting of INPUT_PULLUP).

What is the point of setting a pin to INPUT if OUTPUT can still read in values?

That certainly won't work with digitalRead because you are writing a value to the pin in OUTPUT mode, so you will merely read back what you are writing. In other words, you have activated the driver transistors, which will override whatever value you are trying to read.

0

Because I/O pins can be used for either digital input or digital output and can be switched to perform in a given direction. Some of the pins also have an analog capability, either A-to-D conversion (input) or PWM (kind of a psuedo-analog output) but only in one direction permanently assigned by the hardware and therefore not switchable from one to the other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.