I don't have the money to buy Ping))) sensors (It ain't cheap here) but think I found a simple solution by using two piezo buzzers as sender and receiver with the "tone(pin,40000,5);" at 40khz like the original Ping))) for 5 milliseconds. the part I want is counting the time until receive so I can calculate the distance at 330m/s for sound speed here is my code please someone edit the bottom commented part for me.
/*that sketch needs help with the commented part and it uses
two piezo buzzers each having its cathode to groung and one
anode to digital pin 2 and the anode of the other to digital
pin 3 and it is used as a DIY Ping))) sensor*/
int speed = 330;
int time;
int distance;
int distanceCm;
int outPin = 2;
int inPin = 3;
void setup(){
pinMode(outPin,OUTPUT);
pinMode(inPin,INPUT);
digitalWrite(inPin,LOW);
}
void loop(){
tone(outPin,40000,5);
//count time in ms until inPin is high and store it as "time"
//multiply "time" by "speed" and store it as distance in meters
//multiply "distance" by 1000 and store it to distanceCm in centimeters
//display that on a serial port or utelise it
}
Turn on 40 kHz output for 300 microseconds (a little over 10 pulses).
Wait 600 microseconds.
Start a timer.
Loop until an analog input (from the receiver) goes above 0 or until the timer overflows.
Stop the timer.
Firstly, I doubt there's anybody here on the forum willing to write code for you - but will offer their advice and expertise to help you help yourself to write code, by providing help with specific problems.
So, take a look at the following Arduino page on Ping))) setups. You can pick out simple bits you need - in your code, you wish to output to serial, so you must initialize serial comms using the Serial.begin function.
For things like counting time, check the Arduino example for the 'Blink without delay' example, in there is a snippet of code which counts time.... Please ask specific questions on here, not the classic "I haven't done my homework, please do it for me"
To: johnwasser
Thanks for the advice the link you posted I've visited it previously many times and I only don't know what are the speakers here, Are these buzzers? maybe, can I use buzzers or do I have to use some special transducers?, thanks again.
To: AWOL
Thanks man, do I change it down in the millisecond or the microsecond range and can you please suggest a suitable duration? I didn't know I had to use an amplifier, Can I make one using lm386, lm324 or lm339 ? I'm only worried about the distortion of the amplifier to affect the analog input and the code not having a good threshold, any way thanks.
To: jtw11
Thank you for the reply, I've read the blink without delay before but I didn't understand it, thanks
Murata transducers are cheap and designed for the job.
I really wouldn't advise building U/S sensors from scratch unless you have an oscilloscope.
First choice, buy ready-made units, second choice build published design.
A very poor third is build your own.