Ultrasonic Lag Timing

testerrrs

New Member
I'm using this circuit shown on this page: http://www.leang.com/robotics/info/articles/minison/minison.html.

With the Ultrasonic timing diagram looking like this:


I would like to attach this to a PICAXE so I can determine the "Time Of Flight", therefore allowing me to create a distance sensor.

Seeing as the echo line stays high, and drops low on an echo (as shown in the diagram), how would I go about doing this on a PICAXE?

Thanks in advance,
Jon
 

hippy

Technical Support
Staff member
A forum search should reveal previous discussion on this. The problem for the PICAXe is that it has no easy mechanism to measure a period up to an event occuring, so in this case having initiated a pulse there's no way to accurately tell how long it was before the echo arrived back.

The two primary options are to use two PICAXE's or other hardware so a pulse can be generated which is present between initiating the pulse and the echo coming back which can be measured with PULSIN, or to configure an internal timer so it can be started, the pulse measured and the elapsed time when the pulse is detected determined. The timing method can however also be quite inaccurate because of the time it takes to execute PICAXE commands so there can be considerable latency in recognising, responding to the echo and in reading the timer. Also, moving any of the code around can mess up any previous calibration.

The best bet is two PICAXE's. The main PICAXE tells the second to initiate a pulse and then imediately enters a PULSIN. The second delays long enough for the first to have started its PULSIN and then actually initiates the pulse. You'll also need some additional circuitry to create a suitable pulse for the main PICAXE to see.

The easiest option is to use an ultrasound module which already does all that and is 'PICAXE friendly'. The Daventech SRF04 module is one such possibility; check the Rev-Ed SRF004.PDF for details of using it with a PICAXE.
 

testerrrs

New Member
You'll also need some additional circuitry to create a suitable pulse for the main PICAXE to see.
PICAXE 1 sends a command to PICAXE 2 and a HIGH signal to the PING line simultaneously. Then PICAXE 2 sends pin X high, then returns it to low when the ECHO line goes low. Pin X is attached to an input on PICAXE 1.

Then PICAXE 1 uses pulsin to measure the time between pin X going HIGH and it dropping back to low.

Would that work?

Thanks for the reply,

Jon
 

hippy

Technical Support
Staff member
@ geforce : Would that work ? Yes and no.

You'll get the PULSIN the master PICAXE needs but you'll be measuring a pulse generated by the slave PICAXE which will have the inaccuracy of timing caused by the slave waiting for the echo. You did make me think of a possible solution though ...

Assuming the echo pulse is positive going, the master PICAXE tells the slave to trigger and then immediately does a PULSIN for a low-going pulse. The way that will work is the PICAXE waits for the line to go high, then waits for it to go low and times how long it is to the next going high ( the echo pulse ).

The slave has an output which is diode mixed with the echo pulse, normally the slave keeps that output high. When told to trigger, the slave sends the activation pulse then drops its output line. The slave also monitors the echo pulse, and once it has been seen ( and after a short delay ) sets its output to the master PICAXE back high again.

The master code would be -
Code:
Do
  Toggle 1                      ' Output 1 to slave
  PulsIn 2,0,w0                 ' Input 2 from diode mix
  Gosub ProcessW0Value
Loop
The Slave Code would be -
Code:
 High 2                          ' Output 2 to master
bit1 = pin1                     ' Input 1 from master 
Do
  Do : while pin1 = bit1        ' Wait for input signal to change
  bit1 = pin1                   ' Remember signal state
  PulsOut 3,20                  ' 200uS pulse on Output 3
  Low 2                         ' Start the low pulse
  Do : while pin3 = 0           ' Wait for echo pulse
  Pause 1                       ' Short delay
  High 2                        ' Low pulse finished
Loop
The only concern is that the echo pulse is long enough for the slave to recognise. That could be ensured by using a capacitor charged through a resistor. Alternatively, have the slave wait for the master to toggle its line again, which would actually be better ( I'm not editing the code though ! ) and would also avoid the slave locking up if no echo were received back.

All untested so there may be some tweaking required.
Code:
                    _______________________________________
Master Out     ____|
                      ___
Trigger        ______|   |_________________________________
               ____________                            ____
Slave Out                  |__________________________|
                                                   _    _
Echo Pulse     ......_____________________________| |__| |_ 
               ____________                        _   ____
Master In                  |______________________| |_|
PulsIn times               |<-------------------->|
 
Last edited:

testerrrs

New Member
Thanks for that, most helpful :)

