Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Ultrasonic Sensor Troubles

Status
Not open for further replies.

gstavrev

New Member
Hi,
I am trying to get an ultrasonic sensor to work. I bought, and this is the data sheet: https://www.electro-tech-online.com/custompdfs/2008/09/139492.pdf

I have followed this example, mainly for the schematic :
**broken link removed**

Nothing seems to be working. I don't have the 22 uF capacitor. Is it critical? I have 10 uF, maybe that will do the trick? I also don't have the inductor. Could that be critical?

I am using a pic18f4525 with C18 compiler.

Does anyone have any suggestions or resources where I can try different schematics?

Thanks,
George
 
Make 20 uF from a pair of 10 uF in parallel. That will be close enough.

The circuit will not work without the inductor.
 
Thank you. I am not familiar with inductors and Radioshack doesn't seem to have it. Are there any alternatives, perhaps other circuits?
 
Thank you. I am not familiar with inductors and Radioshack doesn't seem to have it. Are there any alternatives, perhaps other circuits?

Look at this link:
Its got a number of inductor winding calculators, you could wind your own.:)

**broken link removed**
 
So I managed to get an inductor, and I think I have the right pulse or maybe not ( I can hear a very high pitch, but very its quiet. I am having trouble with the reciever. I am still using the same schematic here. I don't know what to expect. A voltage drop from the receiver that goes up and down depending on distance?

Thanks.
 
So I managed to get an inductor, and I think I have the right pulse or maybe not ( I can hear a very high pitch, but very its quiet. I am having trouble with the reciever. I am still using the same schematic here. I don't know what to expect. A voltage drop from the receiver that goes up and down depending on distance?

Thanks.

hi,
Looking at that simple receiver, all you will see is a change in the amplitude of the received pulse.
The amplitude of the pulse will not only depend upon the 'distance' but also the reflecting surface.

I wouldnt say the circuit is a range finder, just a ultrasonic receiver.:)

What are you trying to do with this project.?
 
hi,
Looking at that simple receiver, all you will see is a change in the amplitude of the received pulse.
The amplitude of the pulse will not only depend upon the 'distance' but also the reflecting surface.

I wouldnt say the circuit is a range finder, just a ultrasonic receiver.:)

What are you trying to do with this project.?

Hi Eric,

That circuit is just a snippet from the project linked to in the first post on this thread. That should provide a usable ultrasonic rangefinder, no? It's broadly similar to my 8051 rangefinder (must get that finished sometime so I can post it) which in turn was based partly on EPE magazine's ultrasonic tape measure project.


Torben
 
Hi Eric,

That circuit is just a snippet from the project linked to in the first post on this thread. That should provide a usable ultrasonic rangefinder, no? It's broadly similar to my 8051 rangefinder (must get that finished sometime so I can post it) which in turn was based partly on EPE magazine's ultrasonic tape measure project.


Torben

hi Thanks,
I used his link named ' here' to find the circuit which I thought he was using.???

Perhaps George will confirm which bit he has built.:confused:
 

Attachments

  • 090-ULTRASONIC-RANGER-schematic.png
    090-ULTRASONIC-RANGER-schematic.png
    9.5 KB · Views: 188
Last edited:
hi Thanks,
I used his link named ' here' to find the circuit which I thought he was using.???

Perhaps George will confirm which bit he has built.:confused:

I think maybe he's using that as the receiver stage. Or else just testing that part of it right now. You're right though--it's not completely clear.


Torben
 
I think maybe he's using that as the receiver stage. Or else just testing that part of it right now. You're right though--it's not completely clear.


Torben

George says he just sees the amplitude going up/down, which is what we would expect.
 
George says he just sees the amplitude going up/down, which is what we would expect.

Hmmm....that's not how I read his last post:

gstavrev said:
So I managed to get an inductor, and I think I have the right pulse or maybe not ( I can hear a very high pitch, but very its quiet. I am having trouble with the reciever. I am still using the same schematic here. I don't know what to expect. A voltage drop from the receiver that goes up and down depending on distance?

Thanks.

I took that to mean that he can hear the sound--which means it's nowhere near the 40kHz that the circuit description calls for.

I think the last two sentences are saying that he wants to know whether he should expect to see a voltage drop which goes up and down with distance--not that he's seeing that, but I think he's asking whether he should or not.

I would think that if the receiver is working well for input into a PIC that the receiver should act like a comparator--either it's receiving a reflected pulse or not--and that the voltage should be either HI or LO correspondingly. He should of course see the voltage change when a pulse is received, but the amplitude of the change shouldn't depend on the distance to the target.

Of course I'm only describing how I think the receiver input section of a time-of-flight rangefinder should work, not necessarily how the posted receiver circuit works. :) I used a high-pass filter on a pair of op-amp amplifiers to do the job. More parts but at least I can understand more or less what it's doing. :)

Mind you, I've only built one unit, and that was on a breadboard. No experience with real sonars.


Torben
 
Last edited:
I'm sorry I haven't been clear enough. I am trying to build a distance sensor for a robot. I am using a pic18 (which I also have limited experience with). Using the simulator and the timer in MPLAB, I got the pulse to have a period of 25µs. (1 / 40 000 = 0.000025). The sound is very hard to detect.

I have build the whole circuit in the schematic, receiver and transmitter.

I think the last two sentences are saying that he wants to know whether he should expect to see a voltage drop which goes up and down with distance--not that he's seeing that, but I think he's asking whether he should or not.
Torben

Torben's diagnostic at my question is correct (sorry for being unclear). I don't know what to expect from the receiver. Is the detection done entirely in software? Using a multimeter, I don't see any voltage drop over the receiver, and also no voltage drop between ground and the pin I'll be using on the Pic.

I have chosen this particular circuit among the others online because it doesn't use and of the logic gates.

Here is my simple code:
Code:
void sonar(void)   // using the 8Mhz internal Oscillator for PIC18f4525
{
  TRISD = 0;
  while (1)
  {
    PORTD = 128; // turn on RD7
    Nop(); // repeats 23 times total
    PORTD = 0; // turn off RD7
    Nop(); // repeats 22 times total;
  }
}

Thanks for your patience.
 
Last edited:
I'm sorry I haven't been clear enough. I am trying to build a distance sensor for a robot. I am using a pic18 (which I also have limited experience with). Using the simulator and the timer in MPLAB, I got the pulse to have a period of 25µs. (1 / 40 000 = 0.000025). The sound is very hard to detect.

Hi George,

Unless you're a bat, being able to hear the sound at all means it's nowhere near 40kHz. Human hearing tops out at 20kHz.

Torben's diagnostic at my question is correct (sorry for being unclear). I don't know what to expect from the receiver. Is the detection done entirely in software? Using a multimeter, I don't see any voltage drop over the receiver, and also no voltage drop between ground and the pin I'll be using on the Pic.

A multimeter is not fast enough to show you what you want to see on those lines--you'd need an oscilloscope.

I have chosen this particular circuit among the others online because it doesn't use and of the logic gates.

Here is my simple code:
Code:
void sonar(void)   // using the 8Mhz internal Oscillator for PIC18f4525
{
  TRISD = 0;
  while (1)
  {
    PORTD = 128; // turn on RD7
    Nop(); // repeats 23 times total
    PORTD = 0; // turn off RD7
    Nop(); // repeats 22 times total;
  }
}

Thanks for your patience.

Can you post the entire source? I'd like to know how Nop(); is defined. How can it possibly be repeating 23 times the first time it is called but only 22 the second time? Is it using globals or statics to maintain some kind of state?


Torben
 
I'm using Nop() just to delay. It must be implemented in the C18 library. This is my code:

HTML:
#include <p18cxxx.h>

#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config OSC=INTIO67 // select the internal oscillator

void sonar(void)   // using the 8Mhz internal Oscillator for PIC18f4525
{
  TRISD = 0;
  while (1)
  {
    PORTD = 128; // turn on RD7
    Nop();
    Nop();
    Nop();
    Nop();  < -- // ... repeats 23 times in actual code
    ...
    PORTD = 0; // turn off RD7
    Nop(); 
    Nop();
    Nop();
    Nop();  < -- // repeats 22 times total in actual code
    ...
  }
}

void main(void)
{
  sonar();
}

The first is 23 and the last is 22 just so I can get the timing close enough to 25µs per period. I suppose I have a timing issue? I'll check out the timer modules in the PIC.
 
Last edited:
I'm using Nop() just to delay. It must be implemented in the C18 library. This is my code:

HTML:
#include <p18cxxx.h>

#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config OSC=INTIO67 // select the internal oscillator

void sonar(void)   // using the 8Mhz internal Oscillator for PIC18f4525
{
  TRISD = 0;
  while (1)
  {
    PORTD = 128; // turn on RD7
    Nop();
    Nop();
    Nop();
    Nop();  < -- // ... repeats 23 times in actual code
    ...
    PORTD = 0; // turn off RD7
    Nop(); 
    Nop();
    Nop();
    Nop();  < -- // repeats 22 times total in actual code
    ...
  }
}

void main(void)
{
  sonar();
}

The first is 23 and the last is 22 just so I can get the timing close enough to 25µs per period. I suppose I have a timing issue? I'll check out the timer modules in the PIC.

Ah, gotcha. I'm afraid I don't have experience with PICs yet (I'm waiting for some parts to come in before I can finish my PIC programmer/development board) so someone else will have to confirm your timer setup. However, I believe that most PIC18s have built-in PWM which would handle the tone generation for you--googling something along the lines of "C18 PWM" should get you lots of information. This tool looks like it could be helpful too: https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html

Also, there is an example of using the hardware PWM in the source code on the page where this project is hosted. It uses the hardware PWM to generate the 40kHz tone and then uses a simple delay to send out 300ms blips of 40kHz sound. After sending a blip, it waits for a bit to see if the state of the pin being fed by the receiver section changes. The time between the blip being sent and the receiver getting the reflection back indicates the distance to the target.

Do you have access to a scope which you can use to test your outputs and inputs? I used a bat detector to hear the blips from my rangefinder. I don't suppose you just happen to have one of those lying around? ;)


Torben
 
Also, there is an example of using the hardware PWM in the source code on the page where this project is hosted. It uses the hardware PWM to generate the 40kHz tone and then uses a simple delay to send out 300ms blips of 40kHz sound. After sending a blip, it waits for a bit to see if the state of the pin being fed by the receiver section changes. The time between the blip being sent and the receiver getting the reflection back indicates the distance to the target.
Torben

Thanks! I have the concept now. I will try checking the status of the input pin. Would the reciever change states if the waves are not exactly 40khz, but maybe 20khz, or 39khz?

Thanks
 
Thanks! I have the concept now. I will try checking the status of the input pin. Would the reciever change states if the waves are not exactly 40khz, but maybe 20khz, or 39khz?

Thanks

39kHz should be fine. 20kHz, no way. The frequency response peaks very sharply at 40kHz and falls off on either side--20kHz will be way too low for it to give you a decent response.


Torben
 
I have managed to get the PWM module in the pic working. I just don't know how correct it is. This is supposed to generate a 40khz wave:

Code:
void sensor(void)
{
	char inputVal;  // ADC input Value
	char lc;   // loop counter
	char message[17] = "Sonar Detected!"; 	// detected message
	TRISAbits.TRISA0 = 1;	// set as input

	while(1)
	{
		lc = 20;
		OpenTimer2( TIMER_INT_OFF & T2_PS_1_1 & T2_POaST_1_1);	// initalize timer
		OpenPWM2 (50); // RC2 = 50 for 40khz wave
		SetDCPWM2 (50);	// set duty
		delay(30); // delay (.6 milli seconds)
		ClosePWM2(); // close the pulse
	
		ADCON0 = 0x81;	// set the ADC config bits
		ADCON1 = 0xCE;

		while(lc > 0)	// loop until too late
		{
			ADCON0bits.GO = 1;	// ask input
			while(ADCON0bits.DONE == 1);	// wait until donw
			inputVal = ADRESH;	// get the value
			printnum(inputVal);	// display the value
			delay(1);	// delay
			lc--;
		}
		delay(10000);	// put enough pause to see between pulses
	}
}

OpenTimer and OpenPWM functions are from the C18 library. The parameter for OpenPWM is calculated with this calculator. (third one down, for timer2).

My receiver however is not moving at all. Once its turned on, the ADC gets - 84mV and its like nothing gets detected. Could someone look over my code? I will try and get an amplifier from radioshack and see if that helps.

Thanks,

-George
 
Last edited:
Hi George,

Can you post your entire code please? I am not a PIC expert (as I think I've said) but I don't see where delay() is defined and so I cannot guess what the delay times you are using actually work out to.

It is possible that delay() is provided by C18 but I don't see it in the C18 software library documentation.

You are using the full circuit as described in the project link from your first post, right?


Torben
 
Yes, I am using the full circuit. Exactly as shown in the schematic, with identical resistors. My inductor looks different than the picture provided, but it has the same 330µH specification.

My full code is a bit long, but here is the delay function. I don't remember if I wrote it myself or got it from somewhere else. When 1000 is passed as a parameter, it delays for about 1 second.

Code:
void delay (int num)
{
  int i;
  for (i = 0; i < num; i++)
    ;
}

I also have another delay functions I got off a book, but I don't use it in the sensor function.

Code:
void MSDelay(unsigned int itime) // MS delay
{
	unsigned int i, j;
	for(i = 0; i < itime; i++)
		for ( j = 0; j < 135; j++);
}

If it matters, here are my configuration bits and includes:

Code:
#include <p18cxxx.h>
#include <string.h>
#include <pwm.h>
#include <timers.h>

#define ldata PORTD			// PORTD = LCD data pins
#define rs PORTCbits.RC1	//rs = PORTB.0
#define rw PORTCbits.RC2	//rw = PORTB.1
#define en PORTCbits.RC3	//en = PORTB.2
#define inbit PORTCbits.RC0 // for button
#define sinp PORTAbits.RA0 // for sonar

#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config OSC=INTIO67
#pragma config CCP2MX = PORTBE

Thanks again.

Maybe I should post this in the PIC code category?
 
Status
Not open for further replies.

Latest threads

Back
Top