How to Configure Arduino Analog Pin to be Digital Pin

Analog pins on Arduino board is default pin used as analog input. In this pin you can measure 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 pin Analog. For example, the Arduino Uno board has 6 analog pins namely ‘A0’, ‘A1’ until ‘A5’. For Arduino Mega, the board has more analog pins (16 pcs).

If we look at Arduino as an entity, the A0 – A5 pins, as the name implies, is intended as an analog pin. But if we look further, an Arduino board is a control board with microcontroller AVR ATmega as main component. So, it can be said that Arduino analog pins has more functionality than just analog measurement. Consider Arduino pinout diagram as follows:

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

Pinout diagram from picture above explains that Arduino analog pin at pin A0 - A5 can also be activated as regular digital pins because it was actually pin PC0 - PC5 in AVR ATmega microcontroller. Just because Arduino is designed as compact board, then the pins are enabled as analog pin only. However, once again, this pin can be used as a digital input / output pin as usual. How to do it, please refer to the tutorial below:

Arduino Analog Pin as Digital Output Pin

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

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

The first line will make analog pin ‘A0’ as a digital output pin. While on next line you would issue a 'HIGH' logical voltage (5V or logic '1') to this pin.

Note: do not forget to put the code 'pinMode (a, b);’ in void setup ()

Arduino Analog Pin as Digital Input Pin

To set Arduino analog pin as a digital input pin is somewhat different way. You need to know, the PC0 -PC5 pins are connected with internal pull-up resistor inside ATmega microcontroller. Thus the trick to make Arduino analog pin into a digital input pin is to issue 'HIGH' logical voltage to the analog pin without calling pinMode (a, b); function first. For example:

Void setup () {
 digitalWrite (A0, HIGH); 

Note that a declaration setting of analog pin as digital input should be done in declaration void setup () block as in example above. After you write the source code above, then the analog pin 'A0' will function as a normal digital input pin and can be used to detect digital input (e.g. keystrokes, button or limit switch) with digitalRead () function . For example:

A5status int = 0;
Void setup () {
 digitalWrite (A0, HIGH);
A5status = digitalRead (A5);
 If (A5status == 0) {
 digitalWrite (13, HIGH)
else {
 digitalWrite (13, LOW)

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

So, this is an Arduino tutorial on how to configure Arduino analog pins as digital pins either as input or digital output, may be useful for you. If you have any questions can be submitted in the comments column below. Thanks.
Next Post »