Ultrasonic HC-SRD4 not working

currently creating a device that measures velocity through the ultrasonic sensor then outputs to a LED light depending on the range of velocity.

it was working before but the sensor suddenly didn't output correct values. it was either 0 or 1k cm

(Grove base shield, Grove LED RGB stick, ultrasonic sensor)


#include "Adafruit_NeoPixel.h"
#ifdef __AVR__
  #include <avr/power.h>
#endif
#define PIN_LED           7
#define NUMPIXELS      10
const int trigPin = 2;
const int echoPin = 3;

long duration;
int distance1=0;
int distance2=0;
double speed=0;
double velocity=0;
int distance=0;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN_LED, NEO_GRB + NEO_KHZ800);

int delayval = 500;   

void setup()
{
    pixels.setBrightness(25);
    pixels.begin();
    pinMode(trigPin, OUTPUT); 
    pinMode(echoPin, INPUT);
    Serial.begin(9600); 

}

void loop(){
  pixels.clear();
  
  distance1 = ultrasonicRead(); //calls ultrasoninicRead() function below
   
  delay(2000);//giving a time gap of 2 sec
   
  distance2 = ultrasonicRead(); //calls ultrasoninicRead() function below
   
  //formula change in distance divided by change in time
  velocity = (distance2 - distance1)/2.0;
  speed = abs(velocity);

  //Displaying speed
  Serial.print("Velocity in cm/s  :  "); 
  Serial.println(speed);
  Serial.println("_______");

  if (speed >0 && speed <4){
    pixels.setPixelColor(0, pixels.Color(0,255,0)); 
    pixels.show();
    delay(50);
  }

  else if (speed >= 4 && speed < 8 ){
    for(int i=0;i<2;i++){
      pixels.setPixelColor(i, pixels.Color(0,255,0)); 
      pixels.show();    
      delay(50);
    }
  }

  else if (speed >= 8  && speed < 16){
    for(int i=0;i<3;i++){
      pixels.setPixelColor(i, pixels.Color(100,150,0)); 
      pixels.show();    
      delay(50);
    }
  }

  else if (speed >= 16 && speed < 20){
    for(int i=0;i<4;i++){
      pixels.setPixelColor(i, pixels.Color(100,150,0)); 
      pixels.show();   
      delay(50);
    }
  }

  else if (speed >= 20  && speed < 24){
    for(int i=0;i<5;i++){
      pixels.setPixelColor(i, pixels.Color(255, 165, 0)); 
      pixels.show();    
      delay(50);
    }
  }

  else if (speed >= 24  && speed < 28){
    for(int i=0;i<6;i++){
      pixels.setPixelColor(i, pixels.Color(255, 165, 0)); 
      pixels.show();    
      delay(50);
    }
  }

  else if (speed >= 28  && speed < 32){
    for(int i=0;i<7;i++){
      pixels.setPixelColor(i, pixels.Color(255,80,0)); 
      pixels.show();   
      delay(50);
    }
  }

  else if (speed >= 32  && speed < 36){
    for(int i=0;i<8;i++){
      pixels.setPixelColor(i, pixels.Color(255,80,0)); 
      pixels.show();    
      delay(50);
    }
  }

  else if (speed >= 36   && speed < 40){
    for(int i=0;i<9;i++){
      pixels.setPixelColor(i, pixels.Color(255,0,0)); 
      pixels.show();    
      delay(50);
    }
  }

  else if (speed >= 40){
    for(int i=0;i<NUMPIXELS;i++){
      pixels.setPixelColor(i, pixels.Color(255,0,0)); 
      pixels.show();   
      delay(50);
    }
  }

}

float ultrasonicRead (){
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  //calculating distance
  distance= duration*0.034/2;

  // Prints the distance on the Serial Monitor
  Serial.print("Distance in cm : ");
  Serial.println(distance);

  return distance;

}

im suspecting the problem lies with the sensor itself

Software doesn't change but hardware does. Try and post schematics.

Did you move it in any way? did you change your position? What is the target? What else could have changed? Post an annotated schematic as requested by @Railroader. The following will help us help you and give you an accurate answer.

  1. Provide Us Clear Documentation: Since we can’t see your project, share an annotated schematic (best) or a clear drawing of your setup. Pictures are welcome, but avoid using Fritzing diagrams as they are wiring diagrams, not schematics, and are not ideal for troubleshooting.

  2. Include Technical Details: If there is specific hardware involved, include links to technical information. There are often many versions of similar components, so precise details are essential. What version are you using?

  3. Reference Resources: For additional help, check out useful links and tutorials: Useful Links on Arduino Forum. Post links to your hardware technical information.

  4. Obtain a copy: of the Arduino Cookbook and complete several of the projects. Many of them will help you achieve parts of your goals.

  5. Course Electronics For Beginners (eBook)

  6. Electronics For Beginners (eBook)

  7. Arduino Step-by-step Projects Course | Build 25 Projects

What values do you consider incorrect?

The devices are probably performing exactly as they should when used as intended. Verify your hardware.

Did you not in the documentation that ZERO is returned when no echo is detected?

Before, I would place and object 4 cm away and the sensor would read it as 4 cm, but now the output keep being zero or a completely random number no matter where I put the object.

I also tried running a test code for the sensor only and it kept doing the same thing.

Looks like the brown and the white wires are just stuffed into the connector. Can you make a more solid connection to those pins?

A picture like that is more of a mystery. That"s not schematics. Try and change to new, fresh, cables.

The power supply is insufficient, or the ?board? is not connected correctly, or the HC-SR04 is not in the correct slot. You need to verify your work. Your lack of information indicates you are not giving attention to the details.

You’re right. I was racking my brains up for what could have possibly gone wrong. Turns out there was just a wire that wasn’t working. I changed it and the device went back to normal. Thank you!

Nice. Thanks for telling.