Arduino

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.

Fan of making things beep, blink and fly. Created AddOhms. Stream on Twitch. Video Host on element14 Presents and writing for Hackster.IO. Call sign KN6FGY.

4 Comments

  1. Thank you for the article, btw: pinMode(INPUT, A0) is a typo, it’s should be pinMode(A0, INPUT)

  2. 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.

Write A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.