Ultrasonic transmitter and receiver using arduino uno

Hi!
I’m doing my project regarding 40khz ultrasonic sensor which will be used to get accurate distance measurements. I have a separate transmitter and receiver transducer. I want to get the time between the moment my transmitter transmitted the waves and the time my receiver received it. However, I couldn’t actually make it work. Attached below is the code I’m using.

I’m sending pulses of 40khz waves through the transmitter whose pin is connected to pin 10, then the receiver which is connected to analog pin 0 will measure the voltage peak of the wave it received which is assumed to correspond to the duration the wave has travelled. This code I used is from Mr. Kerry Wong’s blog (A Sensitive DIY Ultrasonic Range Sensor – Kerry D. Wong). However, i’m not quite sure if the code is right since i’m not getting accurate results. Can anyone help me? I would really appreciate that. Thank you in advance.

sensor.ino (1.35 KB)

Did you build the required transmitter and receiver circuitry (described in the blog article), and have you verified that it is working correctly?

Note that similar circuitry is incorporated into the HC-SR04 modules, which you can buy for about $1 on eBay. They work quite well.

What hardware are you using?

For on off control, you can easily accomplish this.
Suggest you look at the basic examples that come with the IDE.

Yes SIr, I build the same circuit with modification in the receiver part since his work focuses on a 24khz sensor while mine is 40 khz. The transmitter side is working but I cannot detect any echo in the receiver side.

Did you change the frequency response of the receiver to peak at 40 kHz? If so, please post the modified receiver circuit diagram.

It would be helpful to verify receiver operation with an oscilloscope, as shown in this video.

Assuming that is correct, I took a closer look at your code, and there are some serious problems.

The transmit transducer is turned on for far too long by these lines:

  startTransducer(40000.0, 0.5);
  delayMicroseconds(1000000);
  stopTransducer();

Commercial ultrasonic rangers typically send about 8-16 cycles of 40 kHz oscillation, which is 200 to 400 microseconds (not 10^6, as you are doing).

In one second, the initial 40 kHz wavefront has traveled 330-340 meters. By the time you turn off the transducer, the room is hopelessly full of echos.

Threads merged to provide continuity from where you've been, where you are, and where you are headed. Good luck @Jenifer3096.

Your transmitter outputs ~8volt peak/peak.
A HC-SR04 module (with a range of only a few meters) outputs close to 50volt peak/peak.
What sort of range are you expecting.
Leo..

I'm so sorry Sir, I don't know why my posts emerged.

Disregard any posts, here’s the final.

Attached is the transmitter and receiver circuit I used, I changed the encircled arameters in the receiver side just like this (since the center frequency of my sensor is 40 khz):

6.8n, 1.2k → 1n 4k
0.1u, 1.2k → 0.1u 4k

and here’s the final code I used:

/*
* A Sensitive DIY Ultrasonic Range Sensor
* http://www.kerrywong.com
*/
void delayMilliseconds(int ms) {
  for (int i = 0; i < ms; i++) {
    delayMicroseconds(1000);
  }
}

void stopTransducer()
{
  cli();
  TCCR1B = 0;
  sei();
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
}

void startTransducer(float freq, float dutyCycle)
{
  if (dutyCycle > 0.5) dutyCycle = 0.5;
  else if (dutyCycle < 0) dutyCycle = 0;

  cli();
  TCCR1B = _BV(WGM13) | _BV(CS10) | _BV(ICNC1);
  //f0 = fclk / (2 * N * Top)
  long topv = (long) ((float) F_CPU /(freq * 2.0 * 1.0));
  ICR1 = topv;


  OCR1A = (int) ((float) topv * dutyCycle);
  OCR1B = (int) ((float) topv * (1 - dutyCycle)); 
  DDRB |= _BV(PORTB1) | _BV(PORTB2);
  TCCR1A = _BV(COM1A1) | _BV(COM1B1); 
  sei();   
}

