Welcome to the Parallax Discussion Forums, sign-up to participate.

# Measure the speed of electricity...

Posts: 7,992
edited 2012-02-06 - 03:56:54
I have been inspired by "Laser Developer" and have made a DMTD (Dual Mixer Time Difference) measurement system using two propeller boards.

One board is simply a generator. It generates two 1MHz square waves that are 90 degrees (250nSec) out of phase.

These two signals are fed into a second propeller board that will sample the two signals at 1MHz. But because the two boards have seperate crystals there will be (should be) a small frequency difference between the two boards. In my setup the difference between the two was 19ppM (19Hz @ 1MHz). There MUST be some difference between the two for the system to work.

First the two signals are interconnected using short wires of the same length. The system can then be zeroed by pressing a key in PST. Then one of the short wires is replaced with a long wire to be measured. The system will measure the actual phase delay caused by the time it takes the signal to travel the length of the wire being measured.

I have hacked this up in PropBasic, but I want to re-write it in spin to make it more generic for Propeller users.

I've tried this on two different setups (one at work and one at home) and it seems to work. I used a 19 foot piece of wire and the system said it was 22 ft (but I don't think electricty flows at exactly 1 ft per nSec).

If you want to try this out, I used P0 and P1 for the signals (don't forget to connect the grounds together too).

Let me know if you try this. If there is enough interested I'll do a little write-up for it.

P.S. If you prefer there is a spin version posted further down.

Bean
«1

• Posts: 1,695
edited 2011-11-01 - 18:54:25
Cool experiment. The speed of a signal through a wire will be less than the speed of light, so your "longer" result would be expected.
I believe the difference in signal speed vs. speed of light is called Velocity Factor. It would be a good experiment to try coax vs. a single conductor as the VF of coax is usually pretty significant, on the order of .8 or more.

It is amazing to see some of the folks on here like you that are good at putting ideas into practice.

C.W.
• Posts: 7,992
edited 2011-11-01 - 19:31:41
C.W.
Thanks, yeah I guess I should have made the units "nSec" instead of "ft".
It would be a great experiment to measure the velocity factors of different cables.

Bean
• Posts: 5,770
edited 2011-11-01 - 23:26:24
Bean, this is a great project! I can see some apps for it in the Big Brain from array to array or coded up could examine the connections from props. It could be a great diagnostic tool and a benchmark to measure parts of the hardware connections and various interfacing.
• Posts: 21,213
edited 2011-11-01 - 23:47:04
Wow, for a while now I have been pondering on how to measure the speed of light. Why? Well just because I think it's something I should be able to do. We it back in uni with big and complicated arrangement of a laser and rotating octagonal mirror. Seemed to me that with todays MCU's laser diodes and such we should be able to do it much more directly.
Perhaps you have the solution there. Just run the signal through a laser/photo diode set up.
Anyway the speed of signals through wires might be close enough for me:)
• Posts: 77
edited 2011-11-02 - 03:33:07
Great experiment, Bean!
My friend works as a director/professor at our German standars lab PTB where they are doing very high precision speed & time measurement.
If you look in their publications there are lots of very interesting things to be found. But more to the normal user environment, I would suggest to use
GPS stabilized oszillators for such experiments, because standard quarz oszilators are not very good in terms of jitter & stability.
http://www.jrmiller.demon.co.uk/projects/ministd/frqstd.htm
Cheers, Fried
• Posts: 4,312
edited 2011-11-02 - 05:09:47
The numbers are registering speed of electricity through the medium, so there should be different reading for say copper and aluminum wire?

What does DMTD stand for? Google comes up with noise...
• Posts: 2,025
edited 2011-11-02 - 05:43:07
And the speed of light from Switzerland to Italy is different ...
• Posts: 7,992
edited 2011-11-02 - 05:55:42
Here is the spin versions. The DMTD reciever has some notes at the bottom to explain how the measurements are done.

DMTD is Dual Mixer Time Difference

The reading are pretty jittery, but using long wires of CU and AL should show some difference in speed.

