I found this design somewhere on the internet so I take no credit for it.
In my testing I made several modifications to increase accuracy etc, but it was a few years ago so I can't remember exactly what.
I remember it worked very well and had better accuracy at short range than the one in my above post, although it had a max range of about 1.5m.
See attachments for code and schematic.
----------------
Ultrasonic Rangefinder
This circuit uses a PICAXE-08 as the basis of a simple ultrasonic rangefinder which has a resolution of 1.7mm and a maximum practical range of about 120mm.
A novel approach is used to interface the ultrasonic receiver to the PICAXE, in theory eliminating the need for any amplification, rectification, filtering or threshold detection that would typically be employed.
The PICAXE detects a signal from the ultrasonic receiver (RX1) on input 1 (pin 6). Being a digital input, at least 2V (nominal) must be applied to this pin before it will be read as a logic "high".
Pedestal voltage
In order to detect a much smaller signal level than this from the receiver, a DC "pedestal" voltage is applied to the pin, lifting it up to a value just below the logic high threshold. In this condition, even a small signal from the receiver is sufficient to exceed the threshold voltage and thus be detected by the PICAXE.
The pedestal voltage is provided by capacitor C1, which is connected in series with the receiver. The voltage on C1 is initialised at the start of every measurement cycle using the aforementioned port pin and a simple software sequence.
First, the capacitor is charged until the voltage applied to pin 6 reads "high". This is achieved by alternately making pin 6 a high output, which charges C1 via D1 and the 100kO and 1MO resistors, and making it an input to read its logic state.
Next, the capacitor is discharged to the point where the input samples "low" 30 consecutive times, thereby minimising the chance that noise (either electrical or acoustic) will cause false triggering. This is achieved by alternately making pin 6 a low output, which discharges C1 via the 1MO resistor, and making it an input to read its logic state.
Once initialisation is complete, pin 6 is set as an input, ready to receive a signal from RX1. Loading of the receiver signal is kept to a minimum by the very high impedance of the digital input. In addition, any voltage produced across its terminals is too small to forward-bias D1, so the 100kO resistor is effectively out of circuit.
Amplification
Whilst in theory not required for this circuit, I have included an LM741 Operational Amplifier. The receiver is connected between the two input pins. The receiver is quite selective, so when a 40kHz signal is picked up, the small voltage generated by the receiver is amplified around 1000 times by this system. The output is then fed into the pedestal capacitor (C1) via a 0.02μF coupling capacitor.
By doing this, the range of the system is increased considerably to around 300mm. In addition, it gives more reliable operation in that a flat surface is not required for a signal to be received on its return.
Transmitter
The ultrasonic frequency required for the transmit signal is generated by a 555 timer configured as a 40kHz free-running oscillator. The frequency of oscillation is set by the 10kO resistors and 1nF capacitor connected to pins 2 & 6. It drives the ultrasonic transmitter via a 100nF coupling capacitor.
The reset input (pin 4) of IC3 is controlled by a second 555 timer. This timer is configured as a monostable, producing a 300µs wide "transmit enable" pulse as determined by the 1nF capacitor and 270kO resistor.
A low-going pulse on pin 3 of the PICAXE triggers the monostable, generating a 300µs transmit burst. The trigger pulse is delayed by about 5ms by an RC network. This is required because the PICAXE takes a small amount of time from the finishing of running one command to the beginning of running another. The delay makes sure that the PICAXE has run the ‘pulsin’ command before the burst of ultrasound is fired.
The 5ms delay works as follows: The PICAXE output on pin 3 is normally high, holding the 47nF capacitor (C2) charged via the 270O resistor. In turn, the 4.7nF capacitor (C3) is charged via the 2.7MO resistor. This holds the trigger input of IC2 high.
The PICAXE sends a transmit burst by briefly taking pin 3 low, then setting it as an input. This discharges capacitor C2 but does not hold it discharged. C3 then slowly discharges into C2 via the 2.7MO resistor.
After about 5ms, the voltage on C3 falls below the 1/3 Vcc threshold of IC2’s input (pin 2) and triggers it, generating a high output on pin 3 for about 300µs. Diodes D2 and D3 are included to ensure that IC2 is not retriggered. They charge C2 and C3 while the output of IC2 is high, taking the trigger input of IC2 high.
The transmit enable signal at pin 3 of IC2 is fed back to the PICAXE receiver input on pin 6 via the negative end of C1. The voltage divider formed by the 10kO and 100kO resistors raise the negative end of C1 to about 0.5V during the transmit burst. This is sufficient to raise pin 6 above the logic high threshold voltage.
After the PICAXE initiates the transmit burst, it uses the pulsin command to measure the length of the negative-going pulse at pin 6, with a resolution of 10µs. This pulse is formed from the trailing edge of the transmit enable pulse and the rising edge which shows reception of the first echo. There is no attempt to check that the receiver is receiving a 40kHz signal but as the transducer is highly selective, this method is quite reliable.
When the pulsin command completes, the PICAXE again makes pin 3 a high output to charge the two capacitors, ready for the next transmit burst.
Second burst
The PICAXE now sends a second transmit burst for the sole purpose of measuring its length. The time from the pulsin command is then added to the first measurement to correct for the length of the transmit burst. The result is then multiplied by a constant to convert it into millimetres and saved in the distance variable.