# Ultrasonic Range Sensor

### Parts:&#x20;

1 – Arduino Uno

1 – Ultrasonic Sensor

Assorted Wires

### Wiring:&#x20;

![](/files/jc0LoSvSIo6UY3Sg63l1)

| Ultrasonic | Arduino |
| ---------- | ------- |
| Vcc        | 5V      |
| Trig       | 13      |
| Echo       | 12      |
| GND        | Gnd     |

### Code:&#x20;

```
//here we declare what each pin will be on arduino board
int trigPin = 13;
int echoPin = 12;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //gives the user half a second to put down the car after turning the switch  delay(500);
}
void loop() {
  // get the distance away from the sensor for an object
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  //print distance for troubleshooting
  if (distance >= 200 || distance <= 0) {
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.print(" cm\t");
    Serial.print(distance / 2.54);
    Serial.println(" in");
  }
  delay(20);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://guides.mccaskeyrobotics.org/arduino/ultrasonic-range-sensor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
