AnalogRead on DigitalPins

Hello,

I would to know if is possibile to use the AnalogRead command on Digital Pins.

I need it due that I'v to read the GND signal (wich is possibile on A0-5 pins).. but for Digital Pins is a lil different and I didnt know how to manage it.

May you help me?

Thank you,
Best Regards

I have not tried it but you can surely code it.
If the compiler does not complain, you're in bussiness, but i have no clue about the usefullness of the result.

But what is exactly the point.
If you read the state of the digital port it is either high or low.
You could say that low is the equivalent of being "grounded".

The analogRead function uses the analog to digital convertor available from pins A0 to A5. This gives you a number between 0 and 1023 in proportion to the voltage on the pin (between GND (0V) and 5V).

You cannot use it on the other digital pins.

Please explain a bit more about what you are trying to achieve.

Hi,

I want to connect a normal infrared sensor (for alarm system) to the arduino.
Usually those infrared close the GND to the pin wich will go connected to the arduino, and I want to check with the arduino if the pin is closed or not.

Sorry for my bad english.

Thank you.

(the infrared sensor is somethings like it: http://www.segurprofnetsrl.com/wp-content/uploads/2009/11/sensoreAdInfrarossiInterno.jpg)

This means you want to detect a status change from the sensor.
I guess that a digital input is good enough for that.
If the normal state is "on" and it is +5V then if the sensor is activated will turn to GND which is "off" or low.
That is no problem with a digital port.

The only thing you need to do is see how to connect the sensor to the arduino and program the functionality.

I have to remark that "it looks like this" is not good enough, the specification and schematics is what counts :wink:

You can read a active low digital input signal coming from a open collector or open drain output by enabling the internal pull-up resistor for the specific digital input pin you are using. That will provide the needed +5vdc voltage when the sensor is in the inactive state.

Lefty

just use code (x stands for pin You want to use):

in setup
pinMode(x,INPUT);
digitalWrite(x,HIGH);

in loop

if(digitalRead(x)==LOW)
{
//Your code
}

This will enable the pin as a input then set the internal pull up resistor. Then You just check the pin status

Thanks to everyone for the help :slight_smile:

Noorman:
If the normal state is "on" and it is +5V then if the sensor is activated will turn to GND which is "off" or low.
That is no problem with a digital port.

For what I know.... in normal state the pin is like "unconnected", the signal will arrive only when it is connected.

zachwiej:
pinMode(x,INPUT);
digitalWrite(x,HIGH);
This will enable the pin as a input then set the internal pull up resistor. Then You just check the pin status

But it will not cause a short-circuit if the PIN is HIGH and the senso connect the GND ?
Or when the pin is in INPUT mode, it will not send the +5v out trought the pin when set HIGH ?

But it will not cause a short-circuit if the PIN is HIGH and the senso connect the GND ?

No, the internal pull-up resistor represents a 40k ohm connection to +5vdc, so when the sensors pulls the pin to ground, only .12 millamps flows through the sensors output pin.

Or when the pin is in INPUT mode, it will not send the +5v out trought the pin when set HIGH ?

No, when the pin is set to input mode, setting the pin HIGH just enables the internal pull-up resistor to +5vdc.
This is the standard way to deal with 'floating' input pin conditions when wiring to passive switch contacts or devices that use a open collector or open drain type output pin.

Lefty

HI,

Thanks you for all the helpfull information, but I'v a question, if I have a similare sensor connected to the Analog pins, I'v to keep it HIGH too for avoid false-positive ?

SO maybe in the setup I'v to put somethings like that:

for (int i=14; i<=19; i++) {
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}

for (int i=3; i<=9; i++) {
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}

Yes, that code looks fine for setting the pins to input mode and enabling the internal pull-up for each of them.

Lefty

Hi,
Thanks it work as a sharm! :slight_smile:

Just for future reference:

When you pull-up the interna resistor setting digitalPort(pinNum, HIGH), with analogRead or digitalRead it will give the value as the 5V is connected to the pin.

Eg. digitalRead(pinNum) will give 0 when connected to GND, otherwise if the pin is free.. it will give 1.

Thank you to everyone who helped me!