I'll certainly play around with that way and see how it goes. The only problem is that the ECHO line is HIGH by default, and drops LOW when an echo is recieved.

In the meantime, I've had some thoughts about the "dual PICAXE" method I posted above. I understand what you mean about the inaccuracy of PICAXE 2 having to wait before beginning its pulse. However, I may have solved that.



- PIC 1 OUT is the pulse sent to the trigger pin on the US system, the resulting emission shown as PING.
- PICAXE 1 simultaneously sends a pin high (PIC 1 => PIC 2), which PICAXE 2 picks up.
- PICAXE 2 waits 1ms (pause 1) which is 1000uS. Then it sends a pin high.
- PICAXE 1 sees this high pin and begins its pulsin.
- When the ECHO line drops LOW, PICAXE 2 drops its output also to LOW.

- The length of the pulse received by PICAXE 1 is shown as MEASURED RESULT.
- As you can see from the timing diagram in the first post of this thread, the TRUE result (Time of Flight) is measured from the END of the PING to the BEGINNING of the ECHO.
- Pulsin measures in 10uS units, so if we add 1000uS to the MEASURED RESULT (for the PICAXE 2 delay), and then take away 200uS (the length of the trigger pulse), we should have the TRUE RESULT (shown as TRUE RESULT).

Therefore:
TRUE RESULT = MEASURED RESULT + 800uS.

I hope that's reasonably clear! And thanks again for the reply and help so far.

Jon
 

hippy

Technical Support
Staff member
When the ECHO line drops LOW, PICAXE 2 drops its output also to LOW.

This is where the timing issue is. It's not that it won't work, but that the time between the PICAXE seeing the line go low and passing that on to the master is variable and quite considerable -

Code:
CheckForEchoPulse:
  If pin1 = 1 Then CheckForEchoPulse
  Low 2
The echo pulse could go low just before the IF is executed so the PICAXE will quickly see it and set its output low, but if it goes low just after the IF starts executing the PICAXE has to finish the IF, execute the GOTO, and only then will it set the output low.

PICAXE commands are incredibly slow relative to this application and the output could go low some 250uS to 500uS after the echo pulse occurs, and you wouldn't know how long the delay was. You cannot therefore simply subtract a constant to correct the timing. The best you can hope for is a time measured ( assuming my values are right, I haven't checked reality ) which could be out by 250uS, and once you convert that to a distance that's quite a considerable inaccuracy; 4cm. If you can live with that inaccuracy you'll be okay. Running at 8MHz will reduce reduce the inaccuarcy by half.
 

testerrrs

New Member
Aah I understand, sorry!

So if I run both master and slave PICAXEs at 8mhz, that would give me an inaccuracy of around 2cm, correct?

That's good enough for my usage.

Also, apart from that slight inaccuracy, should the system I described above work?

Jon
 
Last edited:

testerrrs

New Member
Thanks :)

I will indeed get started building the thing and see what happens.

I had another thought on this. Instead of PICAXE 2 sending a line HIGH, it sends an output to a thyristor HIGH. The thyristor's output is attached to an input on PICAXE 1. The thyristor stays latched even after PICAXE 2's output has gone LOW again. So whilst the thyristor is latched, PICAXE 1 is doing a "pulsin" and counting the length of the pulse.

Now, instead of PICAXE 2 watching the echo line and sending a line LOW when the ECHO line drops low, the ECHO line is attached to the thyristor and resets it. That way there is no delay between the ECHO line dropping LOW and the input to PICAXE 1 dropping low.

What do you think?

Cheers for the help,
Jon
 

hippy

Technical Support
Staff member
It may work, but I'd go back to my earlier idea of diode mixing, and swap the diodes to face the other way and invert the logic -- I think that's possible.
 

boriz

Senior Member
Might be better to use just one PICAXE and have it &#8216;gate&#8217; an external oscillator. Like &#8211; gate output high, 200 uS delay, gate output low, then pulsein.
 

testerrrs

New Member
@hippy: OK :) I'm not familiar with the concept of diode mixing. What is it and how does it work?

