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

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

Project Source on GitHub Return to the LEAP Catalog
About LEAP

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.

Projects are often inspired by things found wild on the net, or ideas from the many great electronics podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know or send a pull-request.

NOTE: For a while I included various scale modelling projects here too, but I've now split them off into a new repository: check out LittleModelArt if you are looking for these projects.

Project Gallery view the projects as an image gallery Notebook reference materials and other notes Follow the Blog follow projects and notes as they are published in your favourite feed reader