Go Down

Topic: pulseIn() returning values from 0-36 regardless of pulse length (Read 167 times) previous topic - next topic


Hi! I'm pretty much a beginner at this, so I apologize for the poor code and circuit diagram, but i'm at my wit's end with trying to use the pulseIn() function. For reference, i'm using an Arduino Uno.

I have two red laser dot diodes pointed at each other to induce a voltage in the unpowered one, which activates an LM339 voltage comparator and sends a signal to pin 2, so that it can be registered as HIGH/LOW, as my measured current produced by the LED was barely a micro-amp. Basically, pin 2 reads HIGH when the beam is interrupted, and LOW otherwise.

My intent is to use pulseIn() to measure how long the beam is interrupted to make a laser tachometer and measure rpm, however the function seems to output nonsensical values, typically a random value between 0 and 36, but occasionally a value in the tens of thousands, and a couple of times i saw values that seemed large enough to indicate an integer overflow somehow, all regardless of how long i block the beam for. pulseInLong() didn't seem to make much of a difference. I should also note that for some reason, pulseIn() also returns two values per interrupt, once when the beam is broken, then again when the beam is restored. Is there an issue with rapid current fluctuation, or perhaps do i need some sort of interrupt system in the code?

The code i've been using to try and get the function to work is as follows:

Code: [Select]

unsigned long lastPulse = 0;

void setup() {
  pinMode(2, INPUT);

void loop() {
  lastPulse = pulseIn(2, HIGH, 5000000UL);

Circuit diagram (apologies for how messy it is - first one i've made beyond a simple LED circuit):

Should add that E2 is the receiving LED, and E1 is simply an indicator. I've yet to have solidworks or circuit diagrams covered at uni, so i'm not sure which icons were appropriate.

I apologize for posting this again, but it's been three days without a response, and my alternative method of simply running digitalRead() every time the update function runs just isn't fast enough for my purposes. I won't spam the forum with any further reposts, but if there's something i'm doing wrong in my post format, please tell me - thank you!


Circuit diagram (apologies for how messy it is - first one i've made beyond a simple LED circuit):
Since you're unfamiliar with the drawing software IMO you'd be better off just posting a hand drawn schematic with the existing connections. F'rinstance, the posted drawing shows nothing connected to Arduino pin 2, the destination of the signal.  You also show E2 as connected to Vin1 on the opamp and to Arduino ground on the anode.  ??
I don't trust atoms.  They make up everything.

No private consultations undertaken!


I've not heard of a laser diode being used as a light sensor.  Is it a raw diode or is it a laser module that has a constant-current driver built in?

I think you will need to use an oscilloscope to determine if the input pulse is what you are expecting.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


Whoops - sorry about that! I'm a bit tight on time, so sorry about the late reply and not redrawing the diagram. I've updated the diagram as follows though, where E1 is connected to pin 2 from between the anode and pull-down resistor, and i switched E2 around after looking up the orientation and realizing i was just a bit of an idiot.

I saw another reply mentioning using an oscilloscope, and it's very possible that the signal isn't the constant DC i was expecting, but getting a whole oscilloscope is a bit out of my price range right now, so i'll have to wait another month and a half for university to start again to use theirs.

Go Up