@boriz: Thanks for the input. I don't quite understand what you mean though, could you possibly explain a bit further?

Sorry for the ignorance!

Jon
 

hippy

Technical Support
Staff member
Diode mixing is using simple diodes to create logic gates. If you take two diodes and join the pointy-ends together, you can apply a positive voltage to either and the output will be a positive voltage when that's done to either, an OR gate. A pull-down resistor also attached to the cathodes keeps the signal at 0V when no input is provided.

The diodes would be driven by PICAXE or other outputs. As the diodes only allow the current to flow in the pointing direction, two outputs can be joined this way with no risk of damage should one be high when the other is low.

If the diodes are switched the other way and a pull-up to +V used, the mixed output will only be low when one or both of the outputs go low, that is, it is only high when both inputs are high, an AND gate.
 

boriz

Senior Member
Geforce.

As has been suggested, it would be better to purchase a ready made U/S ranging module. These are cheap and designed to be easily interfaced with microcontrollers. Have a look here

By ‘gated’ I simply mean that your TX oscillator is external and is turned on and off by the PICAXE. You will also need an RX circuit, tuned to the appropriate frequency, that can provide the PICAXE with a logic level change upon receiving a reflected signal.
 

Rickharris

Senior Member
Thanks :)

I will indeed get started building the thing and see what happens.

I had another thought on this. Instead of PICAXE 2 sending a line HIGH, it sends an output to a thyristor HIGH. The thyristor's output is attached to an input on PICAXE 1. The thyristor stays latched even after PICAXE 2's output has gone LOW again. So whilst the thyristor is latched, PICAXE 1 is doing a "pulsin" and counting the length of the pulse.

Now, instead of PICAXE 2 watching the echo line and sending a line LOW when the ECHO line drops low, the ECHO line is attached to the thyristor and resets it. That way there is no delay between the ECHO line dropping LOW and the input to PICAXE 1 dropping low.

What do you think?

Cheers for the help,
Jon

AFAIK a thyristor can only be reset by dropping it's supply voltage once it has been switched on. You may need to use a relay or similar to do that. - At low voltage/current a transistor may well do it OR another thyristor placed across the first to short it out and so reset.
 

BeanieBots

Moderator
Thyristors will only switch off when their current is reduced below a threshold level.

I don't want to dampen your enthusiasm with this project but there are a lot of bits missing.
Correct me if I'm wrong but I get the impression that you have two utrasonic transducers and are planning on connecting them direct to a PICAXE.
That won't work.
The sender requires a pulse stream rather than just a single pulse.
The receiver will be very low amplidude and will require amplifcation.
To avoid problems with other 'sounds', the receiver needs to be 'tuned' to the ultrasonic frequency you are using. For long ranges(>1m), the amplification of the receiver is usually made to increase as a function of flight time to pick up the ever decreasing reflected pulse stream.

To get extra drive into the sender, a bridge style driver is often used such as the MAX232 to give +/-12v (24vAC) from a 5v supply. These cause noise and must be suppressed when the sensitive receiver electronics are enabled.

As already mentioned by several others, I STRONGLY suggest the use of a ready made control module such as the SRF-04 or similar. Without a 'scope and very sound electonics knowledge you will be in for a lot of frustration trying to build your own.
 

testerrrs

New Member
Thanks for all the replies.

@BeanieBots: No I'm not planning that as I know it won't work. I'm using this circuit here:


A 200uS pulse to the 555 emitter (looking at doing this with a PICAXE) will send the stream of pulses. The reciever using a PLL Tone Decoder to lock the signal onto 40kHz, and an op-amp is used to amplify the signal. The second op-amp is used to help minimise false triggering.

I do realise pre-built modules can be bought, but I would like to have a go at getting this working from the ground up. I do also have access to a 'scope, and units to measure frequency which I can just attach to the 555 & pin 5 of the PLLDC to set the frequencies accurately to 40kHz.

-----

So I'll scrap the thyristor idea for the moment, and have a look at dropping the input to PICAXE 1 -> LOW using the diode mixing technique.

Again, thanks for all the help, and any other help/advice or other input is very much appreciated.

Jon
 

BeanieBots

Moderator
@ Dippy, yes the SRF02 looks VERY interesting. It's only been around for a short while and I've not had the pleasure of trying one yet. Got a project that requires 8 sensors so it would be much cheaper and smaller than using 08s.

