Project 002: Arduino HC-SR04 Ultrasonic Sensor Project

Project 002: Arduino HC-SR04 Ultrasonic Sensor Project © GPL3+

A beginner to mid-level project utilising an inexpensive ultrasonic sensor, an Arduino and some intermediate code language.

  • 134 views
  • 0 comments
  • 4 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
You could use any other Arduino board as well. The Keyestudio Uno is used in this example.
×1
Adafruit industries ada62 image 75px
USB-A to B Cable
Depends on the Arduino.
×1
11026 02
Jumper wires (generic)
4 Male to Male Jumper Wires.
×4
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
12002 04
Breadboard (generic)
×1
Pcbway qeheggiidg
PCBWay Custom PCB
×1

Apps and online services

About this project

Before starting this project...

Credits go to Netram Technologies

This basic project is very essential especially if you are a beginner. It can be useful in the future when building security systems, collecting data, or making a robot. The code is very accurate, measuring in centimetres. If you didn't know, an ultrasonic sensor works when the waves it sends out bounces on the nearest object to it and bounces back measuring the distance between the two objects. This doesn't use much parts but the main part is the HC-SR04, a ultrasonic sensor. This specific sensor is good quality and can measure far distances easily. It can be found almost every where including various online stores. The parts needed in this project is:

  • 1 Arduino (the Keyestudio Uno is used in this example)
  • 1 USB Data Cable (depends on the Arduino)
  • 4 Jumper Wires (Male to Male)
  • 1 HC-SR04​ Ultrasonic Sensor​
  • 1 breadboard

Mounting the circuit

Let's start mounting the circuit. Always unplug the USB cable out of your computer for safety reasons. First, place the ultrasonic sensor horizontally onto the breadboard. Behind, the sensor, insert the 4 jumper wires into the pins. Connect the wire that is behind the VCC (+)on the sensor into 5v (+5 volts)on the Arduino. Next, connect the wire that is behind the TRIG (signal)pin on the sensor into D8 (digital pin 8). Then, connect the wire behind ​ECHO on the sensor to D7 (digital pin 7). Finally connect GND (-) on the sensor to GND(-) on the Arduino. Use the code below and open the serial monitor to 9600 bauds. You should see the results coming in through the sensor and onto serial monitor after uploading the code. Use the code below:​

About the code

The three first lines of the code are basically defining some of the ultrasonic sensor pins, it states which pins connect to which on the Arduino. The next three lines tell you what is the maximum and minimum distance the ultrasonic sensor can reach. Then we reach void setup, which only runs once. The first line of the void setup tells the serial monitor to start under 9600 bauds. Below, it defines whether the pins are sending info to the computer (OUTPUT) or sending information to the sensor (INPUT). The lines after void setup but before void loop will run only once at the beginning as a setup. Next comes void loop, which runs over and over again. The lines after void loop are going to loop while the code is running. In void setup, you are setting up the ultrasonic sensor and the serial monitor. In void loop, the different pins of the ultrasonic sensor are either on or off. The pins send information from the readings to be converted for us to read. One of the lines of codes mentions about the distance and whether it is going to show in centimetres. In the serial monitor, there's also a line of code which displays an 'out of range' message. The others tell you how long the distance is from the ultrasonic sensor to the nearest object in its perimeter. This is a basic explanation of the code done.

Amazing opportunities

Also, be sure to check out PCBWay, a leading manufacturer and distributor in PCB design and manufacturing. They have amazing prices and excellent quality in their services, so don't miss out on them! Plus, right now, they are having an amazing Christmas promotion, where free gifts and packages will be gifted, so make sure you check out the link below:

https://www.pcbway.com/PCBWayShoppingFestival2018.aspx

Enjoy! Contact us for any inquiries!

Code

Arduino HC-SR04 Ultrasonic Sensor Project CodeC/C++
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
  
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
  
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
  distance = duration/58.2;
  if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
    Serial.println("Can't read");
    digitalWrite(LEDPin, HIGH);
}
else {
  
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
  Serial.println(distance);
  digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(50);
}

Schematics

Circuit Diagram
Hmg pf6vzwebgz
Schematics
Hlyi bshyyan6xq

Comments

Similar projects you might like

Ultrasonic Sensor with LCD Display Using 433MHz

Project tutorial by mcharrison500

  • 3,419 views
  • 0 comments
  • 16 respects

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 5,097 views
  • 7 comments
  • 19 respects

IOT - Smart Jar Using ESP8266, Arduino and Ultrasonic Sensor

Project tutorial by Team Smazee

  • 6,212 views
  • 4 comments
  • 12 respects
Add projectSign up / Login