Ideas for measuring model railway loco speed


Senior Member
Using an M2, I want to measure loco speeds.
I have two IR sensors (TX/RX) in small packages mounted in the track 12 inches apart.

So, 1 Foot Per Sec = 0.6818 MPH, for N Scale multiply by 148 gives me scale speed. Should be easy enough.

Thinking of using the 'pulsin' command to measure time between IR's. The number of 10uS units will give me a time.
1st IR has to start pulsin.
2nd IR has to stop the count.
Was thinking of a simple S/R logic gate latch to use with pulsin ('srlatch' doesn't make any sense to me).

I seem to be having a bit of a 'senior moment' presently & can't seem to get my head around the best way to achieve what I want.

Anybody any better ideas, or has it been done before.

Last edited:


Well-known member
Hi Neil, one of the first things I learned about using “Pulsin” is that it times the duration of a pulse, Not the time between pulses. It can handle a pulse which is high for the duration, or one that is low, which ever you require.

Its big advantage is that it is great for really short pulses, but there is a limit to long duration pulses before it times out. Depending on the speed of your locomotive, the sensors may have to be much closer together, but that is not a problem for pulsin. Worth exploring the maths on how long a duration causes pulsin to time out and give a zero result. But your detectors may only have to be inches apart, or less for the required accuracy.

To pursue your approach, it might be necessary to use some external components to make the two sensors work together to make a value first high then low, or vice versa. There are 74 series chips that will do this, look for various forms of J-k flip-flop, which have two inputs, one trips the out put high and the other trips it low. Possibly some 4000 series chips that also do this. Then the flip flop output can be used as the input to your Picaxe to be timed by pulsin, calculate the speed and output to either your computer or to the Picaxe serial display.

I hope that helps get you moving in a useful direction. It’s great fun developing a concept that will achieve these tasks.



Senior Member

PULSIN will Time Out after about 650ms and, as said, measures between the positive and negative edges of a pulse on a single pin, so you would need some external hardware. No, I've never found the internal S-R Latch to be of any use with PICaxe.

For sensors 12 inches apart, the timed period is presumably going to be a second or more. Also, to calculate a speed, you need to DIVIDE by the time taken (the longer the time, the slower the speed) and the "best" practical divisor is only a few hundred with PICaxe Basic, so you may need to measure in, say, 10 ms units. The easiest way to do that it is a simple Program "counting" loop: the program loops until the first input changes and then repeatedly tests the second input, incrementing a counter (variable) each time until there is a change in the second input.

You may need to calibrate the loop with a test, e.g. a manual period of 10 or 100 seconds, but a good starting point for a 10ms increment would be something like: gap = 0 : DO : gap = gap + 1 : PAUSE 7 : LOOP UNTIL inputpin2 = 1 (because the loop instructions will take a few ms to execute). For better accuracy of shorter time periods, you might use a higher SETFREQ , or trim the value of a PAUSEUS 700 instead of the PAUSE.

Cheers, Alan.


Senior Member
Gents, thanks for the thoughts.

Yes, I was going to use either 74HC or 4000 series CMOS gates for the latch. That's fairly easy for me (my original background was TTL logic).
Forgot about the pulsin max duration of 650ms, good point. I haven't drilled the holes for the sensors yet, 12 inches was just a first thought.
It's looking like the counting loop may be the best way to go.

I wouldn't be suprised If I don't end up doing most of it in TTL Logic - I have still got 'tens' of chips in the draws, just may use the PICAXE for the display, or end up using one of the many 74C912's I still have (used loads of them in the past - very easy to use & even used one with a PICAXE once).

It maybe a PICAXE is not the best solution for what I want to do, but thought it may save time, perhaps not.

Accuracy need only be approximate, 10% error wouldn't be too much of a concern.

Think I'm going to have to experiment and do some testing.



My speedometer wagon could be another option rather than a fixed device:


Senior Member
What an interesting idea.
Not sure I can fit that into a 'N' guage wagon though, it's half the size of yours.
But I will study the code to see how you did it.