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?

1
  • 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
    Commented Aug 26, 2015 at 18:43

4 Answers 4

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.

3
  • Why do the digital operations require a pin mode to be set while analog pins do not? Commented Aug 26, 2015 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
    Commented Aug 26, 2015 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
    Commented Aug 26, 2015 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.

0

It is good practice to define them, especially if you program something other then an arduino. For example a port pin on the PCF8574 (Peripheral device) is written as a 1 so you can read it, it has a small internal pull up. If you pull it low in this condition you will read a zero. If you write a zero you will always read a zero. Driving it to a 1 when set to zero can destroy the part. This configuration is also used on microcontrollers. There are hundreds of different port configurations and programming options, it is best to read the data and understand it first otherwise you may be purchasing another.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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