How to Configure Arduino Analog Pin as Digital Pin

Analog pins on Arduino board are used as analog input pin as default configuration. In these pin you can detect analog voltage from 0 - 5V continuously. So, input voltage with a value of 1V, 1.1V, 2V, 2.7V up to 5V can easily read through this pin. Typically, an Arduino board has more than one Analog pin. For example, Arduino Uno board has 6 analog pin namely A0 - A5. Arduino Mega board has more, which are 16 Analog pins.


If we look at Arduino as an entity, A0 - A5 pins, as the name implies, are intended as an analog pin. But if we look further, an Arduino board is a control board built in ATmega microcontroller as main components. So, it can be said that the actual Arduino analog pin has more functionality than just Analog pin. Please check Arduino pinout diagram as follows:


Note that pins A0 - A5 Arduino Uno are actually PC0 - PC5 pin (Port C.0 - Port C.5) belonging ATmega microcontroller IC. As we know through the datasheet, each pin microcontroller is designed to have a dual function, namely as input or output pin. In addition, in some of the pins also have special functions such as ADC / Analog to Digital Converter input pin (PC0 - PC5), or as analog output pin using PWM / Pulse Width Modulation (PD3, PD5, PD6, PB1, PB2, PB3 pin), or has serial communication function in PD0 and PD1 pin. For more detail about mapping Atmega microcontroller pin into Arduino board can be read on diagram as follows:


Pinout diagram of the image above, we can a conclusion that the actual Arduino Analog pin at pin A0 - A5 can also be activated as a regular digital pins because it was actually pin PC0 - PC5 in ATmega microcontroller. Just because the Arduino board was designed compact controller board, then the pin is enabled as an analog pin only (default configuration). However, once again, this pin can be used as a digital input / output pin. How to do this, please refer to tutorial below:

Analog Pin Arduino as a Digital Output Pin

To enable Arduino analog pin as digital output pins, do same action as a regular digital pins, relatively. You can call this analog pin with alias name of 'A0', 'A1', up to 'A5'. For example:

pinMode (A1, OUTPUT);
digitalWrite (A1, HIGH);

The first line will make analog pin ‘A1’ as a digital output pin. While source code on next line you would issue a logic 'HIGH' voltage (5V or logic '1') to this pin.
Note: please do not forget to put script above (pinMode (A1, OUTPUT);) in void setup ()

Analog Pin Arduino as a Digital Input Pin

To set Arduino analog pin as a digital input pin is somewhat different way. You need to know that the PC0 - PC5 in ATMega microcontroller are wired with internal pull-up resistor. Thus the trick to make the Arduino analog pin into a digital input pin is to issue logic 'HIGH' to the analog pin WITHOUT calling ‘pinMode (a, b)’ function first.

For example:

Void setup () {
digitalWrite (A1, HIGH);
}
Note : declaration setting for analog pin as digital input should be done in ‘void setup ()’ declaration block as in example above. 
After you write the source code above, then analog pin 'A1' will function as a normal digital input pins and can be used to detect digital input (e.g. keystrokes) with ‘digitalRead()’ function. Example usage:

A1pin int = 0;
Void setup () {
 digitalWrite (A1, HIGH);
}

A1pin = digitalRead (A1);
if (A1pin == 0) {
 digitalWrite (13, HIGH)
}
else {
 digitalWrite (13, LOW)
}

Examples sketch above is to turn on / off the internal LED on the Arduino board based on pin 13 according to A1 pin status. If the status value is ‘0’ (the button is pressed), the LED will light up, and if the value is '1' (the button is released) LED will turn off.

Well,tutorial about how to configure Arduino Analog pin as digital pins either as input or output is ended at this moment. Hopefully it could be useful for you. If you have any questions,please submitted in comments column below. Thanks.
Previous
Next Post »