# Distance measurement using ultrasonic transceiver

#### Pratyay

Joined Jul 4, 2019
11
Hello, I am working on a project of finding distance using ultrasonic transceivers. I am going to use an arduino as a microcontroller. I will be designing the transmitter and receiver circuits, whose links I have given below. So i have two specific doubts and i will be very thankful if you can spare some time and answer them:
1) For testing, I had given a burst of 40 kHz of square waveform using a function generator to the transducer and i also had connected the DSO probes in parallel with the transducer pins. But I didn't get a proper echo waveform. Is it because the echo cannot be detected directly and I first need to receive it through the receiver circuit. (The voltage through the Function generator was 9.5 V pk-pk)

2) Is it possible to connect both the transmitter and receiver waveform in parallel with the transducer and switch the circuits using arduino after i get the echo properly?

3) Datasheet of sensor that I am using : (attatched)

#### Attachments

• 657.6 KB Views: 10

#### AlbertHall

Joined Jun 4, 2014
12,020
1. I think you need the gain of the receiver circuit to be able to see the reflections.
2. There are circuits which do this using only one transducer so it is possible.

#### Pratyay

Joined Jul 4, 2019
11
Thank you very much, Ill do the further experiments and keep this thread updated.....

And one more thing, i have made a typing mistake, its 'transmitter and receiver circuits' not 'transmitter and receiver waveforms'. Extremely sorry for that though.

#### ericgibbs

Joined Jan 29, 2010
15,517
hi,
With that TX circuit you will not be able to measure range, it is a Astable 40kHz osc, so the receiver will have no time of flight information.

E

#### BobTPH

Joined Jun 5, 2013
4,900
Sure ot can. You send a short burst then measure elapsed time before the echo.

Bob

#### ericgibbs

Joined Jan 29, 2010
15,517
The circuit diagram the TS has posted does not show any way of burst transmission, that's the point I am bringing to his attention.

#### MrChips

Joined Oct 2, 2009
26,089
Is this a school course project?

#### Ylli

Joined Nov 13, 2015
1,058
Rather than sending a burst and seeing how long it takes to come back, you can use a continuous output frequency sweep method. Sweep the ultrasonic signal over a signal range, and compare the frequency of the echo with the frequency currently being transmitted. Knowing the sweep rate and the frequency difference it is a simple matter to determine the distance.

#### Pratyay

Joined Jul 4, 2019
11
hi,
With that TX circuit you will not be able to measure range, it is a Astable 40kHz osc, so the receiver will have no time of flight information.

E
I
Sure ot can. You send a short burst then measure elapsed time before the echo.

Bob
Yes this is exactly what I was going to do using an arduino. Thank you!

#### Pratyay

Joined Jul 4, 2019
11
The circuit diagram the TS has posted does not show any way of burst transmission, that's the point I am bringing to his attention.
I am going to power the transmitter circuit ON and OFF using an arduino, so that will be acting as a burst signal. Thank you!

#### Pratyay

Joined Jul 4, 2019
11
Is this a school course project?
Rather than sending a burst and seeing how long it takes to come back, you can use a continuous output frequency sweep method. Sweep the ultrasonic signal over a signal range, and compare the frequency of the echo with the frequency currently being transmitted. Knowing the sweep rate and the frequency difference it is a simple matter to determine the distance.
Hello, thank you for your suggestion, it is indeed a nice method, but i have to do it using burst - echo method. Nevertheless, please can you share a link or any file from where I can learn about the frequency sweep method?
Thanks!

#### ericgibbs

Joined Jan 29, 2010
15,517
hi Pratyay,
The transducer datasheet that you posted shows a resonant 40kHz transducer response, refer to page #3, using frequency sweeping will be a problem.

E

Last edited:

#### Alec_t

Joined Sep 17, 2013
12,808
but i have to do it using burst - echo method
So this is a school/college assignment?

#### Pratyay

Joined Jul 4, 2019
11
So this is a school/college assignment?
Not exacty, its an internship project

#### Pratyay

Joined Jul 4, 2019
11
okay so I did as follows:
1) I connected the I/O pin of the sensor to the trigger pin (pin 3) of arduino from which i am giving a burst, also this pin is connected to the ground of receiving circuit.
2) I connected the output of receiving circuit to arduino pin A0.
3) I am giving 5 V to the receiving circuit through arduino pin 6.
4) The ground is common.

The arduino code is as follows:

C:
int trig_pin = 3;
int echo_pin = A0;
int power_pin = 6;

unsigned long duration;
unsigned long distance;
int i;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
pinMode(power_pin, OUTPUT);
}

