Arduino Tip : Turn your Analog pins into Digital I/O

Analog To Dig Banner
Recently I’ve just found out that I can actually turn my Analog input pins into Digital Inputs or Outputs. It’s really simple.

Usually for the digital pins, we write instructions such as digitalWrite(13, HIGH). To do that with analog pins, simply use digits 14 to 19.

Pin 14 being Analog 0, 15 being Analog 1 and so on. Or you can just write digitalWrite(A0, HIGH).

Here’s a simple sketch to test that out. Turn on an LED that’s connected to Analog 0.

The proof.

What about Digital Inputs? Just treat the Analog pins like they’re digital by using pins 14 to 19 or A0 to A5.

I wrote a sample sketch to test whether the Analog pins can be turned to Digital inputs. When the button is pressed, the LED will light up for two seconds.

Ta-da!

No digital pins were used in these tests 🙂

  1. Frank07-05-2016

    Thanks for the info! I almost got an Arduino Mega because my UNO had not enough Pins for my current project, but now that I know that the analog Pins can also used as digital I don’t need the Mega.

  2. Aniket01-27-2016

    Thank you so much for this information!!!

    • Darwynn McPherson04-12-2016

      I have tried and tried to get my analog pins working for me. Im using the Intel Galileo Gen 2 board with a base shield that comes in the grove starter kit plus.
      I have edited the servo sketch example for a sweep. I have tried to pull some ideas from this but i have gotten no where.
      Here is my code. How would i alter this to set my servo on analog A0 instead of pin 5?

      // Sweep
      // Darwynn McPherson dmcp7600@students.vsu.edu
      // This example code is in the public domain.

      #include
      const int buttonPin = 2;
      const int buttonPin2 = 4;
      int buttonState = 0;
      int buttonState2 = 0;

      Servo myservo; // create servo object to control a servo
      // a maximum of eight servo objects can be created

      int pos = 0; // variable to store the servo position

      void setup()
      {
      myservo.attach(5); // attaches the servo on pin 5 to the servo object
      pinMode(buttonPin, INPUT);
      pinMode(buttonPin2, INPUT);
      }

      void loop()
      {
      buttonState = digitalRead(buttonPin);
      buttonState2 = digitalRead(buttonPin2);
      if(buttonState == HIGH && pos 3){ // goes from 180 degrees to 0 degrees

      myservo.write(pos–); // tell servo to go to position in variable ‘pos’
      delay(5); // waits 15ms for the servo to reach the position
      }
      }

  3. Gil w.05-30-2015

    Thanks for sharing the information. You saved me time.. (If I would’nt find your post, I had to do the tests myself.. 😉

  4. vinod07-02-2014

    Can we do the opp. I want o convert some of the digital pins with Analog capability into actual Analog input pins.

    Vinod

    • waihung07-07-2014

      No you can’t. You’ll have to get additional ADC ICs.

  5. Özay Turay04-11-2014

    You should also say “No analog pins were harmed in these tests.” 🙂

  6. Henrique Coelho03-18-2014

    Very good example.
    I need to do something similar but I don´t have any electronics backgrouns and I ´m affarid I can´t deal with it. I need to get an analog signal from a sensor ( ie. piezoelectric sensor ) and turn into a ON/OFF switch to a serial cable (db9 connector) and closing pins 4 and 6 from such connector. Can I do that using the very same concept you have proved here is this post? If may understaning is correct I can connect the piezo sensors to analog pins A0 and A1 and then get a digital output to pins 4 and 6 from DB9 female serial connector. Can you tell me if this feasible or not? Thank you.

Leave a Reply