void setup()
{
  Serial.begin(9600);

  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
}

byte a = 0;
unsigned long t_start = 0;
unsigned long t_peak = 0;
unsigned long t = 0;
byte v_peak = 0;
int x;
const float SPEED_OF_SOUND_20C = 0.0003432; //per micro-second
float d = 0;

void loop()
{  
  startTransducer(40000.0, 0.5);
  delayMicroseconds(200);
  stopTransducer();

  v_peak = 0;
  t_start =micros();
  t_peak = t_start;
  delayMilliseconds(1);

  for (int i = 0; i < 256; i++) {
    x = analogRead(A0);
    t = micros();

    if (a > v_peak) {
      t_peak = t;
      v_peak = a;
    }
  }

  t = t_peak - t_start;
  d = (float) t * SPEED_OF_SOUND_20C / 2.0;
  Serial.println(d , 2);
}

But coudln’t make it work. The transmitter side works btw, the receivver side, im not sure. What could be done?

@wawa my target range would be from 2m-4m. I cannot use a commercially available ultrasonic sensor. I opt to make a sensor by myself.

Hi,
How do you know the transmitter is working?
OPs RX circuit;
6c01783088e06f2a2dc2118c3921288f020b0058.jpg
Tx circuit;
a25824a5b2f0b1da358d67594a8325f3c976b544.jpg

Do you have access to an oscilloscope?

Thanks… Tom… :slight_smile:

Hi,
What are your ultrasonic sensors?

Tom... :slight_smile:

Jenifer3096:
Hi!
I'm doing my project regarding 40khz ultrasonic sensor which will be used to get accurate distance measurements.

How accurate do you want the measurements?
What is the target?
Is there any other possible targets around it in the near or far field to it?
I hate to say you may be disappointed with how "accurate" and the repeatability of your project will be unless you have complete control of the measuring environment.
Tom.... :slight_smile:

Yes, I have an oscilloscope with me, so I know that indeed, the transmitter is working When I connect the probe on the receiver, it doesn't show any echo. It's as if it doesn't detect any reflected waves

I don't know the brand of the ultrasonic sensor I bought here in the Philippines Sir. All I know is that it's center frequency is 40 khz. And for the accurate measurement, may be a deviation of +-1cm would be alright.

I'm concerned with the latter part of my code Sir. What I'm doing is getting the highest voltage peak of the received waves then get the time corresponding to that.

Hi,
Can you measure the DC voltages on the points in this Rx diagram please, with respect to gnd with no signal being Tx.
ReceiverEdit.jpg
With the Tx pointing at the Rx what do you see on the scope at point G when you fire the Tx?

What voltage scale/cm do you have the scope set too and do you have it in AC input mode?

Can you please post a picture of your project please?

Thanks… Tom… :slight_smile:

Jenifer3096:
I don't know the brand of the ultrasonic sensor I bought here in the Philippines Sir. All I know is that it's center frequency is 40 khz. And for the accurate measurement, may be a deviation of +-1cm would be alright.

I'm concerned with the latter part of my code Sir. What I'm doing is getting the highest voltage peak of the received waves then get the time corresponding to that.

I would not be worrying about code until you can verify that the hardware is working.
Tom... :slight_smile:

I'm sorry, I'll get back to you regarding the DC voltage at that points. I just disassembled my receiver circuit now since I'm not sure if the resistor and capacitor values I used are right. Thanks Tom

Hi,
Can you show what scope pattern you get at points A and B with respect to gnd please?
UltrasonicTransmitterEdit.jpg
Thanks… Tom… :slight_smile:

Have you done anything about the problem with your code, that I discussed in reply #4 (the 1 second transmit ensures that ranging won't work).

Also, the receiver amplifier is just a low pass filter with a 3 dB point of about 40 kHz.

A bandpass filter, like the used in the HC-SR04 will work much better. However, the commercial circuit should be corrected, as described here.