How do I measure an analog pulse length?

I like to measure the length of a negative spike. The voltage is about 3.5V and the fall is about 500mV.

Range is about 1-1000ms - but below 1ms would be appreciated (0.1ms starting point)

Can that be done with an UNO?

60s.jpg

Yes but it is not easy.
You can use the internal comparator and a pot or two resistors to trigger an interrupt from the analogue signal. Then use the microseconds timer to measure the time between interrupts.
For simplicity you could use an external comparator and use the pulse in function.

Thanks for the reply.

I think I will go the comparator route for now. I will look into that.

How is the Arduino short time accuracy?

attachInterrupt(interrupt, function, mode)

Description

Specifies a function to call when an external interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3).

Parameters

interrupt: the number of the interrupt (int)

function: the function to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.

mode defines when the interrupt should be triggered. Four contstants are predefined as valid values:

LOW to trigger the interrupt whenever the pin is low, CHANGE to trigger the interrupt whenever the pin changes value RISING to trigger when the pin goes from low to high, FALLING for when the pin goes from high to low.

Surely, a FALLING edge on an interrupt, get the time..

But, can you change it (when the IRQ is triggered) to a RISING edge?

if so, grab the time and compare?

Nick Gammon has an example of using the analogue comparator. A crazy idea might be to try and bias the signal so the normal signal registers as HIGH on a digital pin and the negative spike registers as LOW.

You could maybe use a Schmitt trigger to force a definitive HIGH/LOW.

A software only option might work if you adjust the ADC pre-scale. You can speed ADC up without much loss of accuracy so it only takes about 18us to do a read and with code overhead of checking this you could easily stay within about 30us.

A crazy idea might be to try and bias the signal so the normal signal registers as HIGH on a digital pin and the negative spike registers as LOW.

Well that is what you get with the external comparator, something like LM339.

Grumpy_Mike:

A crazy idea might be to try and bias the signal so the normal signal registers as HIGH on a digital pin and the negative spike registers as LOW.

Well that is what you get with the external comparator, something like LM339.

I was thinking more cheaply using resistors/potentiometer.

I was thinking more cheaply using resistors/potentiometer.

Yes but by the time you have added:-

You could maybe use a Schmitt trigger to force a definitive HIGH/LOW.

You might as well have done it with an LM339 and have done.

Arduino micros() function is good to 4 usecs accuracy. You can get closer using a check and count loop.

I got an LM339 - but seems not working. Fresh units should arrive tomorrow.

Question is if Arduino can/should measure the time or I should do that externally? I need some Arduino I/Os for a LCD. If externally, what IC is suggested. Potential time range is about 0.1ms - 1s

Potential time range is about 0.1ms - 1s

two calls to micros() do the trick, start = micros(); ... duration = micros() - start;

For a more fancy sketch you could use this stopwatch class - http://playground.arduino.cc/Code/StopWatchClass - it makes it easy to use multiple independent stopwatches.

I got an LM339 - but seems not working.

They are open collector so they need a pull up resistor on the output before you see anything.

Grumpy_Mike:

I got an LM339 - but seems not working.

They are open collector so they need a pull up resistor on the output before you see anything.

DOH! I just hooked up the scope there. Now I put a 10K between + and output and it works perfectly!

PS: Big thanks to Grumpy_Mike!