pulseIn( pin, LOW ) returns huge wrong values.

Hi,

I have extrange results from pulseIn.

What I have is an IR receiver, from an old home DVD player. I have programmed arduino with interrupt attatch to pin 2 ( where IR data pin is ) and it's OK.

Problem is when I try going through pulseIn.

This is the so simple code:

int irPin = 2; // IR data
volatile unsigned long data;

void setup() {

pinMode( irPin, INPUT );
Serial.begin( 38400 );
Serial.println( "PulseIn testing" );
}

void loop() {

unsigned long current;

while ( pulseIn( irPin, LOW ) == 0 )
{}

Serial.println( "Reading for 30 millis" );

current = millis();
while ( millis() - current < 30 )
{
data = pulseIn( irPin, LOW );
Serial.println( data, DEC );
}

}

As you can see, I wait for a LOW pulse to come and then read durations with pulseIn during 30 milliseconds.

Here is serial log:

Reading for 30 millis

85437501

85375001

85375001

87875001

Reading for 30 millis

164750001

164812501

85812501

0

Question is ... How is possible durations are 85 or 164 MIllions ????? I think pulseIn returns Microseconds ( 1 seconds == 1 Million microseconds ) so it is not possible to have so big numbers in just 30 milliseconds ...

May be I'm wrong about pulseIn return value ??

May be pulseIn returns something smaller than microseconds ?

Should I divide returned value depending on some arduino parameter ?

May I use pulseIn on any arduino PIN ?

I will thank some light on this.

pulseIn() returns 0 when it times out.

Of course pulseIn returns 0 on time out ( I think defaults to 1 second )

What I dont understand are those many-millions microseconds returned within a just only 30 milliseconds time.

In 30 milliseconds I should get up to 30.000 microseconds durations, and PulseIn is returning durations about 85.000.000 or 160.000.000 ... there are not enough microseconds in just 30 milliseconds !!! :o

I don't know, do a print before you read the pulse

This is amazing ... more extrange about pulsein()

I've test this pulseinhelloworld:

void setup()
{
Serial.begin(57600);
pinMode(3, INPUT);
}
void loop()
{
Serial.println(pulseIn(3, LOW));
}

What is my surprise when I just plug a wire on pin 3 ( other extreme not connected ), serial monitor reports many many many values like these:

29816589
31441589
32504089
30379089
33129089
32004089
30004089
31566589
32379089
30816589
32441589
31504089
31816589
31816589
30754089

When I remove wire from pin 3 I get the correct

0
0
0
0
0
0

What is pulsein correct time out so there is no pulse.

Why pulsein reports that ugly data when one extreme wire is connected to pin 3 and other extreme wire is not connected at all ?? Where do those suposed pulses come from ?? and why those so big values ?? ( 30.000.000 microseconds == 30 seconds, wich is absolutly false ... :frowning: )

Doesn't any one see this ( aparently ) malfunction ??

Note: tested with arduino12 and arduino15 on linux platform.

Try enabling the pull-resistor in pin 3: digitalWrite(3, HIGH);

What is my surprise when I just plug a wire on pin 3 ( other extreme not connected ), serial monitor reports many many many values

This is because the input pin is "floating" and picking up noise and so returning noise like values. This will stop when you enable the pull ups because there will be a current path to +ve. Then you just need to feed a real signal into it.

Thankyou, glt and Grumpy_Mike

You are all right about pull-up resistor. I've tested it and it solved my "helloworldpulsein" issue ...

Problem is I can yet understand first topic question ... In an IR test, how is it possible to get in just 30 milliseconds range, several many-millions-microseconds returns from pulseIn ?

If I am not wrong, 30 milliseconds == 30.000 microseconds so I could get at most 30.000 ( aprox ) microseconds adding all pulseIn made in that 30 milliseconds range ...

Thankyou, glt and Grumpy_Mike

You are all right about pull-up resistor. I've tested it and it solved my "helloworldpulsein" issue ...

Problem is I can yet understand first topic question ... In an IR test, how is it possible to get in just 30 milliseconds range, several many-millions-microseconds returns from pulseIn ?

If I am not wrong, 30 milliseconds == 30.000 microseconds so I could get at most 30.000 ( aprox ) microseconds adding all pulseIn made in that 30 milliseconds range ...

In an IR test, how is it possible to get in just 30 milliseconds range, several many-millions-microseconds returns from pulseIn ?

Are you sure the signal is the way up you think it is?

Try pulseIn( irPin, HIGH ) just to check

Almost sure ...

It is an IR receiver and I use a normal TV remote. IR signal mantains HIGH level on data pin where there is no command and receives a pulses train on new command.

I've checked it with attachInterrupt and then log signal during several milliseconds and function is as spected ...

I have read about other thread about pulseIn reporting data several magnitude bigger than spected ...

I'm thinking it is an arduino library bug ... or may be gcc regarding ...