Arduino pinMode on Analog Inputs

pinMode analog input tip

All Arduino boards contain analog and digital pins. The Arduino functions have different calls depending on the pin type. For example, when calling analogRead(), an analog input pin is automatically changed from a digital input (or output) into an analog input. For this reason, it isn’t necessary to call the pinMode function on the pin. However, when I write Arduino Sketches, I still put a pinMode(A0, INPUT) in setup anyway.

Keep reading to see why I use pinMode on Analog Inputs.

Why use pinMode on Analog Inputs?

Because when I look at the code, especially after a long break, I want to know what pins I am using and how. So if I look at setup() and see I am pinMode() on the pins, I’ll know I’m using it. Additionally using the designator A0 instead of 14, on an Uno, I know I mean the Analog pin.

However, there is still a problem.

How do I know whether or I am using the pin as a digital or analog input?  Even though I call it A0, maybe I want the pin to be digital.

Well, in that case, I use a comment.

// Example of how I setup analog inputs
void setup() {
     pinMode(A0, INPUT); // analog
}

void loop() {
     int reading = analogRead(A0);
}

// EOF

Simple, but effective.

Long comments, URLs, and code tend to get flagged for spam moderation. No need to resubmit.

ALL comments submitted with fake or throw-away services are deleted, regardless of content.

Don't be a dweeb.

Leave a comment

4 thoughts on “Arduino pinMode on Analog Inputs

  1. When you say “For example, when analogWrite() is called, an analog input pin is automatically”, you actually mean analogRead(). You cannot analogWrite() on an analog pin, it’s for PWM.