CIPAD Leçon 19: Comment utiliser un capteur de distance à ultrasons HC-SR04 avec Arduino

3 min read 2 hours ago
Published on Oct 02, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of using the HC-SR04 ultrasonic distance sensor with an Arduino board. The HC-SR04 sensor is popular for measuring distances, making it ideal for robotics and automation projects. By the end of this guide, you'll be able to integrate the sensor into your Arduino projects and accurately measure distances.

Step 1: Gather Your Materials

To get started, you'll need the following components:

  • Arduino board (e.g., Arduino Uno)
  • HC-SR04 ultrasonic distance sensor
  • Jumper wires
  • Breadboard (optional)
  • Computer with Arduino IDE installed

Step 2: Connect the HC-SR04 Sensor

Follow these instructions to connect your HC-SR04 to the Arduino:

  1. Identify the HC-SR04 Pins:

    • VCC: Power supply (5V)
    • Trig: Trigger pin
    • Echo: Echo pin
    • GND: Ground
  2. Make the Connections:

    • Connect the VCC pin to the Arduino 5V pin.
    • Connect the GND pin to the Arduino GND pin.
    • Connect the Trig pin to Arduino digital pin 9.
    • Connect the Echo pin to Arduino digital pin 10.

Step 3: Write the Arduino Code

Open the Arduino IDE and write the following code to read distance values from the HC-SR04 sensor:

#define TRIG_PIN 9
#define ECHO_PIN 10

void setup() {
  Serial.begin(9600); // Start serial communication
  pinMode(TRIG_PIN, OUTPUT); // Set the trigger pin as an output
  pinMode(ECHO_PIN, INPUT); // Set the echo pin as an input
}

void loop() {
  long duration, distance;
  
  // Clear the trigger
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  
  // Set the trigger to HIGH for 10 microseconds
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // Read the echo pin
  duration = pulseIn(ECHO_PIN, HIGH);
  
  // Calculate distance in cm
  distance = duration * 0.034 / 2;
  
  // Print the distance to the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(1000); // Wait for a second before next measurement
}

Step 4: Upload the Code

  1. Connect your Arduino board to your computer using a USB cable.
  2. Select the correct board and port in the Arduino IDE.
  3. Click on the upload button (right arrow icon) to upload the code to the Arduino.

Step 5: Test the Setup

  1. Open the Serial Monitor in the Arduino IDE (Tools > Serial Monitor).
  2. You should see the distance measurements displayed in centimeters.
  3. Test the sensor by placing objects at various distances and observe the readings.

Conclusion

You have successfully set up and tested an HC-SR04 ultrasonic distance sensor with an Arduino. This sensor can be utilized in various projects, such as obstacle detection in robots or distance measuring devices. For further exploration, consider integrating the sensor with other components, like motors or displays, to create more complex projects. Happy coding!