So im working on a school project, and i want to make a trashcan that responds when it's full.
So i'm using a SRF 04 sensor, an Arduino Uno R3 and thats it.
My sensor has 5 pins. And i think i've found a layout of it here: SRF04 Technical Documentation
but im not sure, since it was lose when i found it in a box with other sensors.
So i've hooked it up this way.
Arduino 5v. ---> Pin 1
Arduino pin 11 ---> pin 2
Arduino pin 12 ---> pin 3
Nothing ---> pin 4
Arduino GND - pin 5
After that i've made a program that should digitale write to pin 12.
and then pulseIn for pin 11.
The problem is when i read from serial, it reads 0 all the time. If i disconnect my pin 11, it will read around 170 cm all the time.
it's very annoying, and i cant seem to detect the problem.
Here is my code:
const int pingPin = 12;
long duration, inches, cm;
void setup() {
Serial.begin(9600);
pinMode(pingPin, OUTPUT);
pinMode(11, INPUT);
}
void loop()
{
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
duration = pulseIn(11, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(duration);
Serial.print("dur, ");
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
The data sheet says the trigger pulse needs to be 10 microseconds not 5. The timing diagram implies an active high trigger, but later description says the trigger is active low. Try inverting the trigger pulse.
delayMicroseconds(2);
delayMicroseconds(5);
And change these.
Change the delays to what?
I understand that it has to be 10 mircoseconds instead of 5.
And i've tried to inverse the HIGH and LOWS. Still the problem persist.
Each time i hook up my pin 11 which is my input, that should give me the duration it took for the pulse to come back, it gets 0,0,0,0,0,0,0 as so on. If i disconnect it, i get 179, 179, 179 cm, changing by 2-3 cm each time.
I think there is something wrong with my input, since it's 0, when hooked up to the arduino, but i get input, when its now hooked up...
Hi, google NewPing.h , which is a library that should do all the setup for you and provide you with a simple output.
// ---------------------------------------------------------------------------
// Pings every 200mS and displays it on 16x2 LCD and through serial port.
// Oversamples to try to eliminate some signal noise.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// include the library code:
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
// LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
lcd.begin(16, 2);
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
int icon;
int Sample=50;
float TuS;
float uS;
float ddistance;
delay(200); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
filterstart: // Filter noise from signal by over sampling.
for (int i=0; i < Sample; i++){
uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
TuS=TuS+uS;
}
uS=TuS/Sample;
filterend:
TuS=0;
int uSS = uS;
ddistance = uS / US_ROUNDTRIP_CM;
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.print("cm ");
Serial.print (uSS);
Serial.print("uS ");
Serial.println(US_ROUNDTRIP_CM);
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0,0); // place cursor at start of line 0
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(ddistance);
lcd.print("cm ");
lcd.setCursor(9,0);
lcd.print(uSS);
lcd.print("uS");
icon = ddistance/10; // determine how many icons to show on lower line.
lcd.setCursor(0,1); // place cursor at start of line 1
lcd.print(" "); // blank old icons
lcd.setCursor(0,1);
for (int i=0; i <= icon; i++){ // print as many icons as calculated, icon.
lcd.print("*");
}
}
I used this sketch ages ago, I'm not sure where I got from, I may have even modified it.
So see if it works, check that it outputs to an LCD as well as serial, so you may need to mod it, sorry I have just dumped this sketch here, but its morning and I'm just getting ready for work.
Tom....... 
Okay, i've just tried with the libary and your code, modified for my trigger and echo pins.
Now i get this in the serial : ÒPüc\üÓpý×$ÿcLüÖ?ÿÄ?ÿ?<þràþ¦úÒpü±??Ñöa?ÿ?ô???ô?ô??µ?aóÆ?ÿ ???? 
Now i'm really confused? What could have gone wrong?
When the input is connected do you get a pulse duration of 0? If so that means no pulse was received within the timeout. The readings you get when the input is disconnected are just noise. It may be that the pulse has already begun by the time you call pulseIn(), thus pulseIn() never sees the rising edge and times out. Have you tried this..
All SRF04's manufactured after May 2003 have new software implementing an optional timing control input using the "do not connect" pin. This connection is the PIC's Vpp line used to program the chip after assembly. After programming its just an unused input with a pull-up resistor. When left unconnected the SRF04 behaves exactly as it always has and is described above. When the "do not connect" pin is connected to ground (0v), the timing is changed slightly to allow the SRF04 to work with the slower controllers such as the Picaxe. The SRF04's "do not connect" pin now acts as a timing control. This pin is pulled high by default and when left unconnected, the timing remains exactly as before. With the timing pin pulled low (grounded) a 300uS delay is added between the end of the trigger pulse and transmitting the sonic burst. Since the echo output is not raised until the burst is completed, there is no change to the range timing, but the 300uS delay gives the Picaxe time to sort out which pin to look at and start doing so. The new code has shipped in all SRF04's since the end of April 2003.
Thanks for the help David'O'Connor, i will try to connect ground to pin 4 on the SRF 04, tommorow. And thanks for trying to help me everyone, this is my first time using arduino, so i'm really new to all of this. 
Hi, yes the monitor baud rate is 115200 in the sketch I posted.
Tom.... 
So now i've tried with ground to pin 4 on the sensor. Still returns a value of 0....
Other suggestions? Could i have a defect sensor? If so how do i test it?
Do you have an oscilloscope?
Can you post your new code? And its output. Also include a picture or diagram of your circuit.