Bean
```  Wagon Wheels & Stobe lights -
Have you ever seen in old movies when a wild west wagon is moving and the wheels seem to be turning very slowly (or even backwards) ?
This is because the movie is a series of still picture frames taken at 24 frames per second.
If the spokes in the wheel rotate just enough so that a spoke is in almost the same place as a different spoke in the previous frame,
it will look like the spoke barely moved at all from one frame to the next.
The same effect can be seen by using a strobe light and a fan. By adjusting the speed of the strobe and/or fan, you can make the fan
appear to stop or move very slowly.

Aliasing & Downconverting -
Well the same thing can be done for digital signals. This is called "Aliasing".  Anytime you sample a signal at a frequency less than
twice the frequency of the signal itself, you will get aliasing.
For example if you have a squarewave at 1MHz and you sample it at 1,000,100 it will appear to be toggling at only 100Hz (the difference
between the two frequencies). The interesting thing is that the duty cycle will be correct. If the 1MHz signal has a duty cycle of
30% then the 100Hz signal seen by sampling it will also have a duty cycle of 30%. This process of converting a frequency to a lower
frequency is called "Downconverting".

Time Expansion -
It could be said that by downconverting the fast signal to a much slower rate (but keeping the waveform shape) is expanding time.
We took a signal that lasted only 1uSec and transformed it into an identical signal that lasts for 10mSec (10,000 times longer).
Of course we haven't REALLY expanded time because the signals need to be repeating.

Measuring the speed of electricity -
We can use this time expansion to accurately measure a phase error too. If we take TWO 1MHz signals that have a phase error, and sample
them at the same time we get two signals that are the same duty-cycle and have the same phase error (in degrees) as the originals.
So if the use two signals with 90 degrees of phase error (250nSec @ 1MHz), when we measure the phase error in the downconverted signals
the phase error will still be 90 degrees, but 90 degrees of 100Hz is 2.5mSec.
We can measure this 2.5mSec time very accurately by counting the samples. By introducing an additional phase change (like using a
longer piece of wire for one of the two signals) this phase change can be expanded and measured. Since the phase change is expanded
by 10,000 times, a 1nSec (about 1 foot of wire) change at the inputs will result in a 10uSec change at the outputs.

Propeller implementation -
Since we cannot get EXACTLY 100Hz of difference just by using the propeller boards, we will take what we get by chance.
No two crystals will be exactly 5MHz. And between any two propeller boards there will most likely be a significant enough variance
that we will get reasonable result. However if you don't, try a different board. The crystals may be too close or too far apart
to get good measurements.

```
• Posts: 4,312
edited 2011-11-02 - 06:17:17
Could the signals be made less jittery by using say precision oscillators? Not that I know anything about them, its just a term I've seen.
If the signal was not so jittery, could something approaching the accuracy of Laser Developer's chip be made with a couple props?

Measuring very fast events using a relatively slow clock seems like kind of cool thing that might be useful. I love this stuff!
• Posts: 1,283
edited 2011-11-02 - 06:20:32
Bean, Could this be made into a TDR? That could be a nice hand held cable tester.
Jim
• Posts: 7,992
edited 2011-11-02 - 06:44:31
Could the signals be made less jittery by using say precision oscillators? Not that I know anything about them, its just a term I've seen.
If the signal was not so jittery, could something approaching the accuracy of Laser Developer's chip be made with a couple props?
Measuring very fast events using a relatively slow clock seems like kind of cool thing that might be useful. I love this stuff!

Yes, if you had two oscillators (TCXOs) that where about 10ppM apart (or VCTCXOs) that would be ideal.

Bean
• Posts: 182
edited 2011-11-02 - 08:05:25
Bean, Could this be made into a TDR? That could be a nice hand held cable tester.

Exactly my thoughts. Now I'm interested because I have been thinking of an el'cheapo homebrew TDR for a while. I have tried a couple ideas out, but I could never get it to work accurately enough to be field-usable. If I ever get the non-exsistant spare time, I would love to set this up and see it in action. *glares menacingly at the HUGE stack of paperwork on my desk*
• Posts: 870
edited 2011-11-02 - 08:46:33
Have you tried this code running at 5MHz with the PLL disabled? That might make the base clock signal cleaner.

Lawson
micro-power experiments with the propeller.
Drivers for TAOS TSL3301 line sensor Forum thread, and OBEX
Lumen Electronic Jewelery Website
My AWD motorcycle Website and action video
What I'm paid to work on. UW Lidar Group.
FME, a Spin-only floating point library with trig, exponential, and logarithm functions. OBEX and Forum.
• Posts: 7,992
edited 2011-11-02 - 10:02:21
If you average consecutive readings the values are a lot more stable.
I used 16 averaged together and the results varied only by a couple tenths of a nSec.

Bean
• Posts: 4,312
edited 2011-11-03 - 06:00:18
By TDR do you mean http://en.wikipedia.org/wiki/Time-domain_reflectometer ?

