0

I'm working on water level detector using lolin nodemcu v3 and AJ-SR04M Sensor, a water proof ultrasonic sensor. It is working fine at close distance less than one meter or so. However for long distance > 1 meter, I get varying results. enter image description here

Using the following code:

void setup() {
  
  Serial.begin (115200);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);
}

void loop() {
  
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance= duration*0.034/2;
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000);
}

This is what I get in the monitor for the same distance 2-3 meters:

Distance: 141 cm
Distance: 135 cm
Distance: 225 cm
Distance: 195 cm
Distance: 226 cm
Distance: 226 cm
Distance: 129 cm
Distance: 135 cm
Distance: 134 cm
Distance: 146 cm
Distance: 136 cm
Distance: 129 cm
Distance: 154 cm
Distance: 123 cm
Distance: 147 cm
Distance: 226 cm
Distance: 181 cm
Distance: 137 cm
Distance: 134 cm
Distance: 136 cm
Distance: 137 cm
Distance: 145 cm
Distance: 227 cm
Distance: 179 cm
Distance: 226 cm
Distance: 226 cm
Distance: 101 cm
Distance: 134 cm
Distance: 270 cm
Distance: 294 cm
Distance: 266 cm
Distance: 281 cm
Distance: 146 cm
Distance: 279 cm
Distance: 128 cm
Distance: 579 cm
Distance: 138 cm

So, is there a way to make it right or that means my sensor is faulty.

  • try to put 4K7 resistance at triger pin to gnd – catalin Dec 9 '20 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.