How to connect Arduino with HC-SR04 ultrasonic distance sensor

Tilted Brush Stroke

You will need the following components: 1. Arduino board (e.g., Arduino Uno) 2. HC-SR04 ultrasonic sensor 3. Jumper wires 4. Breadboard (optional, for easier connections)

Get all the components as per above list

Tilted Brush Stroke

Now let us connect the above components.

Tilted Brush Stroke

Please note the sensor pins and connect accordingly.





Tilted Brush Stroke

Connect as per above instructions.

Wiring Steps: 1. Connect the VCC pin of the HC-SR04 to the 5V pin on the Arduino. 2. Connect the GND pin of the HC-SR04 to the GND pin on the Arduino. 3. Connect the Trig pin of the HC-SR04 to a digital pin on the Arduino (e.g., D2). 4. Connect the Echo pin of the HC-SR04 to another digital pin on the Arduino (e.g., D3).

// Define the pins const int trigPin = 2;  const int echoPin = 3;  // Define variables long duration; int distance;  void setup() {   // Initialize serial communication  Serial.begin(9600);    // Set trigPin as OUTPUT and echoPin as INPUT pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);  }

Tilted Brush Stroke

Time for Arduino sketch. Get it from our website.

Uploading and Running the Code: 1. Connect your Arduino to your computer using a USB cable. 2. Open the Arduino IDE on your computer. 3. Copy the provided code. 4. Paste the code into the Arduino IDE. 5. Select your Arduino board under the "Tools" menu. 6. Select the port that your Arduino is connected to under the "Tools" menu. 7. Click the "Upload" button to upload the code to your Arduino. 8. Open the Serial Monitor to view the distance measurements.

Tilted Brush Stroke

See it in action.

Tilted Brush Stroke

Want any help ?

Thanks for watching

Contact us at

Want to more projects ?