Ultrasound Distance Sensor Reading Slows Down When Zero

This article is a guide well-nigh the Ultrasonic Sensor HC-SR04. We'll explain how information technology works, show yous some of its features and share an Arduino project case you can follow to integrate into your projects. We provide a schematic diagram on how to wire the ultrasonic sensor and an example sketch with the Arduino.

Complete Guide for Ultrasonic Sensor HC-SR04 with 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 adept for most hobbyist projects. In addition, this item module comes with ultrasonic transmitter and receiver modules.

The following picture shows the HC-SR04 ultrasonic sensor.

HC-SR04 Ultrasonic Sensor Module Distance Measurement Component Part Front

The next picture shows the other side of the sensor.

HC-SR04 Ultrasonic Sensor Module Distance Measurement Component Part Back

Features

Hither'south a list of some of the HC-SR04 ultrasonic sensor features and specs—for more than data, y'all should consult the sensor's datasheet:

  • Power Supply :+5V DC
  • Quiescent Current : <2mA
  • Working Current: 15mA
  • Effectual Angle: <xv°
  • Ranging Distance : 2cm – 400 cm/one″ – 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS TTL pulse
  • Repeat Output Point: TTL pulse proportional to the distance range
  • Dimension: 45mm x 20mm 10 15mm

How Does it Work?

The ultrasonic sensor uses sonar to make up one's mind the altitude to an object. Here's what happens:

  1. The ultrasound transmitter (trig pivot) emits a high-frequency sound (twoscore kHz).
  2. The sound travels through the air. If it finds an object, information technology bounces back to the module.
  3. The ultrasound receiver (repeat pin) receives the reflected sound (echo).
How Ultrasonic Sensor Works

The time between the transmission and reception of the point allows us to calculate the distance to an object. This is possible considering we know the sound'south velocity in the air. Here's the formula:

          altitude 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

ultra

Here's the pinout of the HC-SR04 Ultrasonic Sensor.

VCC Powers the sensor (5V)
Trig Trigger Input Pin
Repeat Echo Output Pin
GND Common GND

Where to buy?

Yous tin can check the Ultrasonic Sensor HC-SR04 sensor on Maker Advisor to notice the all-time toll:

  • HC-SR04 Ultrasonic Sensor

Arduino with HC-SR04 Sensor

Arduino UNO Board HC-SR04 Ultrasonic Sensor Module Arduino

This sensor is very pop among Arduino tinkerers. So, here nosotros provide an example of how to utilize the HC-SR04 ultrasonic sensor with the Arduino. In this projection, the ultrasonic sensor reads and writes the distance to an object in the serial monitor.

The goal of this project is to help you sympathise how this sensor works. Then, you should be able to utilise this example in your own projects.

Parts Required

Here'southward 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

Yous can use the preceding links or get 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 following table shows the connections you need to make:

Ultrasonic Sensor HC-SR04 Arduino
VCC 5V
Trig Pin 11
Echo Pin 12
GND GND

Lawmaking

Upload the post-obit code to your Arduino IDE.

          /*  * created by Rui Santos, https://randomnerdtutorials.com  *   * Consummate Guide for Ultrasonic Sensor HC-SR04  *     Ultrasonic sensor Pins:         VCC: +5VDC         Trig : Trigger (INPUT) - Pin11         Repeat: Repeat (OUTPUT) - Pin 12         GND: GND  */   int trigPin = 11;    // Trigger int echoPin = 12;    // Echo long duration, cm, inches;   void setup() {   //Serial Port brainstorm   Serial.begin (9600);   //Define inputs and outputs   pinMode(trigPin, OUTPUT);   pinMode(echoPin, INPUT); }   void loop() {   // The sensor is triggered past a High pulse of 10 or more microseconds.   // Give a short Low pulse beforehand to ensure a clean HIGH pulse:   digitalWrite(trigPin, Depression);   delayMicroseconds(5);   digitalWrite(trigPin, High);   delayMicroseconds(10);   digitalWrite(trigPin, LOW);     // Read the signal from the sensor: a High pulse whose   // duration is the time (in microseconds) from the sending   // of the ping to the reception of its echo off of an object.   pinMode(echoPin, INPUT);   duration = pulseIn(echoPin, High);     // Convert the time into a distance   cm = (duration/2) / 29.one;     // Divide by 29.1 or multiply by 0.0343   inches = (duration/two) / 74;   // Divide past 74 or multiply by 0.0135      Serial.print(inches);   Series.print("in, ");   Series.print(cm);   Serial.print("cm");   Serial.println();      delay(250); }                  

