View Full Version : Precise Event Timing Apporaches

12-05-2006, 05:42 AM

I am working on a physics demonstration and need to start and stop a timing device. Accuracy of 10 millisecond or better would be nice.

I tried using a do loop with with different pulsout values to slow the loop down so that 500 loops would equal a second and then use the loop counter as a "millisecond" counter - but there has to be a better way. The entire experiment apparatus and the code are ready to go but it would be so much better with a real clock timing the events.

I thought the DS 1302 might work but I am not skillful enough to either see if the chip would work or make a simple program to count milliseconds, seconds and leave all the longer intervals out of the code.

Any advice would be appreciated.

Phil Pilgrim (PhiPi)
12-05-2006, 05:50 AM
I'm a little confused about what you want to do. Are you trying to measure the time between events (inputs), or create the events (outputs) with 10ms resolution timing?


12-05-2006, 06:07 AM
Thanks for your response. To clarify, I want to measure time between 2 input events.

Our experiment is dropping a marble and measuring how long it takes to fall different distances. The trigger for the clock start is a microswitch on the end of a solenoid. The solenoid is activated by a pushbutton, the marble sits on top of the solenoid rod, the rod is retracted, the marble starts its fall and the end of the solenoid rod makes momentary contact with the microswitch. This event triggers the clock start. When the marble falls for a meter or whatever length is chosen, it breaks a light beam shining at a photoresistor. This event tells the clock to stop. Everything seems to work except it just doesnt seem right to play around with the timing until the "loop clock" gives the calculated value. We need a real clock.

Phil Pilgrim (PhiPi)
12-05-2006, 06:23 AM
The method you describe using seems completely legit, and using PULSOUT to calibrate the loop is downright clever! You describe wanting a "real clock". I think you've just created one!


Peter Verkaik
12-05-2006, 08:24 AM
How about a simple RC circuit to count time in defined units

············· VCC=5V
··············· ·|
·············· ·-+-
··············· |·|
··············· |·| R
··············· -+-
················ |
dischargePin· o--+
················ |
················ = C
················ |
················ - 0V

The C will discharge to some minimal voltage Um when dischargePin is made a low output.
The C will charge via R·when dischargePin is made an input. The threshold voltage is 1.4V

The time T for charging C from Um to 1.4V is given by
T = R*C*ln((VCC-Um)/(VCC-1.4))
Um=0.0V then T = 0.328504*R*C
Um=0.1V then T = 0.308301*R*C
Um=0.2V then T = 0.287682*R*C
basically T = k*R*C

example program
loopcount var word 'number of loops
chargecount var word 'number of charges

low dischargePin 'discharge C
· if clockinPin=1 then waitforstart
· input dischargePin
· if dischargePin = 0 then L1
· low dischargePin
· chargecount = chargecount+1
· input dischargePin
· loopcount = loopcount+1
· if clockinPin = 0 then L0
'at this point clock is stopped
'time passed is chargecount*T

There is of course some inaccuracy.
The looptime must be significantly shorter than T
for example looptime is T/100
The quotient chargecount/loopcount is a measure for the inaccuracy.
The trick is to find a T that is suitable that will give a large
enough loopcount to keep the inaccuracy low.
Once you have an R and C that gives a large enough loopcount,
you can calibrate for k (which will differ from the calculated k)

regards peter

Peter Verkaik
12-05-2006, 01:47 PM
You could do without the RC circuitry
by just counting

· if clockinPin=1 then waitforstart
· loopcount = loopcount+1
· if clockinPin = 0 then L0
'at this point clock is stopped
'time passed is loopcount*T

where T is the time for a single pass through the loop.
To calibrate:
apply a low going pulse to clockinPin which has a known
low period time and note the loopcount.
The looptime T then equals lowperiod/loopcount

You can generate an accurate pulse with a 2nd stamp.

regards peter

12-08-2006, 07:55 AM
Thanks for your advice Peter.

I am an electronics rookie and learned a lot by reading your RC post and playing around with some different values.

Actually, I am approaching the problem in a different way now. I really wanted to do the timing without using the stamp so I took apart an electronic stop watch and soldered some leads on the start, stop, reset buttons. Essentially, all I have to do is momentarily connect these leads to start and stop. Even this is causing me some problems though. All I want the Stamp to do is to send a signal to a "switch" that will start or stop the clock. I need an electronic switch, not a mechanical relay. Since the clock has its own power supply, I need to have it electrically isolated from the Stamp.

My first thought was a transistor but my application seems turned around from my understanding of transistors. I want to switch relatively low power to the clock using relatively high power from the Stamp Out Pin. Do you have any experience with a bilateral switch? or any other ideas?

Thanks again for your time.


12-08-2006, 09:01 PM
Hi Ted,

I think you're onto something with the electronic stop-watch.

Use the contacts of the microswitch to start the clock, use a phototransistor with your light beam to stop to the clock and then just have a manual push button to reset. There's no real need for the stamp unless you want to drive a more sophisticated display or something like that.

My only suggestion would be to use a light beam and phototransistor instead of the microswitch to start the clock, too. That way, any delay by the action of the circuitry is identical on the start and finish.


Tom Sisk


12-08-2006, 10:06 PM
A small relay should be able to do a 'button press'. If you get a 5-volt coil 'reed-relay', it may even take so little current you could drive it from the BS2 directly. If not, add a "Darlington Array" chip and you should be good to go -- LM28003? something like that.

And you COULD go with something like the: http://www.rhombus-tek.com/co-processors.html

The "Simple Multi-Tasking" chip gives you a settable square wave -- set it to 10 mSec, and the BS2 can do the 'hard real-time' you're looking for.· It gives you a buffered recieve-only UART, too.

Post Edited (allanlane5) : 12/8/2006 3:10:10 PM GMT

12-22-2006, 12:21 PM
I am looking for info on this subject too.

I am working on designing a "speedtrap" for RC cars, with plans on making a full "dragstrip" timer/light setup down the line

I have found info on making a simple "stopwatch" using a 555 and counter chips.

if you take that idea and a 16 or 24-bit counter, pipe in a known timed pulse and have the stamp give it a start and stop signal. then all you need to do is have the stamp read out the counter data for elasped time

you may be able to use the BS2s internal count function but I dont know if you can start and stop it with inputs or do you have to wait for the duration set to end.

I haven't purchased a stamp yet so I am not sure how flexable the count function is.