My Ultrasonic Distance Sensor Keeps Reading 0 Cm

This article is a guide about the Ultrasonic Sensor HC-SR04. Nosotros'll explain how it works, bear witness you lot some of its features and share an Arduino projection example 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 make up one's mind the altitude 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 virtually hobbyist projects. In addition, this particular 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

Here'due south a listing 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 Angle: <fifteen°
  • Ranging Distance : 2cm – 400 cm/1″ – 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: thirty caste
  • Trigger Input Pulse width: 10uS TTL pulse
  • Repeat Output Bespeak: TTL pulse proportional to the altitude range
  • Dimension: 45mm 10 20mm x 15mm

How Does it Piece of work?

The ultrasonic sensor uses sonar to decide the altitude to an object. Here's what happens:

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

The time between the manual and reception of the signal allows united states of america to calculate the distance to an object. This is possible because we know the sound's velocity in the air. Here's 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/south

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
Echo Echo Output Pin
GND Common GND

Where to purchase?

You tin can check the Ultrasonic Sensor HC-SR04 sensor on Maker Advisor to notice the best toll:

  • HC-SR04 Ultrasonic Sensor

Arduino with HC-SR04 Sensor

Arduino UNO Board HC-SR04 Ultrasonic Sensor Module Arduino

This sensor is very popular amidst Arduino tinkerers. So, here we provide an example of how to employ 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 you understand how this sensor works. Then, you should be able to use 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 can utilise the preceding links or get directly to MakerAdvisor.com/tools to find all the parts for your projects at the best cost!

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
Repeat Pin 12
GND GND

Lawmaking

Upload the following code to your Arduino IDE.

          /*  * created by Rui Santos, https://randomnerdtutorials.com  *   * Complete Guide for Ultrasonic Sensor HC-SR04  *     Ultrasonic sensor Pins:         VCC: +5VDC         Trig : Trigger (INPUT) - Pin11         Echo: Echo (OUTPUT) - Pin 12         GND: GND  */   int trigPin = eleven;    // Trigger int echoPin = 12;    // Repeat long duration, cm, inches;   void setup() {   //Serial Port begin   Serial.begin (9600);   //Define inputs and outputs   pinMode(trigPin, OUTPUT);   pinMode(echoPin, INPUT); }   void loop() {   // The sensor is triggered by a HIGH pulse of 10 or more microseconds.   // Give a short Low pulse beforehand to ensure a clean High pulse:   digitalWrite(trigPin, LOW);   delayMicroseconds(5);   digitalWrite(trigPin, High);   delayMicroseconds(10);   digitalWrite(trigPin, Depression);     // Read the signal from the sensor: a Loftier 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);     // Convert the fourth dimension into a distance   cm = (duration/2) / 29.one;     // Divide by 29.1 or multiply by 0.0343   inches = (elapsing/2) / 74;   // Separate by 74 or multiply by 0.0135      Serial.print(inches);   Serial.print("in, ");   Series.print(cm);   Series.print("cm");   Serial.println();      delay(250); }                  

View raw code

How the Code Works

First, yous create variables for the trigger and echo pin called trigPin and echoPin, respectively. The trigger pin is continued to digital Pivot 11, and the echo pin is connected to Pivot 12:

          int trigPin = 11;  int echoPin = 12;        

You besides create 3 variables of type long: elapsing and inches. The duration variable saves the time between the emission and reception of the indicate. The cm variable will save the distance in centimeters, and the inches variable will salvage the distance in inches.

          long elapsing, cm, inches;        

In the setup(), initialize the serial port at a baud rate of 9600, and fix the trigger pin as an OUTPUT and the echo pin equally an INPUT.

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

In the loop(), trigger the sensor past sending a High pulse of x microseconds. But, before that, requite a short Depression pulse to ensure you'll get a clean HIGH pulse:

          digitalWrite(trigPin, LOW); delayMicroseconds(5); digitalWrite(trigPin, Loftier); delayMicroseconds(10); digitalWrite(trigPin, Low);        

We apply thepulseIn() function to become the sound wave travel time:

          duration = pulseIn(echoPin, Loftier);        

ThepulseIn() function reads a HIGH or a Depression pulse on a pivot. It accepts as arguments the pin and the state of the pulse (either Loftier or LOW). Information technology returns the length of the pulse in microseconds. The pulse length corresponds to the time information technology took to travel to the object plus the time traveled on the mode back.

So, we summate the altitude to an object, taking into account the audio speed.

          distance = (traveltime/two) x speed of sound          The speed of sound is: 343m/s = 0.0343 cm/united states of america = one/29.1 cm/united states of america  Or in inches: 13503.9in/s = 0.0135in/uS = 1/74in/uS

We need to divide the travel fourth dimension by 2 because we have to consider that the wave was sent, hit the object, and then returned to the sensor.

          cm = (duration/ii) / 29.i; inches = (elapsing/ii) / 74;        

Finally, we print the results in the Serial Monitor:

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

Source code with NewPing Library

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

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

          /*  * Posted on https://randomnerdtutorials.com  * created by http://playground.arduino.cc/Lawmaking/NewPing */  #include <NewPing.h>   #ascertain TRIGGER_PIN 11 #ascertain ECHO_PIN 12 #define MAX_DISTANCE 200  // NewPing setup of pins and maximum distance NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);    void setup() {    Series.begin(9600); }   void loop() {    delay(50);    unsigned int distance = sonar.ping_cm();    Serial.impress(distance);    Series.println("cm"); }                  

View raw code

How the Code Works

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

You start past including the NewPing library:

          #include <NewPing.h>        

Then, ascertain the trigger and echo pin. The trigger pin is continued to the Arduino digital Pin 11 and the echo to Pin 12. You also need to define the MAX_DISTANCE variable to be able to employ the library.

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

Then, you create a NewPing example called sonar:

          NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);        

In the setup(), yous initialize the Series communication at a baud rate of 9600.

          Serial.begin(9600);        

Finally, in the loop(), you just need to use the ping_cm() method on the sonar object to go the distance in centimeters.

          unsigned int distance = sonar.ping_cm();        

If y'all want to go the distance in inches, you tin apply sonar.ping_in() instead.

Demonstration

Arduino UNO Board HC-SR04 Ultrasonic Sensor Module Arduino Demonstration

Upload the lawmaking to your Arduino board. Then, open the Series Monitor at a baud charge per unit of 115200.

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

Arduino with Ultrasonic Sensor Demo Serial Monitor

Wrapping Upwards

In this mail service, we've shown you how the HC-SR04 ultrasonic sensor works and how you can employ it with the Arduino board. For a project example, you tin build a Parking Sensor with LEDs and a buzzer.

If y'all are a beginner to the Arduino, we recommend post-obit our Arduino Mini-Course that will help you get started chop-chop with this amazing board.

If yous like Arduino, you may also similar:

  • Arduino Step-past-footstep Projects course
  • Guide to SD card module with Arduino
  • Guide to DHT11/DHT22 Humidity and Temperature Sensor With Arduino
  • Guide for TCS230/TCS3200 Color Sensor with Arduino

You can detect all our Arduino projects and tutorials here.

Nosotros hope you found this tutorial useful. Thanks for reading.

martinezreanday.blogspot.com

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

0 Response to "My Ultrasonic Distance Sensor Keeps Reading 0 Cm"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel