Matha Goram
Published © GPL3+

What Is the Length?

A simple way to measure distance to an object is to bounce an ultrasonic pulse using an inexpensive sensor.

BeginnerFull instructions provided30 minutes870
What Is the Length?

Things used in this project

Hardware components

Elegoo Arduino UNO R3
×1
Elegoo HC-SR504 Ultrasonic Sensor
×1
Breadboard (generic)
Breadboard (generic)
×1
Elegoo DuPont wires, male-to-male
×6
Baseplate
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

Timing diagram

Schematics

Schematic diagram

Assembly diagram

Wiring complete

Code

Elegoo HC-SR504 Basic Example

Arduino
#include <SR04.h>

/*
 * ElegooHC-SR501-01.ino
 * Measure distance using the HC-SR504 ultrasonic sensor
 * adapted from examples at https://www.elegoo.com/
 * 2018-09-12
 * armw
 * v0.1
 * � 2018 <reza@parkcircus.org> All Rights Reserved
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * Notes
 *   HC-SR504:
 *     Pin #  Description
 *     1      VCC
 *     2      Trigger
 *     3      Echo
 *     4      GND
 *   Datasheet: http://elecfreaks.com/estore/download/EF03085-HC-SR04_Ultrasonic_Module_User_Guide.pdf
 *   Distance = Ultrasonic (air) velocity * EchoDetectionTime/2
 */

#define pinTrig 6                 // pin to initiate transmission of sound pulse
#define pinEcho 7                 // pin to receive the signal of echo from the sound pulse
#define blinkPeriod 1000          // nominal delay between discrete activities, microseconds

SR04 mySensor = SR04(pinEcho, pinTrig); // instatiation of object to retain sensor data

void setup()                        // run only once
{
  Serial.begin(115200);             // default initialize of serial port that will be used to display distance readout
  delay(blinkPeriod);               // wait for 1,000 microseconds
  Serial.println("HC-SR504 simple test using Elegoo SR04 library.");
}

void loop()                         // run indefinitely
{
  int myDistance = mySensor.Distance(); // obtain estimated distance using pulse/echo signals
  Serial.print("Distance = ");
  Serial.print(myDistance);
  Serial.print(" cm, = ");
  myDistance = 0.3937008 * myDistance; // convert from centimeters to inches
  Serial.print(myDistance);
  Serial.println(" in");
  delay(blinkPeriod);               // pause
}

Credits

Matha Goram
27 projects • 23 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.
Contact

Comments

Please log in or sign up to comment.