Ultrasonic sensor

Please can you tell me what exactrly this program do? I need it to my school.

[ltr][color=#939393][color=#000000]/* Example code for HC-SR04 ultrasonic distance sensor with Arduino. No library required. More info: https://www.makerguides.com */

// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3

// Define variables:
long duration;
int distance;

void setup() {
  // Define inputs and outputs:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  //Begin Serial communication at a baudrate of 9600:

void loop() {
  // Clear the trigPin by setting it LOW:
  digitalWrite(trigPin, LOW);

  // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  // Calculate the distance:
  distance = duration * 0.034 / 2;

  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
  Serial.print("Distance = ");
  Serial.println(" cm");






Remove that from your code.

Please give a proper description of the problem. Does it compile? If it compiles, does it upload? If it uploads, what does it do and how does it differ from what you expect it to do?

And we might need a wiring diagram (photo of hand-drawn is fine).

Well as sterretje said, remove that from your code and looks like the code just prints the distance to the serial monitor measured by the ultrasonic sensor....Are there any other components connected? We need a bit more detail please..

Guys... How can he give you more details? He ha no idea what it does.

-jim lee

OP: google hc-sr04 tutorial

The HC-SR04 ultrasonic rangefinder has an input pin called Trigger and an output pin called Echo. When the Trigger input goes from LOW to HIGH, the HC-SR04 sends out a short 40kHz ultrasonic pulse and times how long it takes for the first echo to return. On the Echo output, it puts out a HIGH pulse that same duration.

The example pulses the Trigger and then uses pulseIn() to measure the duration of the Echo pulse in microseconds. Sound travels about 0.0343 CM per microsecond so multiply the duration by 0.0343 to get round-trip (out and back) centimeters. Dividing by 29.15 also works. Since the round-trip distance is twice the one-way distance, divide by 2 to get the one-way distance.