Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
The Importance and Utility of Ultrasonic Sensors
Ultrasonic sensors are widely used in various fields, including robotics, automation, and security systems. These sensors utilize sound waves to measure distances, making them an essential tool for object detection and obstacle avoidance. They offer a non-contact method of distance measurement, enabling accurate and reliable readings in a wide range of applications.
By emitting ultrasonic waves and measuring the time it takes for the waves to bounce back after hitting an object, ultrasonic sensors can calculate the distance between the sensor and the object. This capability makes them ideal for applications such as parking sensors, liquid level measurement, and even gesture recognition.
Project: Ultrasonic Distance Measurement with Arduino
In this project, we will create a simple distance measurement system using an ultrasonic sensor and an Arduino board. The objective is to measure the distance between the sensor and an object and display the result on a serial monitor.
List of Components:
You can find these components at the following links:
Examples:
Example 1: Basic Distance Measurement
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
}
void loop() {
delay(50);
unsigned int distance = sonar.ping_cm();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
In this example, we use the NewPing library to simplify the ultrasonic sensor interfacing. We define the trigger and echo pins, as well as the maximum distance to measure. The sonar.ping_cm()
function returns the distance in centimeters, which we then display on the serial monitor.
Example 2: Obstacle Avoidance
#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
#define OBSTACLE_DISTANCE 20
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
delay(50);
unsigned int distance = sonar.ping_cm();
if (distance <= OBSTACLE_DISTANCE) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
In this example, we add an LED to indicate the presence of an obstacle. If the distance measured by the sensor is less than or equal to the predefined obstacle distance, the LED is turned on. Otherwise, it remains off.