Analog and Digital ports

Can analog port be used as digital?

Your topic was MOVED to its current forum category as it is more suitable than the original as it is no way an Introductory Tutorial

As to your question the answer is yes, apart from A6 and A7 on a classic Nano

1 Like

On WHAT?

Thank you, In my case, I have a Mega, so i can use all 16 analog ports as digital? Do i need to change or add additonal coding?

No additional coding required. Just treat them as you would any other digital pin

Got it, thanks again!

A0 - A15 are the legends on the silkscreen (component side) of the MEGA Board. As depicted in Fig-1, by default (at power up of MEGA), A0-A7 pins are connected with Port-F and A8-A15 are connected with Port-K. As a result, they are going to work as digital IO lines. Here, pinMode(), digitalRead(), and digitalRead() functions are active.

When ADC Module is activated, the same A0-A15 pins are connected with ADCUB (ADC Upper Bank) and ADCLB of the ATmega2650 MCU of MEGA Board and they are termed as analog ports/lines/channels. Here, analogRead() function is active.

The schematic of MEGA Board does not show any numerical values for A0-A15 pins.
adcMegax


Figure-1:

Behind the scenes, the Mega pins A0-A15 are defined as digital pins 54-69:

#define PIN_A0   (54)
#define PIN_A1   (55)
#define PIN_A2   (56)
#define PIN_A3   (57)
#define PIN_A4   (58)
#define PIN_A5   (59)
#define PIN_A6   (60)
#define PIN_A7   (61)
#define PIN_A8   (62)
#define PIN_A9   (63)
#define PIN_A10  (64)
#define PIN_A11  (65)
#define PIN_A12  (66)
#define PIN_A13  (67)
#define PIN_A14  (68)
#define PIN_A15  (69)

static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;
static const uint8_t A7 = PIN_A7;
static const uint8_t A8 = PIN_A8;
static const uint8_t A9 = PIN_A9;
static const uint8_t A10 = PIN_A10;
static const uint8_t A11 = PIN_A11;
static const uint8_t A12 = PIN_A12;
static const uint8_t A13 = PIN_A13;
static const uint8_t A14 = PIN_A14;
static const uint8_t A15 = PIN_A15;

Thanks Dave,
Without defining variables, would this be ok?

const int buttonPin = 54;
const int ledPin = 55;

That will work fine.

But why confuse things by not using the pin designations printed on the board ?

Yes, those would work fine. 3d printer control boards using the Mega's chip use definitions like that. If you are writing them in your code to help folks understand how to hook things up to a Mega, using the common silkscreened labels may be clearer:

const int buttonPin = A0;
const int ledPin = A1;

Does the board not show PIN55?

New to Arduino and programming, forgive my ignorance. Learning stage.

No, it doesn't: Arduino Mega 2560 Rev3 — Arduino Online Shop

Ah, i see the light now. Got it.
So Arduino will know by the code how to use the pin as either a analog or digital?

Yes

Thank you all for making it a little clearer to understand the pinout on the Mega, very much appreciated!

Yes. Behind the scenes, these all compile (on a Mega) to measure the voltage on the exact same pin:

x = analogRead(A0);
y = analogRead(0);
z = analogRead(54); 

...but if you switch devices, then A0 might not be 54.

That makes much more sense to me now.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.