How to do the coding of a ping sensor or ultrasound sensor

Have a lot of doubts in it . Somebody please write and explain .Sir says its very big will explain it l8r. :slight_smile: :slight_smile: :slight_smile:

I'm working with an HC-SR04 sensor currently as well. I made a post here about my difficulties, however I've gotten most of the bugs worked out since then.

The basic gist of how they operate is as follows: you send a 10 uS pulse to the trigger, which will send 8 40kHz clock cycles out through the module. When the signal comes back, it will send a pulse through the echo pin. Depending on the length of that pulse, you determine the distance an object is away from your sensor. I would assume the PING)) works the same way, except that it operates using 1 signal pin instead of 2.

The following code would work for one sensor

// PIN DEFINITIONS 

#define TrigPin 8
#define Echo 9

long Time, Distance;


void setup(){
  
// I/O DECLARATIONS
pinMode(TrigPin, OUTPUT);
pinMode(Echo, INPUT);
digitalWrite(TrigPin, LOW); //Initialize trigger as low 
    
Serial.begin(9600);    //Serial Communication for troubleshooting
}

void loop(){
  
  // Send signal to Trig Pin
  digitalWrite(TrigPin, HIGH); //Writes Trig Pin HIGH
  delayMicroseconds(10); //HC-SR04 Needs a 10uS pulse sent to trigger
  digitalWrite(TrigPin, LOW);  //Sends Trig Pin Low
  
  // variables to read sensor feedback

  Time = pulseIn(Echo, HIGH);
  Distance = (Time / 148);    //Distance in inches, eqn from datasheet

  Serial.print(Distance);
  Serial.println(" inches");

  }