These undefined SF-TLA's make me crazy!
• Posts: 182
edited 2011-11-03 - 06:00:52
Been doing some heavy thinking on this, and how TDR's work. Wouldn't it be possible to use this as a base program where you enter in the known length of the cable, it does, say, 20 consecutive measurements to overcome the inconsistencies Bean noticed, average them out, and compares that data to the known cable length to output the fault location? The biggest problem I can think of is you would need a good storage medium for it, and compile lots of data on the cables you test to find an an average signal propagation speed of known good samples. There are a lot of factors to think about, and I would think that any number of them can bring about WILD inaccuracies... everything from cable and crystal temperature fluxuations to power supply spikes. I would think It would have to be one heck of a good design in order to have any kind of reliability.

And yeah, Prof_Braino. Thaat's exactly what I'm talking about.
• Posts: 4,312
edited 2011-11-03 - 06:15:04
The laser time of flight unit that Bean referenced as inspiration uses dozens to hundreds of samples to get accurate reading.
The fluctuations are small in the short term, so these average out. I think that you would want to limit sample taken to a single measurement session, as the large drift occure over the long term, in that case you would be measure the long term drift of the crystal and other parts of the system.

I would think one could store reading in EEPROM and transfer to PC later, or just store directly to SD.

The point of interest in this application would be the reflected signal, and the chanllenge would be to fliter out the reflection from other noise, is that correct?

This is a cool application, and might turn out to be useful!
• Posts: 182
edited 2011-11-03 - 06:33:29
Yeah your real measurements are taken from the time it takes the reflected signal to come back to you, with regard to cable length. the filtering would have to be VERY precice because minor intermittant failures on the professional models can and will show up as smaller peaks on the o'scope, as opposed to full breaks that are quite visible. I imagine it would be tough to know the difference in a lot of cases.
I would think one could store reading in EEPROM and transfer to PC later, or just store directly to SD.

When I was talking about storage, I was not implying the cable under test, I was meaning some sort of database with the average signal propegation for your stock of cables. Like for our purposes here, say 14ga wire made of copper would have a very different signal propegation than 30ga. nickel plated wire, and that would have to factor in to your calculations. I am by no means a math whiz, and would have to call in some big guns to help out with this, but I can't help but think it would work... but.. to repeat myself, accuracy would be my biggest fear. High-dollar TDR setups can detect faults down to the centimeter over miles and miles of cable, and the already inherant errors Bean saw is the biggest turn-off. High precision xtals would help, but not an end-all solution to it.

C'mon Bean. I know you are interested in this, where's your \$0.02?
• Posts: 7,992
edited 2011-11-03 - 07:24:45
The number of samples is quite high. If you are downconverting to 100Hz you would need 10,000 (1,000,000 / 100) samples.

I tried it without the PLL (5 MHz instead of 80 MHz) and it didn't seem to make much difference.

Here is the latest version of the receiver with averaging and better edge handling. Before I started measuring after any jitter, now I start measuring at the first edge regardless of how much jitter comes after it.

