Ultrasonic sensor
Hc-sr04 Ultrasonic sensor

 HC-SR04 Ultrasonic Sensor Module 

Hello friends, In my blog I will show you how to create a connection between a hc-sr04 ultrasonic sensor and the Arduino board and here you can also find the introduction, working principle, applications, specificatoins, pinout & explanation, circuit diagram, test code, code explanation, Video Tutorial and data sheet of the ultrasonic sensor. 

what is the ultrasonic sensor?

An utrasonic sensor is a proximity sensors, which is used to measure the distance without making contact with objects as they emit ultrasound waves and When an object detects a wave, it reflects it to the receiver. Calculating the distance to an object is possible by measuring the amount of time taken between transmitting and receiving the signal. Sensors can detect distances between 2 cm to 400 cm.

It can be used with a variety of microcontrollers like Arduino, raspberry pi, esp8266, Teensy and other.

Working of Ultrasonic sensor?

Ultrasonic sensors transmit the signal by trigpin and receive it by echopin. It transmits the frequency of 40khz by trigger pin and when it detects an object, the sound signals reflects back to echopin. Calculating the distance to an object is based on the duration between the transmission and reception of the signal.
            Formula: Distance = Speed x Time

Working of ultrasonic sensor

 Hc-sr04 Specifications: 

Hc-sor4 sensor Values
1. Operating Voltage 5V DC
2. Operating Current 15mA
3. Measuring Distance 2 to 450 cm
4. Accuracy 3mm
5. Measuring Angle Covered 15 degree
6. Frequency 40Hz
7. Dimension 45 x 20 x 15mm


 Aplication of Hc-sr04 ultrasonic sensor: 
  1.  Water Level detector.
  2.  Obstacle avoid robot.
  3.  path-finding robots.
  4.  Measures distance like a scale.
  5.  HC-SR04 security alarm alerts you by sounding a buzzer when anyone approaches it.
  6.  Rotating the sensor allows mapping objects surrounding it.

 Hc-sr04 Pinout: 
ultrasonic sensor pinout

Pins Description
 1. VCC  That's the power supply for an HC-SR04 sensor.
  2. Trig  Sound waves are transmitted via it.
  3. Echo  A sound wave that is reflected by an object is used to receive the sound.
  4. GND  Ground is where the voltage level is 0v.

 Component Requirements:  

1. The Arduino Uno is the brian of the projects.
2. Hc-sr04 ultrasonic sensor is used to measure distance.
3. LEDs are used as indicators.
4. A resistor of 220 ohm is used to protect the led from high currents.
5. The jumper wire and breadboard are used to make the connection. 

 HC-sr04 Circuit diagram: 

Hc-sr04 wiring and coding

1. Start by connecting an Arduino Uno to a breadboard.
  • Connect Arduino 5v pin to breadboard positive side.
  • Connect Arduino GND to the breadboard GND side.
2. Make the connection between the Hc-sr04 ultrasonic sensor and the Arduino uno.
  • Connect the Hc-sr04 GND to pin GND of the Arduino.
  • Connect the Hc-sr04 VCC to pin 5v of the Arduino
  • Connect the Hc-sr04 TrigPin to pin 12 of the Arduino
  • Connect the Hc-sr04 EchoPin to pin 13 of the Arduino
3. A 220-ohm resistor should be connected to the positive pins of the LEDs to protect them from high currents.
  • Connect the resistor end to pin 11 of the Arduino.
The circuit has now been completed.
4. Let's get started with coding.
5. Launch the Arduino IDE.

 Hc-sr04 Utrasonic Sensor Arduino Code: 

1. The first thing you need to do is install the NewPing library.
/*
 * Posted on https://www.mrelectrouino.com/
 * Follow us on Instagram: _mr_electrouino_
 */

#include <NewPing.h>

#define TRIGGER_PIN 12
#define ECHO_PIN 13

#define MAX_DISTANCE 400

#define Led 11 

// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

float distance;

void setup() 
{
	Serial.begin(9600);
	pinMode(Led, OUTPUT);
}

void loop() 
{
	// Send ping, get distance in cm
	distance = sonar.ping_cm();
	
	Serial.print("Distance = ");
	
	if (distance >= 400 || distance <= 2)
	{
		Serial.println("Out of range");
	}
	else if (distance < 20){
		digitalWrite(Led, HIGH);
		Serial.println("Object is detected");
	}
	else{
		digitalWrite(Led, LOW);
		Serial.print(distance);
		Serial.println(" cm");
	} 	
	delay(500);
}

2. You will need a USB cable to connect the Arduino Uno to the computer.
3. Goto tool --> Board --> Arduino AVR board --> Select your Arduino board.
4. Goto tool --> port --> select your port
5. The code is now ready to be upload.
6. Once the code has been uploaded, go to the serial monitor and check the values.

 Hc-sr04 Code Explanation: 

The library provides a simple way to get the distance from the sensor. To use its benefits, we must include it.
#include <NewPing.h>

As a next step, Set the pins of the ultrasonic sensor. The trigger pin should be connected to arduino pin 12 and the echo pin to arduino pin 13 and define the maximum range of the sensor is 400 cm.
You should also define the led that should be connected to arduino pin 11.
#define TRIGGER_PIN 12
#define ECHO_PIN 13
#define MAX_DISTANCE 400
#define Led 11 

Initialize an ultrasonic device, to use pin 12 as trigger output, pin 13 as echo input, with a maximum distance of 200 cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

A distance variable is defined as a float that is used to store distance values.
float distance;

The setup() function executes the code once. Serial.begin() is used to initialize serial communication to display results on the serial monitor. We need to set its baud rate to 9600. The baud rate should be set to 9600. Also set the Led as a output.
void setup() 
{
	Serial.begin(9600);
	pinMode(Led, OUTPUT);
}

The loop() function allows us to run the code again and again. 
The ping_cm() function returns the distance in centimeters, while the ping_in() function returns the distance in inches. 
distance = sonar.ping_cm();

When the distance is greater than 400 or less than 2 cm, it will print the object is out of range.
if (distance >= 400 || distance <= 2)
{
	Serial.println("Out of range");
}

If the distance is less than 20 cm, the LED will be on, otherwise it will be off and the distance will be printed in centimeters.
else if (distance < 20) {
	digitalWrite(Led, HIGH);
	Serial.println("Object is detected");
} else {
	digitalWrite(Led, LOW);
	Serial.print(distance);
	Serial.println(" cm");
} 
Wrapping up:
I hope this information helps you to test the ultrasonic sensor (hc-sr04) by arduino uno. If you have any problem free feel to comment it. Will reply it as soon as possible. 

Thank you for reading!

 HC-sr04 Project Tutorial: 


 LinKs

Arduino ultrasonic sensor led projects
Hc-sr04 Datasheets.
Fritzing: Hc-sr04 Ultrasonic sensor.
Hc-sr04 Ultrasonic sensor test.
arduino sensor test.
how to test sensors

Post a Comment

Previous Post Next Post