Fork me on GitHub

Project Notes

#240 DigitalInputWithAnalogPins

Demonstrate how analog pins can also be used as a digital GPIO

Notes

This is a simple demonstration of a tip shared by peter@txplore.com in the Arduino Tips and Tricks email newsletter

On the Arduino Uno, there are 6 analog pins. While we’d normally use AnalogRead to read A/D values and AnalogWrite to write PWM, they can also be used as GPIO digital pins.

The conventional analog pin mnemonics can be used:

pinmode(A0, INPUT);
int in = digitalRead(A0);

If desired it’s also possible to use the raw pin number (if driving pins algorithmically for example). Pin definitions are found in the pins_arduino.h. For the Uno, the

To use them as digital inputs, just use this reference table:

Analog Pin Pin pins_arduino.h definition
0 14 static const uint8_t A0 = 14;
1 15 static const uint8_t A1 = 15;
2 16 static const uint8_t A2 = 16;
3 17 static const uint8_t A3 = 17;
4 18 static const uint8_t A4 = 18;
5 19 static const uint8_t A5 = 19;

Using the pin number explicitly:

pinmode(14, INPUT);
int in = digitalRead(14);

Example

The DigitalInputWithAnalogPins.ino sketch is a simple exercise of analog pins A0..A5 using both digital input and output.

Five pins A0..A4 are set to sequentially drive corresponding LEDs at about 5mA.

A push-button is read on A5. Pressing the button reverse the LED sequence.

Construction

Breadboard

The Schematic

The Build

Credits and References

About LEAP#240 Arduino
Project Source on GitHub Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is my personal collection of electronics projects, usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting (IMHO!).

The projects are usually inspired by things found wild on the net, or ideas from the sources such as:

Feel free to borrow liberally, and if you spot any issues do let me know. See the individual projects for credits where due. There are even now a few projects contributed by others - send your own over in a pull request if you would also like to add to this collection.