5

analogRead(nGPIO_max + lockerVal) were nGPIO_max(maximum digital pins in Arduino Uno is 13) and lockerval is 2. Which pin is it reading?

  • 13 + 2 = 15, so pin 15. I'll give you a clue: A0 is pin 14. – Majenko Jul 17 '15 at 11:29
13

To answer the question in the title: No, you cannot use analogRead() to read a digital pin. A digital pin cannot behave as analog because it isn't connected to the ADC (Analog to Digital Converter).

If you call something equating to analogRead(15) on an Uno, then it should read pin A1. You can see the pin assignments in the board-specific versions of pins_arduino.h (shipped with the IDE):

static const uint8_t A0 = 14;
static const uint8_t A1 = 15;
static const uint8_t A2 = 16;
static const uint8_t A3 = 17;
static const uint8_t A4 = 18;
static const uint8_t A5 = 19;
static const uint8_t A6 = 20;
static const uint8_t A7 = 21;

Using those raw numbers directly is generally not a good idea though as it obscures your code's meaning, and will potentially make it non-portable to other boards. It's better to call something like analogRead(A1).

It's worth noting that the Arduino library fudges the pin numbers slightly. Calling analogRead(1) has the same result as calling analogRead(A1). That means that even if you tried to use analogRead() on a digital pin, it might actually end up reading an analog pin instead. Alternatively, it might just return a meaningless result, depending on the pin number you give it.

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.