Digital & analog read disagrees with meter

I'm currently having a problem with the readings I get using both the digitalRead & analogRead functions (and outputting to serial monitor) not agreeing to what I measure with my meter.

I currently have a sensor that when nothing happens, the pulse line outputs 0V, per the instructions for the sensor and per what I read using my meter. When the sensor is tripped, the pulse hits about 3-4V for about a second. I was hoping to use this change as an input to my program (using an if function).

However, I was getting very inconsistent results and when I hooked the pulse up to the serial monitor, taking readings every 1 second or so, I saw the following:

Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 0
Read switch input: 0
Read switch input: 0
Read switch input: 0
Read switch input: 0
Read switch input: 0
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1
Read switch input: 1

This pattern will continue non-stop, and does not change even when the sensor is tripped. I tried using a pull-down resistor to bring the pin to ground when the switch isn't activated, but that did not help. The thing is that no matter what, when I measure this sensor using my meter, I gt 0V when its not tripped, and 3-4V when it is tripped. I've hooked the meter up with the Arduino, and I'll still get the above pattern, even when the meter reads as expected.

A similar thing happens when I use an analogRead function, except instead of switching between 0 and 1, it gradually escalates to maximum reading and then goes back down.

Any ideas? Thanks for your help.

did you set the pin mode for digital? (this is common and shows the same type of symptoms)

what did u connect to the pin?
is it floating?

Post the sketch you are using.

Here is the sketch I am using:

int pulsePin = 11;


void setup() {               
 Serial.begin(9600);  // Setup the Serial library at 9600 bps
 pinMode(pulsePin, INPUT);  // sets the digital pin as input to read switch
}

void loop() {
  Serial.print("Read switch input: ");
  Serial.println(digitalRead(pulsePin));    // Read the pin and display the value
  delay(1000);
 
}

The pin is connected to the output "pulse" line on the sensor, as well as a pull-down resistor. When the sensor is tripped, the pulse line outputs about 2-3V. When the sensor is not tripped, it outputs 0V, and therefore the pin should be pulled down to ground (if my understanding is correct).

Thanks

Is the sensor well connected to the common ground with board?

r u sure u connected the cable properly to pin 11?

btw: pin 11 is not conected to the ADC MUX... so u cant read from pin 11 with analogRead()...

Serial.println(digitalRead(pulsePin));    // Read the pin and display the value

This will only ever print out a 0 or a 1, which is what you were showing at the first post.

That is how it is supposed to work.

What did you think it would produce?

A digital pin as an input will read 0 if the input voltage is below 1.5V, read 1 if above 3.0V and could read either 0 or 1 if the voltage is anywhere inbetween (in practice the threshold voltage will be fairly stable for each pin, but the threshold is only guaranteed to be somewhere in that range. Slowly varying voltages on a digital pin could lead to lots of switching between 0 and 1 and back as noise interferes with the input stage - analogRead is a better choice if so perhaps. Remember analogRead only works on analog pins.

RPCoyle:
Is the sensor well connected to the common ground with board?

Yes, I have checked that the ground is securely connected to the ground on the board.

RIDDICK:
r u sure u connected the cable properly to pin 11?

btw: pin 11 is not conected to the ADC MUX... so u cant read from pin 11 with analogRead()...

Yes, it is connected properly to pin 11. About the analogRead(), I had tried the sensor using an analog pin and took the readings and got a similar pattern where the reading starts at 0, reaches the maximum in about 5 seconds, and then goes back down to 0. I changed pins to a digital pin to make sure it wasn't the pin and updated the sketch accordingly, which is why the pin referenced above is not capable of using analogRead.

Grumpy_Mike:

Serial.println(digitalRead(pulsePin));    // Read the pin and display the value

This will only ever print out a 0 or a 1, which is what you were showing at the first post.

That is how it is supposed to work.

What did you think it would produce?

Right. I'm know thats how digitalRead works, and that is ideally what will happen. However, as I mentioned, when I am reading a constant 0V on my meter, I am still getting readings of "1" using the above sketch.

MarkT:
A digital pin as an input will read 0 if the input voltage is below 1.5V, read 1 if above 3.0V and could read either 0 or 1 if the voltage is anywhere inbetween (in practice the threshold voltage will be fairly stable for each pin, but the threshold is only guaranteed to be somewhere in that range. Slowly varying voltages on a digital pin could lead to lots of switching between 0 and 1 and back as noise interferes with the input stage - analogRead is a better choice if so perhaps. Remember analogRead only works on analog pins.

Shouldn't using a pull-down resistor help eliminate this noise? Also, the voltage isn't varying slowly when I read the pulse with a meter. It stays at a constant 0V, and then when the sensor is tripped, quickly spikes up to about 3 - 4V. So I get that its possible maybe I'm not reaching the 3V threshold for the pin to constantly read 1, but shouldnt it be a consistent 0 when there is 0V?

what if u just connect a wire from arduino ground to that arduino digital pin 11?

Two things - a meter is a pull-down resistor in effect - so when the meter is connected the pin cannot float. Most modern digital meters look like 1M or 10M resistors. Input pins only float when the resistance is up in the 10^10 ohms or more region.

Secondly fast noise spikes will not show up on the meter but will be seen by digitalRead(). Logic signals switch in nanoseconds, meters integrate input voltages over a second or so - short spikes are smeared out to almost nothing.