I am using a HC-SR04. Whenever I try reading something from the sensor I always get the same value regardless of the actual distance, sometimes random outputs are also received.
This is the code:
int trigPin = 3; // Trigger
int echoPin = 5 ; // Echo
long duration;
float cm;
float inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}
This usually indicates a wiring issue. Verify trig and echo are on the correct pin.
Another "same value" might be caused by the location of the SR04. Is it resting on a surface? Pointing at the surface? The "cone" of the transmitter is 15 degrees (small, but not a pinpoint).
int trigPin = 3; // Trigger
int echoPin = 5 ; // Echo
long duration;
float cm;
float inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = duration * 0.0172;
inches = duration * 0.00677;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1000);
}
Tried the code but still similarly bad results.
Also, many times i get 0 as output untill I adjust the sensors
Is it possible that I have faulty wires but the circuit is correct ?
Yes that is quite possible.
Try moving the sensor to a different spot on the breadboard.
Connect 5V and ground directly to the sensor, don't use the power rails on the edge of the breadboard
Thanks for the suggestion, I wiil do it. I did move it to a diff spot on the breadboard. I also feel that the sensor does not have a tight fit onto the breadboard after i connect the wires
Remove the SR04 and run the sketch again. If you get similar values, the SR04 is either not powered, not working, not returning data or not being read.
I realised the issue, I changed the wires but it didnt work, until:
This did solve the problem.
Thanks a lot. It is working now but occasionally a reading 1000 cm creeps in, but this occurs quite rarely. So , i think I can manage, since my application focuses on objects which are close. The 1000 cm error does not stay it goes away.
Thanks again to everyone who supported me!!!