Hi guys,
i have the following problem.
An analog input on analogic pin 2 and a digital output on the digital pin 2.
Now if i read analog value from the pin 2 the value is corrupted. If i exclude the digital pin 2 the value is correct.
How it works?
Analogue pin 2 is a different physical from digital input / output pin 2. So doing an analogue read to pin 2 will not be affected by anything that is on digital pin 2.
Yes i think so, but how can the system know that pin 2 is different?
I cant't do:
pinMode(2,INPUT); //for analog input
pinMode(2,OUTPUT) //for digital out
analogRead(2);
digitalWrite(2,HIGH);
You can't do analogRead() on a digital pin - as soon as you use this function the arduino assumes you mean the analog pin is what you mean.
You don't need to use pinMode() for the analog pins - it's handled automatically be the arduino. Although it doesn't really affect anything if you do include it, it can only confuse you later, so make it a habit not to include it.
The small snippet of code you included (by the way - use the '#' button when including code, makes everything look nicer), you are actually setting digital pin 2 to be an input, then straight away making it an output.
So just to make it clear:-
// pinMode(2,INPUT); // no need for this for analog input
pinMode(2,OUTPUT) //for digital out
analogRead(2);
digitalWrite(2,HIGH);
WILL use different pins on the Arduino.
If you want to use the analogue pins as digital input / output you refer to them as Pin 14, 15, 16, 17, & 18.
So:-
pinMode(16,OUTPUT) //for digital out
analogRead(2);
digitalWrite(16,HIGH);
WILL confuse the system because here you are asking the same pin to be an analogue input and a digital output.
When all else fails read the link I posted.