Bean
```' DMTD receiver for use with DMTD transmitter
' Receives two 1MHz square waves on P0 and P1
' The waveforms are normally offset by 90 degrees (250nSec)
' To create a phase shift, replace one of the wires on P0 or P1 with a longer wire.
'  If you get a negative phase shift time, use the other wire.
'
' NOTES:
'   This method relies on a small frequency difference between the clock of the transmitter
'     and the receiver. If you are having trouble, try a different board for one or the other.
'
' see further information at bottom of this file...
'
CON
_ClkMode = XTAL1 + PLL16X
_XInFreq =   5000000

OBJ
pst : "Parallax Serial Terminal" ' Serial to PC
f   : "Float32"                  ' IEEE-754 32-bit Floating Point Math
fp  : "FloatString"              ' Format 32-bit Floating Point Value for display

VAR
LONG newValue
LONG value
LONG sum
LONG offset
LONG divisor

PUB __Program
pst.Start(115200)             ' For PC communication
f.Start                       ' Start the floating point math library
fp.SetPositiveChr(" ")        ' Don't show '+' for positive numbers

value := -1                   ' No value measured yet
offset := 0                   ' No offset set yet
waitcnt(80_000_000 + cnt)     ' Wait 1 seconds for things to settle
CogNew(@Init, @newValue)      ' Start measurement cog

repeat                        ' Repeat forever
repeat 10                   ' Average 10 readings
repeat until newValue > -1  ' Wait for a new measurement
if ina[31] == 0           '  While waiting, see if user press a key to set new offset
offset := 0             '  If they DID press a key, set offset to zero (not set)

value := newValue           ' Get measurement value
newValue := -1              ' Set value to no measurement (so we don't read it again)
if offset == 0              ' If offset is NOT set
offset := value           '   Use this value for the offset
divisor := f.fdiv(f.ffloat(value), 250.0) ' Calculate nSec divisor

value-=offset               ' Subtract offset from value
sum+=value                  ' Accumulate sum of values

sum:= sum / 10                ' Compute average of sum
sum:=f.fdiv(f.ffloat(sum), divisor) ' Divide to get nSec
pst.str(fp.FloatToString(sum))' Display value
pst.str(string(" nSec"))      ' Display units
pst.char(13)                  ' Display one measurement per line

DAT
' PASM routine to measure the time expanded phase difference
' Store measurement in "newValue"
'
org           0
Init              mov           time,cnt           ' Setup initial waitcnt value cnt+511

WaitPhase         waitcnt       time,#80           ' Sync 1MHz (80 counts @ 80MHz)
and           temp,#3            ' Mask off bits we are interested in
cmps          temp,#2 WZ, WC     ' Are we in phase 2 ?
IF_NE         jmp           #WaitPhase         '   No, jump back
Loop              waitcnt       time,#80           ' Sync 1MHz (80 counts @ 80MHz)
and           temp,#3            ' Mask off bits we are interested in
cmps          temp,#1 WZ, WC     ' Are we in phase 1 ?
IF_NE         jmp           #Loop              '   No, jump back
mov           _value,#0          '   Yes, zero loop counter
Wait              waitcnt       time,#80           ' Sync 1MHz (80 counts @ 80MHz)
and           temp,#3            ' Mask off bits we are interested in
cmps          temp,#1 WZ, WC     ' Are we in phase 1 ?
IF_E          jmp           #Wait              '   Yes, keep waiting (and counting)
cmps          _value,#100 WZ, WC '   No, is the value =< 100 ?
IF_BE         jmp           #Wait              '      Yes, Loop back (jitter in edges)
mov           temp,par           '      No, write value to HUB so spin code can read it
wrlong        _value,temp
jmp           #WaitPhase         ' Repeat forever

In0            LONG 1 << 0
In1            LONG 1 << 1

time           RES 1
temp           RES 1
_value         RES 1

{{
Wagon Wheels & Stobe lights -
Have you ever seen in old movies when a wild west wagon is moving and the wheels seem to be turning very slowly (or even backwards) ?
This is because the movie is a series of still picture frames taken at 24 frames per second.
If the spokes in the wheel rotate just enough so that a spoke is in almost the same place as a different spoke in the previous frame,
it will look like the spoke barely moved at all from one frame to the next.
The same effect can be seen by using a strobe light and a fan. By adjusting the speed of the strobe and/or fan, you can make the fan
appear to stop or move very slowly.

Aliasing & Downconverting -
Well the same thing can be done for digital signals. This is called "Aliasing".  Anytime you sample a signal at a frequency less than
twice the frequency of the signal itself, you will get aliasing.
For example if you have a squarewave at 1MHz and you sample it at 1,000,100 it will appear to be toggling at only 100Hz (the difference
between the two frequencies). The interesting thing is that the duty cycle will be correct. If the 1MHz signal has a duty cycle of
30% then the 100Hz signal seen by sampling it will also have a duty cycle of 30%. This process of converting a frequency to a lower
frequency is called "Downconverting".

Time Expansion -
It could be said that by downconverting the fast signal to a much slower rate (but keeping the waveform shape) is expanding time.
We took a signal that lasted only 1uSec and transformed it into an identical signal that lasts for 10mSec (10,000 times longer).
Of course we haven't REALLY expanded time because the signals need to be repeating.

Measuring the speed of electricity -
We can use this time expansion to accurately measure a phase error too. If we take TWO 1MHz signals that have a phase error, and sample
them at the same time we get two signals that are the same duty-cycle and have the same phase error (in degrees) as the originals.
So if the use two signals with 90 degrees of phase error (250nSec @ 1MHz), when we measure the phase error in the downconverted signals
the phase error will still be 90 degrees, but 90 degrees of 100Hz is 2.5mSec.
We can measure this 2.5mSec time very accurately by counting the samples. By introducing an additional phase change (like using a
longer piece of wire for one of the two signals) this phase change can be expanded and measured. Since the phase change is expanded
by 10,000 times, a 1nSec (about 1 foot of wire) change at the inputs will result in a 10uSec change at the outputs.

Propeller implementation -
Since we cannot get EXACTLY 100Hz of difference just by using the propeller boards, we will take what we get by chance.
No two crystals will be exactly 5MHz. And between any two propeller boards there will most likely be a significant enough variance
that we will get reasonable result. However if you don't, try a different board. The crystals may be too close or too far apart
to get good measurements.
}}
```
• Posts: 4,312
edited 2011-11-03 - 08:34:20
@Bean - Can I use your explanation of down-converting and time expansion in the DS00VQ100 documentation?
• Posts: 7,992
edited 2011-11-03 - 08:58:28
@Prof_Braino - Sure you may use it as you like. You may modify it also if you wish. I don't claim it to be 100% accurate though...

