CIPAD Leçon 20: Radar de recul ultrasonic avec alarme de proximité
Table of Contents
Introduction
In this tutorial, we will learn how to create an ultrasonic reverse radar system with a proximity alarm using Arduino. This project builds on concepts introduced in previous lessons and combines a few lines of code with a piezo buzzer to detect distance and provide warnings. This guide is perfect for beginners looking to expand their Arduino programming skills and create practical applications.
Step 1: Gather Your Materials
Before starting, ensure you have the following components:
- Arduino board (e.g., Arduino Uno)
- Ultrasonic distance sensor (HC-SR04)
- Piezo buzzer
- Breadboard and jumper wires
- Power source (USB or battery)
Practical Tips
- A breadboard will help you easily connect components without soldering.
- Make sure your ultrasonic sensor and piezo buzzer are compatible with your Arduino model.
Step 2: Connect the Components
Follow these connections to set up your circuit:
- Connect the ultrasonic sensor to the Arduino:
- VCC to 5V
- GND to GND
- Trigger pin to digital pin 9
- Echo pin to digital pin 10
- Connect the piezo buzzer:
- One terminal to digital pin 8
- Other terminal to GND
Common Pitfalls to Avoid
- Double-check your pin connections to avoid incorrect readings.
- Ensure the ultrasonic sensor is oriented correctly for distance measurement.
Step 3: Write the Arduino Code
Use the following code to program your Arduino for distance measurement and alarm functionality:
#include <NewPing.h>
#define TRIGGER_PIN 9
#define ECHO_PIN 10
#define MAX_DISTANCE 200
#define BUZZER_PIN 8
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
delay(50);
unsigned int distance = sonar.ping_cm();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance > 0 && distance < 30) {
digitalWrite(BUZZER_PIN, HIGH); // Activate buzzer
} else {
digitalWrite(BUZZER_PIN, LOW); // Deactivate buzzer
}
}
Explanation of Code
- The code uses the NewPing library to handle the ultrasonic sensor.
- It defines pins for the trigger, echo, and buzzer.
- The loop checks the distance and activates the buzzer if the distance is below 30 cm.
Step 4: Upload the Code and Test
- Connect your Arduino to your computer using a USB cable.
- Open the Arduino IDE and copy the code above into a new sketch.
- Select your Arduino board and port under the Tools menu.
- Click the upload button to transfer the code to your Arduino.
- After uploading, disconnect the USB and power the Arduino using a battery or external power.
Testing Your Setup
- Place an object in front of the ultrasonic sensor at varying distances.
- Ensure the buzzer activates when the object is within 30 cm.
Conclusion
You have successfully created an ultrasonic reverse radar with a proximity alarm using Arduino! This project enhances your understanding of sensor integration and coding in Arduino. As a next step, consider experimenting with different distance thresholds or adding additional features, such as LED indicators or a display for distance readouts. Happy coding!