Hc-sr04 stops updating on the serial monitor

Hey, I am using a hc-sr04 and a Arduino Leonardo to measure the distance of a moving object. The problem is that the serial monitor sometimes stop updating the distance and I have to manually wave something in front of the sensor to update it so it continues to write values. But sometimes when the value stays constant it updates perfectly fine. So the problem only exists sometimes and I don't know why? I would be grateful for any help.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Pin für Trigger
int TRIGGER = 8;

// Pin für reflektiertes Signal
int ECHO = 7;

long Entfernung = 0;

// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);

  Serial.begin(9600);

  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display

  oled.setTextSize(3);          // text size
  oled.setTextColor(WHITE);     // text color
  
}

int getEntfernung() 
{

  // Sender kurz ausschalten um Störungen des Signal zu vermeiden
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(3);
  noInterrupts();
  // Signal für 10 Micrsekunden TRIGGER, danach wieder ausschalten
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);

  // pulseIn -> Zeit messen, bis das Signal zurückkommt
  long Zeit = pulseIn(ECHO, HIGH);
  interrupts();

  
  // Entfernung in cm berechnen
  // Zeit/2 -> nur Strecke
  return Entfernung = (Zeit / 2) * 0.03432;
}


void loop() {
  
  int Entfernung = getEntfernung();
  delay(200);

  // nur Entfernungen < 100 anzeigen
  if (Entfernung < 100) {
    // Messdaten anzeigen
    Serial.println(Entfernung);

    oled.setCursor(0, 10);        // position to display
    oled.clearDisplay();       //clear display
    oled.println(Entfernung); // text to display
    oled.display();               // show on OLED

  }

}

In my experience HC-SR04 are junk because there are many faulty sensors.

I had a lot of problems with them years ago, with sensors that sometimes "lock" returning either imediately a zero value or a "phantom" fixed 3 cm measure, or exiting with a pulseIn() timeout, not detecing obstacles anymore. The only way to unlock it was to power it down and back up, or, as a workaround, I discovered setting ECHO pin to LOW for a few milliseconds and then back to HIGH could recover the lock by forcing it as a "soft reset".

The ultimate solution for me was buying the more reliable HY-SRF05, and I strongly suggest everyone to do so.

PS: years ago I also wrote a small library to be able to handle the "locks" trying a "software" recover, and use SR04 and SRF05 the same way:

The library handles the ping and echo procedure, timeout, and periodic readings in an easy way.
Please note even if the README is in italian, an english version is provided here.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.