void loop() {
for(i=0; i<=7; i++){
digitalWrite(trig_pin, LOW);
delayMicroseconds(12.5);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(12.5);
}

digitalWrite(trig_pin, LOW);
digitalWrite(power_pin, HIGH);
//delayMicroseconds(5);

//Measures the response from the echo_pin
duration = pulseIn(echo_pin, HIGH);

//Calculating the distance
distance = duration*0.000343/2;

Serial.print(duration);
Serial.print(" Distance = ");
Serial.println(distance);

digitalWrite(power_pin, LOW);
delay(500);

}
Mod edit: code tags

Please can you go through it and see what is wrong as on the serial monitor i am getting just on value irrespective of the distance from the sensor. Is it because there is some problem with pulseIn received from analog pin? Or is it because i powered the receiving circuit with 5 V instead of 9 V? (Im afraid 9 V might damage my arduino)

Thanks

Last edited by a moderator:

#### ericgibbs

Joined Jan 29, 2010
15,517
hi P.
I see that you are generating 8 pulses of 40kHz.?
Do you have an oscilloscope to check the echo pulse back from a close target.?
E

#### AlbertHall

Joined Jun 4, 2014
12,020
The program expects the receiver to immediately functioning from power on. It includes a 100nF capacitor fed by two 4.7M resistors so that's about 0.25 second time constant. You need to check with an oscilloscope just what is being transmitted and what is received as a result.

#### drc_567

Joined Dec 29, 2008
1,156
... I realize that you are using an Arduino microcontroller. However, in the book listed here:
"The Quintessential PIC Microcontroller"
... by Sid Katzen
There is an example program that does exactly what you are trying to do. The program sends and receives pulses to an ultrasonic disc, and counts the echo return time from an object. (Towards the end of the book.)
... try a library or used book store.

#### Pratyay

Joined Jul 4, 2019
11
Hello,
So there was a problem in the previous receiver circuit, so i have changed the approach. I have found a receiver circuit on the following link: https://www.electronoobs.com/eng_arduino_tut36_sch1.php It has 3 opamps and it amplifies 60 mV sine wave to >3.3 V sine wave.

And now I have incorporated 2 MOSFETS (IRF 630), one on the transmitter pin and another on the receiver pin so that the two circuits can be isolated whenever required. The circuit figures are attatched below.

The arduino code is as follows:

Code:
int echo_pin = 2;
int trig_pin = 3;
int transmitter_power = 9; //to power ON the transmitter circuit
long start_time, stop_time, dur, dist;
int variable = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(echo_pin,INPUT_PULLUP);
pinMode(trig_pin,OUTPUT);
pinMode(transmitter_power,OUTPUT);
attachInterrupt(digitalPinToInterrupt(echo_pin), ISR_function, RISING);

}

void loop() {
// put your main code here, to run repeatedly:
//delayMicroseconds(100);

digitalWrite(transmitter_power, HIGH);
//I've given 9 us delays instead of 12.5 beacause the square waveform eventually
//becomes of 25us because of internal delays of arduino
for(int i=0; i<8; i++){
digitalWrite(trig_pin, LOW);
delayMicroseconds(9);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(9);
}
digitalWrite(transmitter_power, LOW);

start_time=micros();
//Serial.println(start_time);

while(variable == 0){
//empty
}
if (variable == 1){
stop_time = micros();
Serial.print(" ");
//Serial.print(stop_time);
//variable = LOW;
}
dur =stop_time-start_time;
Serial.println(dur);

delayMicroseconds(1000);
//for(;;){};
}

void ISR_function(){
variable = 1;
}
I have also attached the output waveforms observed on a DSO. (Green- connected to 'Echo-in' pin of receiver circuit, Yellow- connected to the sensor pins)

Im still not getting the output properly ( Its showing the 'dur' as 8 and 12 irrespective of the distance from object)

Please can you try to point out the mistake?
Are the receiver and transmitter powering circuits using MOSFETs whose images i have attached correct?
Thank you for sparing your valuable time and helping me!

#### Attachments

• 68.1 KB Views: 13
• 67.3 KB Views: 12
• 99.9 KB Views: 13
Last edited:

#### Alec_t

Joined Sep 17, 2013
12,808
I'm no programmer, but it seems to me that switching on the receiver power could somehow be causing an immediate interrupt, which then sets variable to 1 and stops the timer.
The receiver circuit in the link has no supply decoupling and has very high value input bias resistors, so is prone to interference pick-up. It is possible that it creates a spurious output pulse at switch-on.