Ultrasonic frustration

I am trying to use an ultrasonic transmitter/receiver pair to measure distance. But, I am having trouble getting the simple things to work. My transmitter/receiver is found here: Ultrasonic Sensors, model us1640. I am wiring two arduinos—one with the transmitter, one with the receiver.

The transmitter is on pin 3. The sketch is

const int soundPin = 3;

void setup(){
pinMode (soundPin, OUTPUT);
}
void loop(){
tone(soundPin, 40000);
delay(1500);
noTone(soundPin);
delay(3000);
}

A separate Arduino has the receiver on pin 7. The sketch is:

const int soundPin = 7;

void setup(){
Serial.begin(9600);
pinMode (soundPin, INPUT);
}

void loop(){
long duration;
duration = digitalRead(soundPin);
Serial.println(duration);
delay(100);
}

I can’t seem to detect a signal on the receiver.

So, I have two questions. Looking at the picture of the transmitter/receiver which pin in positive? I have tried both. And, can you see anything I am doing wrong?

Thanks in advance.

The part that is grounded to the aluminum case would be the "negative" side, the other pin is insulated from the metal casing. You might not detect anything because you might need to amplify the received signal.

Thanks for the help on the pins. But, as to amplifying you may be correct but right now I have the transmitter and receiver about an inch apart. I can't seem to receive anything.

Try switching to one of the analog pins and see if you get anything doing analogRead(). Even if you get enough voltage, it will only read as a HIGH for a short period of the signal. Try this before switching to analogRead.

void loop(){
  for(x = 0; x < 10000; x++) {
    if(digitalRead(soundPin) == HIGH)
      Serial.println("I heard something");
}

The common devices with these transducers, measure from a single location. They transmit and receive and measure the time it takes to reflect the sound off some object in front of the transducer.

If you want to have a one-way transmission from a transmitter to a receiver at a different location, it seems to me, the practical problem you will run into, is how to measure the time which it takes.

Here is the grand plan. Two arduinos which I will call Unit #1 and Unit #2. Unit #1 has an ultrasonic transmitter and an XBee. Unit #2 has an ultrasonic receiver and an XBee. Unit #1 will send "Starting Sound" on the XBee followed immediately by an ultrasonic signal. Unit #2 will pickup the XBee signal nearly instantly. It will record the time which it gets this signal and start listening for the sound signal. When it gets it it will record stop time. Simple math will lead me to the separation between the two arduinos.

I think the logic is fine but I must be able to pick up the sound which I can't seem to do.

I have tried putting the receiver on analog and digital pins and using analogRead, digitalRead, and PulseIn. the sketch is very simple. I can't figure what I have missed.

Right now I do not have the XBee's hooked up (in an attempt to get this part working). Here is the whole story...

Unit #1—the ultrasonic transmitter is wired to Pin 7 and Ground. The sketch is:

const int sendPin = 7;

void setup(){
pinMode (sendPin, OUTPUT);
}

void loop(){
tone(sendPin, 40000);
delay(100);
noTone(sendPin);
delay(300);
}

Unit #2—the ultrasonic receiver is attached to pin A5 and ground. Here is the sketch:

const int soundPin = A5;

void setup(){
Serial.begin(9600);
pinMode (soundPin, INPUT);
}

void loop(){
long duration;
duration = analogRead(soundPin);
Serial.println(duration);
}

I have also tried this sketch:

const int soundPin = 7;

void setup(){
Serial.begin(9600);
pinMode (soundPin, INPUT);
}

void loop(){
int val;
val = digitalRead(soundPin);
Serial.println(val);
}

Anyone see what I am missing?

I think the logic is fine but I must be able to pick up the sound which I can't seem to do.

No the logic is not fine.
An Xbee transmitts data in packets, normally it waits until it has a buffer full to send or until a time out period before sending anything therefor it will not work like you think.

What makes you think you will pick up anything with the receiver connected directly to an arduino pin?
Your use of the variable name duration seems to suggest you are expecting some sort of value rather than an on / off interaction.

Grumpy mike said,

What makes you think you will pick up anything with the receiver connected directly to an arduino pin?

If I am creating a 40,000 Hz pulse a couple inches away, I should get a signal on the receiver, right? Connected to an analog pin I should get some analog signal, correct? That is, I should be able to see a difference in voltage when the 40,000 Hz signal is present and absent.

I should get a signal on the receiver, right?

No.
I don't think you understand what the sensor gives you.

I don't think you understand what the sensor gives you.

Well, I am going to have to agree. Could you share some light on the proper functioning?

Im no expert on ultrasonic sensors, but from a quick look at the specs, the receiving sensor will produce
a 40Khz AC signal at some unknown voltage.
You will need to find out what the amplitude of the AC voltage is , amplify it if needed, and then peak rectify it
to convert the signal into something the Arduino can use.

It might be useful, to get one of the devices with two sensors which works by sending ultrasound and then
receiving the reflection at the same location.

Playing with that will give you some more insight into how ultrasound works.

I recently got one to play with, the "output" from it is some pulses which you have to count,
which is somehow proportional to the apparent distance. It can be a bit tricky to get it to work.

Quite apart from actually getting the sensor to work, I still think you will have a problem with your
entire scheme, with the timing. The radio waves from your xbee will get to the other side before
the ultrasound waves do, but there will still be sufficient ( and variable ) latency in the process, that
you won't actually be able to synchronise them very reliably.

You need an amplifier to convert the tiny signal into one that the arduino can read.
The attached circuit will do that. However this still gives a 40KHz signal and you want just an indication of if it is on or off. Therefore put a diode from the output of the op amp to the arduino analogue input pin. Then put a 1nF capacitor from the input pin to ground and finally put a 10K resistor across that capacitor.

transducer.png