2011 July 13

Simple timer

Filed under: Data acquisition,Software — gasstationwithoutpumps @ 01:51
Tags: , , ,

One of the simplest and most useful tasks that a data acquisition system can do is to measure the time between two events.

On the Arduino, this is easily done with the digitalRead() to detect the transition and micros() to time the events in microseconds. First you have to figure out what pins the inputs will use, and in the setup() routine, specify that they will be used for input, not output. In my example, I have used pin 4 for the first event and pin 5 for the second event.

In the loop() routine, have the Arduino do a busy-wait for the first event. Busy-waiting is a crude technique for determining when an event happens—you just keep checking the input until it is the value you want for starting the event. Modern computers rarely do busy waiting, because it ties up the processor doing nothing useful. The more common approach is to use an interrupt, which is possible with the Arduino, but a bit trickier to code. Since we weren’t doing anything else with the Arduino while waiting for the initial event, it made sense to me to do a busy wait.

Immediately after the starting event, record the current time. I also turned on the on-board LED (pin 13) to indicate that the board was now waiting for the ending event.

Then busy-wait for the ending event, and record the time right after it.

My example reports the difference in times on the USB line, but you could store the results in an array or do further processing of the times.

// Timer test
// Kevin Karplus
// 12 July 2011

// One of the simplest data acquisition tasks is
// to time the interval between two events.

// This program waits for pin 4 to go high,
//   starts a timer, lights the on-board LED,
//   then waits for pin 5 to go low,
//   when it turns of the LED and resets the timer.
// It reports the time between pin 4 going high and pin 5 going low
//   in microseconds.

// It is easy to change the code to use
// either transitions to high or transitions to low.
// Using a pull-up resistor can convert any make or break contact to
// a low-going or high-going edge.
// Opposite polarities were used in this example, so that the
// same square wave could be fed to both pins and the width of the
// positive pulse timed.

// The minimum time it can report (if pin 5 is already low when
// pin 4 goes high is about 12 microseconds +- 4 microseconds).
// The reported time seems to be always a multiple of 4 microseconds,
// which is most likely the resolution of the micros() call.

// Timing seems to be fairly reliable down to about 60 microsecond.

void setup()
    pinMode(4, INPUT);
    pinMode(5, INPUT);

    pinMode(13, OUTPUT);    // LED output

void loop()
    unsigned long start_time;
    unsigned long stop_time;

    while(digitalRead(4)==0) {}
    while (digitalRead(5)) {}
    Serial.print("time hi->lo=");
    Serial.println(" microsec");

[Update: 8 October 2011. I redid the code using the sourcecode tag, so that WordPress no longer discards the spacing.]

I put this little test program on gist as git://gist.github.com/1079561.git


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: