How to use pulseIn Function?

Hello, I'm having a problem using the pusleIn function. I'm pretty sure everything is right. Anyway the led is going HIGH, and LOW. The pusleIn is supposed to give me a lenght (μs). Anyway i keep getting zero . I read that the zero means that there is no pulse. However there is. Anyway here is the code: :confused:

int Led = 7;
long Duration;

void setup(){


void loop(){


Duration = pulseIn(Led, HIGH);



Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we’re trying to read your code. Do not post double spaced code.

I guess you are expecting Duration to be 50? You have a complete and fundamental misunderstanding of how pulseIn() works. When you call pulseIn() it sits there and waits for a pulse. In your code you output a pulse before you called pulseIn(). The lines run sequentially, not simultaneously. The best way to mess with pulseIn() is to put the pin in input mode and then generate the pulse externally to your Arduino, for example with a push button.

Yeah, i did that thinking the the void Loop would then repeat.

It's actually a good thing to learn that pulseIn() is a blocking function. By default it has a 1000 ms timeout duration. So if there is no pulse it will act as a 1 second delay in your sketch, which can cause all sorts of problems. It does have an optional parameter where you can set a different timeout duration.

Oh alright thanks. While the led didn’t work, i just made it with a button and set the timeout to about 1 second. It worked!