0

I have an Arduino Micro and I want to use pin 6 and 8 as an analog pin, according to the manual this is possible:

The Micro has a total of 12 analog inputs, pins from A0 to A5 are labelled directly on the pins and the other ones that you can access in code using the constants from A6 trough A11 are shared respectively on digital pins 4, 6, 8, 9, 10, and 12.

What exactly do they mean by constants? I am aware what a constant is in the scope of programming, however I'm not sure what I should use to assign pins 6 and 8 as an analog pin? I assume I cannot just do an analogRead(6)?

1 Answer 1

2

Constants are variables that cannot change. They are constant.

In this case they are variables that have been set up in the board definition files (in pins_arduino.h in case you're interested) and are named just as they have said - A6 through A11. This complements the existing A0 through A5 that are normally there anyway.

So you just use those names and it chooses the right pin for you:

int val = analogRead(A8);

The definitions of the pins are:

static const uint8_t A0 = 18;
static const uint8_t A1 = 19;
static const uint8_t A2 = 20;
static const uint8_t A3 = 21;
static const uint8_t A4 = 22;
static const uint8_t A5 = 23;
static const uint8_t A6 = 24;   // D4
static const uint8_t A7 = 25;   // D6
static const uint8_t A8 = 26;   // D8
static const uint8_t A9 = 27;   // D9
static const uint8_t A10 = 28;  // D10
static const uint8_t A11 = 29;  // D12

So no you cannot just use the number 8, you have to use A8 which points to pin number 26. The Arduino core then interprets that number as an analog pin (as opposed to the digital pin, which is 8) and performs an analog read on it.

8
  • so conversely (and for my own understanding), to use these pins as a digital pin (which they are also capable of) I can also do a digitalRead(8)? Furthermore, is the 'A' required? I've read that it automatically assigns A if its an analogRead.
    – Alex
    Nov 4, 2016 at 0:23
  • When referring to the pin in a digital context you can use either label. Nov 4, 2016 at 0:26
  • If A8 equals 8 then they are interchangeable. If not, then not.
    – Majenko
    Nov 4, 2016 at 0:26
  • I guess I just found it strange that one pin could be used both as analog and as a digital!
    – Alex
    Nov 4, 2016 at 0:27
  • I just looked the file up - you cannot use 8, you have to use A8 or 26.
    – Majenko
    Nov 4, 2016 at 0:29

Your Answer

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

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