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 🙂
Hi! I’m noticing this is an old post, but I just want to thank you because you helped me with my final project! I’m using this with a motor than I needed to add but I my digital pins were already used.
Once again, thank you . 🙂
Hi webmaster, i’ve been reading your site for some time and I really like coming back here.
I can see that you probably don’t make money on your page.
I know one simple method of earning money, I think you will like it.
Search google for: dracko’s tricks
I read this paragraph completely on the topic of the resemblance of hottest and earlier technologies, it’s amazing article.
I have noticed you don’t monetize your website, don’t waste
your traffic, you can earn additional bucks every month because you’ve got hi
quality content. If you want to know how to make extra bucks,
search for: best adsense alternative Wrastain’s tools
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.
Thank you so much for this information!!!
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 [email protected]
// 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
}
}
Thanks for sharing the information. You saved me time.. (If I would’nt find your post, I had to do the tests myself.. 😉
Can we do the opp. I want o convert some of the digital pins with Analog capability into actual Analog input pins.
Vinod
No you can’t. You’ll have to get additional ADC ICs.
You should also say “No analog pins were harmed in these tests.” 🙂
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.