ARDUINO #15 : Capteur à ultrasons [tuto en français]

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

Table of Contents

Introduction

In this tutorial, we will learn how to measure distances using an ultrasonic sensor with an Arduino. This project is perfect for beginners looking to expand their knowledge of Arduino programming and sensor integration. By the end of this guide, you'll be able to create a simple distance measuring device that can be used in various applications.

Step 1: Gather Your Materials

Before starting the project, make sure you have the following materials:

  • Arduino board (e.g., Arduino Uno)
  • Ultrasonic sensor (e.g., HC-SR04)
  • Breadboard
  • Jumper wires
  • Resistors (if needed)
  • USB cable for Arduino
  • Computer with Arduino IDE installed

Step 2: Understand the Ultrasonic Sensor

The ultrasonic sensor works by emitting sound waves and measuring the time it takes for the echo to return. It has four main pins:

  • VCC: Power supply (typically 5V)
  • Trig: Trigger pin for sending the signal
  • Echo: Echo pin for receiving the signal
  • GND: Ground

Familiarize yourself with how the sensor operates to ensure correct wiring and coding.

Step 3: Wiring the Ultrasonic Sensor

Follow these steps to connect the ultrasonic sensor to the Arduino:

  1. Connect the VCC pin of the sensor to the 5V pin on the Arduino.
  2. Connect the GND pin of the sensor to the GND pin on the Arduino.
  3. Connect the Trig pin of the sensor to a digital pin on the Arduino (e.g., pin 9).
  4. Connect the Echo pin of the sensor to another digital pin on the Arduino (e.g., pin 10).

Refer to the wiring diagram provided in the video for a visual guide.

Step 4: Write the Arduino Code

Now, you need to write the code that will allow the Arduino to communicate with the ultrasonic sensor. Here's a basic example:

const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration, distance;
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034) / 2; // Calculate distance in cm
  
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  delay(1000); // Wait for a second before next measurement
}

Practical Tips for Coding

  • Ensure the correct pin numbers are defined at the top of the code.
  • Adjust the delay time if you need faster measurements.
  • Use the Serial Monitor to view the output.

Step 5: Upload the Code and Test

  1. Connect your Arduino to the computer using the USB cable.
  2. Open the Arduino IDE and paste the code into a new sketch.
  3. Select the correct board and port from the Tools menu.
  4. Click on the upload button to upload the sketch to the Arduino.
  5. Open the Serial Monitor to observe the distance measurements.

Conclusion

Congratulations! You've successfully set up an ultrasonic sensor with an Arduino to measure distances. This project showcases the basics of working with sensors and coding in Arduino. You can further explore this project by:

  • Integrating an LCD to display the distance.
  • Using the distance measurements to control a motor or LED.
  • Experimenting with different sensor models.

Continue to experiment and expand your Arduino skills by trying more complex projects!