@geforce. OK, fine. You had me worried for a while but looks like you know what you're doing. Of course, a lot also depends on if the project is to build a sensor or a build a project that requires a sensor;)
Have fun.
 

testerrrs

New Member
Thanks :) For the moment the project is just to build a distance sensor that can measure distances as accurately as possible. From my calculations, running the master/slave PICAXE config at 8mhz will still mean I can't measure distances less than ~10cm, because a "pause 1" is the smallest pause you can do on a PICAXE.

However, measuring between 10cm and ~1m is fine for what I need to do.

Jon
 

BeanieBots

Moderator
Even the 'readymades' don't work well < 10cm and 10m is a realistic upper limit.
Not sure how accurate you want to get but temperture and air movement are the limiting factors with US sensing. Never seen a sensor that takes either of those into account but I guess it could be done quite easily.
 

Dippy

Moderator
I saw that circuit on Kam Leang's site too.

Have you got a spec for that U/S t/ducer part number 13-334?
I couldn't find that part on Hosfelt's site.
I totally respect 555 circuits.. but I'm uncomfortable about stability when used with cheapo Rs and Cs and pots. So be choosy.

Anyway, keep up the good work.
 

testerrrs

New Member
@BB: Yep I agree completely. As I've said, the project is to build just the sensor, so it does not need to be a certain accuracy. But as you can see, I'd like try and get it as accurate as possible. With the master/slave config that hippy suggested, he said that will inaccurate by ~2cm if I run the PICAXEs at 8mhz, which is perfectly good enough.

@Dippy: Not sure on the spec. This is for an AS-level coursework project so I'll start off by using some 40kHz US transducers that I already have. Unfortunately I don't have access to them at the moment so I can't find out part numbers/manufacturers etc. I'll post that information here when I have it though, as much for my own reference as anything else.

I'm uncomfortable about stability when used with cheapo Rs and Cs and pots
Last year I built a simple 40kHz US transducer driver using a 555 timer, with literally a PTM to switch it on and off. The Rs and Cs were nothing special, but I found that using a good quality pot helped a massive amount. I spent quite a while with the thing attached to timing devices and oscilloscopes, and eventually found a combination of Rs/Cs/pots which gave me a reliably accurate 40kHz output.

Point very much taken and agreed with with regards to quality of components though.

Jon
 

testerrrs

New Member
Ah thanks Dippy :) Very useful document.

I'm currently waiting for my LM567 PLLTD to arrive, then I can get building this thing and see how it goes.

On another tangent, does anyone know of a Serial or I2C "timer chip" that will interface with a PICAXE that will do this timing thing correctly?

Ie.
- Input of timer chip goes high
- Timer starts counting
- Input goes low
- Timer stops counting
- Timer IC sends the data via Serial/I2C to the PICAXE.

If something like that existed, it would make this project a lot easier! Would eliminate the need for all the timing malarkey that, unfortunately, the PICAXEs themselves aren't too good at.

Jon
 

boriz

Senior Member
I put ‘elapsed time counter ic’ into Google and the first link was this. Seems to work the way you describe except it count’s in one second intervals. The clock oscillator uses an external crystal, so you might be able run it faster by using a different crystal. Don’t know what the limits are. Maybe you could just use a couple of chained CMOS digital counter ICs clocked by your 40Khz 555 output.
 

testerrrs

New Member
Still waiting for the PLLTD to arrive. In the meantime, this is the timing solution I have come up with:



The pulsin command is set with "state 0" on PICAXE 2, ie. a high->low transition must occur for the timing to begin.

- A PTM switch connected to both PICAXEs is pressed.
- PICAXE 1 sends the 200uS pulse to the 555.
- PICAXE 2 enters the pulsin (pulsin 1,0,w1)
- The high->low transition on the SIGNAL line at the end of the 200uS pulse starts PICAXE 2's timing.
- ECHO is HIGH by default, meaning the line into the PICAXE from ECHO is held low by the transistor.
- When an echo is received, the line drops LOW. The input into the PICAXE (SIGNAL line) therefore goes HIGH (dragged up to +5v by the pull-up resistor) and PICAXE 2 stops timing.

The pulsin length then on PICAXE 2 will be the time of flight.

Jon
 
Top