How Fast Is Hc-sr04 Ultrasonic Sensor in Taking Readings
This article is a guide almost the Ultrasonic Sensor HC-SR04. Nosotros'll explain how information technology works, testify you lot some of its features and share an Arduino project example you tin follow to integrate into your projects. Nosotros provide a schematic diagram on how to wire the ultrasonic sensor and an example sketch with the Arduino.
Description
The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object. This sensor reads from 2cm to 400cm (0.8inch to 157inch) with an accuracy of 0.3cm (0.1inches), which is good for most hobbyist projects. In addition, this detail module comes with ultrasonic transmitter and receiver modules.
The following picture shows the HC-SR04 ultrasonic sensor.
The side by side flick shows the other side of the sensor.
Features
Here'southward a list of some of the HC-SR04 ultrasonic sensor features and specs—for more information, you should consult the sensor'south datasheet:
- Power Supply :+5V DC
- Quiescent Current : <2mA
- Working Current: 15mA
- Effectual Bending: <15°
- Ranging Distance : 2cm – 400 cm/i″ – 13ft
- Resolution : 0.3 cm
- Measuring Bending: thirty degree
- Trigger Input Pulse width: 10uS TTL pulse
- Repeat Output Indicate: TTL pulse proportional to the distance range
- Dimension: 45mm x 20mm x 15mm
How Does it Work?
The ultrasonic sensor uses sonar to determine the distance to an object. Here'south what happens:
- The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz).
- The sound travels through the air. If information technology finds an object, it bounces back to the module.
- The ultrasound receiver (repeat pivot) receives the reflected sound (echo).
The time betwixt the manual and reception of the indicate allows usa to calculate the altitude to an object. This is possible because nosotros know the sound'due south velocity in the air. Here'due south the formula:
distance to an object = ((speed of sound in the air)*time)/2 - speed of sound in the air at 20ºC (68ºF) =343m/s
HC-SR04 Ultrasonic Sensor Pinout
Here'south the pinout of the HC-SR04 Ultrasonic Sensor.
| VCC | Powers the sensor (5V) |
| Trig | Trigger Input Pin |
| Echo | Repeat Output Pin |
| GND | Common GND |
Where to buy?
You can check the Ultrasonic Sensor HC-SR04 sensor on Maker Advisor to find the best price:
- HC-SR04 Ultrasonic Sensor
Arduino with HC-SR04 Sensor
This sensor is very popular amid Arduino tinkerers. So, here we provide an instance of how to apply the HC-SR04 ultrasonic sensor with the Arduino. In this project, the ultrasonic sensor reads and writes the distance to an object in the serial monitor.
The goal of this project is to help y'all empathise how this sensor works. Then, you should be able to utilize this example in your ain projects.
Parts Required
Here's a listing of the parts required to follow the next tutorial:
- Arduino UNO – read Best Arduino Starter Kits
- Ultrasonic Sensor (HC-SR04)
- Breadboard
- Jumper wires
You tin can use the preceding links or go direct to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Arduino with HC-SR04 Sensor Wiring
Follow the next schematic diagram to wire the HC-SR04 ultrasonic sensor to the Arduino.
The post-obit tabular array shows the connections you need to make:
| Ultrasonic Sensor HC-SR04 | Arduino |
| VCC | 5V |
| Trig | Pin 11 |
| Repeat | Pin 12 |
| GND | GND |
Code
Upload the post-obit code to your Arduino IDE.
/* * created past Rui Santos, https://randomnerdtutorials.com * * Complete Guide for Ultrasonic Sensor HC-SR04 * Ultrasonic sensor Pins: VCC: +5VDC Trig : Trigger (INPUT) - Pin11 Echo: Repeat (OUTPUT) - Pivot 12 GND: GND */ int trigPin = 11; // Trigger int echoPin = 12; // Repeat long duration, cm, inches; void setup() { //Serial Port brainstorm Serial.begin (9600); //Ascertain inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // The sensor is triggered by a HIGH pulse of 10 or more than microseconds. // Give a curt Low pulse beforehand to ensure a make clean High pulse: digitalWrite(trigPin, Depression); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Read the indicate from the sensor: a HIGH pulse whose // duration is the fourth dimension (in microseconds) from the sending // of the ping to the reception of its echo off of an object. pinMode(echoPin, INPUT); elapsing = pulseIn(echoPin, High); // Catechumen the time into a distance cm = (duration/2) / 29.i; // Split up past 29.1 or multiply by 0.0343 inches = (elapsing/ii) / 74; // Split by 74 or multiply by 0.0135 Series.print(inches); Serial.impress("in, "); Serial.print(cm); Serial.print("cm"); Series.println(); delay(250); } View raw code
How the Code Works
First, y'all create variables for the trigger and echo pin called trigPin and echoPin, respectively. The trigger pin is connected to digital Pin 11, and the echo pivot is connected to Pin 12:
int trigPin = xi; int echoPin = 12; You lot as well create three variables of type long: duration and inches. The duration variable saves the fourth dimension between the emission and reception of the signal. The cm variable will salve the altitude in centimeters, and the inches variable volition save the distance in inches.
long elapsing, cm, inches; In the setup(), initialize the serial port at a baud rate of 9600, and prepare the trigger pin as an OUTPUT and the echo pivot as an INPUT.
//Serial Port begin Serial.begin (9600); //Define inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); In the loop(), trigger the sensor by sending a HIGH pulse of 10 microseconds. But, before that, give a short Low pulse to ensure y'all'll get a clean High pulse:
digitalWrite(trigPin, Low); delayMicroseconds(5); digitalWrite(trigPin, Loftier); delayMicroseconds(10); digitalWrite(trigPin, LOW); Nosotros use thepulseIn() office to get the sound wave travel time:
duration = pulseIn(echoPin, Loftier); ThepulseIn() function reads a Loftier or a Low pulse on a pin. It accepts as arguments the pin and the state of the pulse (either HIGH or Depression). It returns the length of the pulse in microseconds. The pulse length corresponds to the time it took to travel to the object plus the time traveled on the way back.
Then, nosotros calculate the distance to an object, taking into account the sound speed.
distance = (traveltime/2) x speed of audio The speed of audio is: 343m/s = 0.0343 cm/uS = one/29.1 cm/united states Or in inches: 13503.9in/s = 0.0135in/the states = 1/74in/uS
We need to divide the travel time past 2 because we have to consider that the wave was sent, hit the object, and then returned to the sensor.
cm = (duration/2) / 29.1; inches = (duration/2) / 74; Finally, we impress the results in the Serial Monitor:
Series.print(inches); Series.print("in, "); Series.impress(cm); Serial.impress("cm"); Serial.println(); Source lawmaking with NewPing Library
You lot tin can also use the NewPing library. Download the library hither.
After installing the NewPing library, y'all tin upload the code provided below.
/* * Posted on https://randomnerdtutorials.com * created past http://playground.arduino.cc/Code/NewPing */ #include <NewPing.h> #define TRIGGER_PIN 11 #ascertain ECHO_PIN 12 #ascertain MAX_DISTANCE 200 // NewPing setup of pins and maximum altitude NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); void setup() { Serial.begin(9600); } void loop() { delay(50); unsigned int distance = sonar.ping_cm(); Serial.impress(distance); Serial.println("cm"); } View raw lawmaking
How the Lawmaking Works
Getting the distance to an object using the NewPing library is much simpler.
You start past including the NewPing library:
#include <NewPing.h> Then, define the trigger and repeat pin. The trigger pin is connected to the Arduino digital Pivot 11 and the echo to Pin 12. Y'all also demand to define the MAX_DISTANCE variable to exist able to employ the library.
#define TRIGGER_PIN 11 #define ECHO_PIN 12 #ascertain MAX_DISTANCE 200 So, y'all create a NewPing instance called sonar:
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); In the setup(), you initialize the Serial communication at a baud rate of 9600.
Serial.begin(9600); Finally, in the loop(), you merely need to use the ping_cm() method on the sonar object to get the distance in centimeters.
unsigned int altitude = sonar.ping_cm(); If yous desire to become the distance in inches, y'all tin utilize sonar.ping_in() instead.
Demonstration
Upload the code to your Arduino board. So, open the Serial Monitor at a baud rate of 115200.
The altitude to the nearest object is printed in the Serial Monitor window.
Wrapping Up
In this post, we've shown you how the HC-SR04 ultrasonic sensor works and how y'all can apply it with the Arduino board. For a project example, you tin can build a Parking Sensor with LEDs and a buzzer.
If you are a beginner to the Arduino, we recommend following our Arduino Mini-Grade that volition help y'all get started quickly with this amazing board.
If you lot like Arduino, you lot may also like:
- Arduino Step-by-step Projects course
- Guide to SD menu module with Arduino
- Guide to DHT11/DHT22 Humidity and Temperature Sensor With Arduino
- Guide for TCS230/TCS3200 Color Sensor with Arduino
You tin can detect all our Arduino projects and tutorials here.
We promise y'all found this tutorial useful. Thank you for reading.
hernandezhicandre.blogspot.com
Source: https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/
0 Response to "How Fast Is Hc-sr04 Ultrasonic Sensor in Taking Readings"
Post a Comment