Sponsor Link:

UTSource.net Reviews

It is a trustworthy website for ordering electronic components with cheap price and excellent quality.

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:

Arduino HC-SR04 Ultrasonic Sensor Project Code

#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);
}

 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, PCBWay has an amazing website, online Gerber viewer function and a gift shop so make sure to check out their links below:

PCBWay Free Online Gerber Viewer Function:  https://www.pcbway.com/project/OnlineGerberViewer.html

PCBWay Gift Shop: https://www.pcbway.com/projects/gifts.html

Enjoy! Contact us for any inquiries!