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.
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.
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.
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..
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?
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....
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.
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...
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