View raw code

How the Code Works

First, you create variables for the trigger and echo pin called trigPin and echoPin, respectively. The trigger pin is connected to digital Pin 11, and the repeat pin is connected to Pin 12:

          int trigPin = 11;  int echoPin = 12;        

You lot also create three variables of type long: duration and inches. The duration variable saves the time betwixt the emission and reception of the betoken. The cm variable will salvage the altitude in centimeters, and the inches variable will relieve the altitude in inches.

          long duration, cm, inches;        

In the setup(), initialize the serial port at a baud charge per unit of 9600, and ready the trigger pin as an OUTPUT and the echo pivot as an INPUT.

          //Serial Port brainstorm Serial.begin (9600); //Ascertain inputs and outputs pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);        

In the loop(), trigger the sensor by sending a Loftier pulse of 10 microseconds. But, before that, give a brusk Low pulse to ensure you'll get a clean Loftier pulse:

          digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);        

We utilize thepulseIn() function to get the sound wave travel time:

          duration = pulseIn(echoPin, HIGH);        

ThepulseIn() function reads a HIGH or a Low pulse on a pin. It accepts every bit arguments the pin and the land of the pulse (either Loftier 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 dorsum.

Then, nosotros calculate the distance to an object, taking into business relationship the sound speed.

          distance = (traveltime/ii) x speed of audio          The speed of sound is: 343m/south = 0.0343 cm/uS = ane/29.1 cm/uS  Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS

Nosotros need to divide the travel fourth dimension past 2 because we accept to consider that the wave was sent, hit the object, and then returned to the sensor.

          cm = (duration/two) / 29.one; inches = (duration/2) / 74;        

Finally, nosotros impress the results in the Series Monitor:

          Serial.print(inches); Serial.print("in, "); Series.print(cm); Serial.print("cm"); Serial.println();        

Source lawmaking with NewPing Library

You can too use the NewPing library. Download the library here.

After installing the NewPing library, yous can upload the lawmaking provided below.

          /*  * Posted on https://randomnerdtutorials.com  * created by 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 distance NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);    void setup() {    Serial.begin(9600); }   void loop() {    delay(50);    unsigned int distance = sonar.ping_cm();    Series.print(distance);    Serial.println("cm"); }                  

View raw code

How the Code Works

Getting the altitude to an object using the NewPing library is much simpler.

Y'all start by including the NewPing library:

          #include <NewPing.h>        

So, define the trigger and repeat pivot. The trigger pin is connected to the Arduino digital Pin eleven and the echo to Pin 12. Yous too need to define the MAX_DISTANCE variable to be able to use the library.

          #ascertain TRIGGER_PIN 11 #define ECHO_PIN 12 #define MAX_DISTANCE 200        

Then, you create a NewPing example 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 just need to apply the ping_cm() method on the sonar object to get the distance in centimeters.

          unsigned int distance = sonar.ping_cm();        

If you want to get the distance in inches, yous can use sonar.ping_in() instead.

Sit-in

Arduino UNO Board HC-SR04 Ultrasonic Sensor Module Arduino Demonstration

Upload the code to your Arduino lath. Then, open the Serial Monitor at a baud rate of 115200.

The distance to the nearest object is printed in the Series Monitor window.

Arduino with Ultrasonic Sensor Demo Serial Monitor

Wrapping Upward

In this post, nosotros've shown you how the HC-SR04 ultrasonic sensor works and how you lot can utilize it with the Arduino board. For a project example, y'all can build a Parking Sensor with LEDs and a buzzer.

If you are a beginner to the Arduino, we recommend following our Arduino Mini-Class that volition aid you get started quickly with this amazing board.

If you like Arduino, you may also like:

  • Arduino Step-by-step Projects course
  • Guide to SD card module with Arduino
  • Guide to DHT11/DHT22 Humidity and Temperature Sensor With Arduino
  • Guide for TCS230/TCS3200 Colour Sensor with Arduino

You can detect all our Arduino projects and tutorials here.

We promise you found this tutorial useful. Thanks for reading.

everettfultisher.blogspot.com

Source: https://randomnerdtutorials.com/complete-guide-for-ultrasonic-sensor-hc-sr04/

0 Response to "Ultrasound Distance Sensor Reading Slows Down When Zero"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel