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 Project Gallery Return to the LEAP Catalog

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

LEAP is just my personal collection of projects. Two main themes have emerged in recent years, sometimes combined:

  • electronics - 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
  • scale modelling - I caught the bug after deciding to build a Harrier during covid to demonstrate an electronic jet engine simulation. Let the fun begin..
To be honest, I haven't quite figured out if these two interests belong in the same GitHub repo or not. But for now - they are all here!

Projects are often inspired by things found wild on the net, or ideas from the many great electronics and scale modelling podcasts and YouTube channels. Feel free to borrow liberally, and if you spot any issues do let me know (or send a PR!). See the individual projects for credits where due.