Bean
• Posts: 5,770
edited 2011-11-03 - 09:09:38
I originally measured the speed of light using the planet Jupiter and its moons.

http://Galileo.phys.Virginia.EDU/classes/109N/lectures/spedlite.html

It could be helpful as a baseline reference..
• Posts: 7,992
edited 2011-11-07 - 14:08:21
I am working on making this work on a single Propeller.
So far it looks good, and the readings are more stable.

I should have something to post tomorrow, but right now it appears able to have about 50 picoSecond resolution. But I just got it working a couple minutes ago, so that may change.

This will be quite a nice demo of the capabilities of the Propeller chip.

Stay tuned...
Bean
• Posts: 5,482
edited 2011-11-07 - 16:49:08
Humanoido post #23 - that is an excellent series of lectures - thankyou!
Answers: 1) A quadcopter. 2) Very high. 3) The internet. 4) A lot. 5) No.
• Posts: 13,612
edited 2011-11-07 - 17:27:33
Bean wrote: »
I am working on making this work on a single Propeller.
So far it looks good, and the readings are more stable.

Interesting, how do you get around the need for 'all timeslot sampling', also known as two non-locked frequencies ?

'NOTES:
' This method relies on a small frequency difference between the clock of the transmitter
' and the receiver. If you are having trouble, try a different board for one or the other.
'
"
• Posts: 7,992
edited 2011-11-07 - 18:31:31
jmg,
I'm trying to use the video generator, but it's not working too well.
Initially I was getting nice steady values, but they seem to jump large amounts. I guess because even though the video generator is creating a different frequency it is still phase locked to the crystal.
I'm going to try dithering the video frequency and see if that helps.

Bean
• Posts: 13,612
edited 2011-11-07 - 22:11:06
Bean wrote: »
jmg,
I'm trying to use the video generator, but it's not working too well.
Initially I was getting nice steady values, but they seem to jump large amounts. I guess because even though the video generator is creating a different frequency it is still phase locked to the crystal.
I'm going to try dithering the video frequency and see if that helps.

Why not add a simple device like a 74HC4060/LV4060, which has an oscillator and divider ?
Two crystals are always going to give a better result, if you want fractions of a ns averaging.
• Posts: 7,992
edited 2011-11-10 - 05:15:54
Well the single board experiment is a bust. Doesn't look like it is possible.
Oh well. I tried....

Bean
• Posts: 955
edited 2011-11-10 - 08:55:48
Just a bit of nit pick here:
Technically the velocity of electrons in the wire move very slowly.
Something like 1/2" per hour.
It's the wave through them that moves fast.
Nit pick off:
Duane
• Posts: 62
edited 2011-11-10 - 14:59:26
The electrons bounce and flow really fast all the time randomly throughout the wire (because the metal is above absolute zero) - their velocity is pretty fast. A chunk of copper has bazillions of electrons flowing all around between all the different atoms while it's just sitting there, but they only start to have a NET velocity or "drift" when we apply a voltage. It's the net drift of these electrons (under the influence of an electric field) from one point to another that is very slow. The net drift is slow because they are constantly bouncing all around in a random fashion - they don't have any sort of straight shot path in the direction they are being influenced before they slam and bounce into another atom (in contrast to something like a vacuum tube in which electrons are pretty much unimpeded). It's really the electric field we are observing which propagates quickly through a circuit. The electrons spend most of their time rapidly bouncing around (rapid velocity), but their net velocity is a slow drift through the circuit.
Kevin McCullough
Applications Engineer, Parallax Semiconductor
(916) 632-4664 x 5512
kmccullough@parallaxsemiconductor.com
http://www.parallaxsemiconductor.com
Support: http://support.parallaxsemiconductor.com

Direct Office: (916) 624-3012
Skype: kmcculla10