Skip to main content

Featured Post

Kerala's First 3D Printed Building

Created as a showcase project, AMAZE-28, the single-room summer house, was successfully constructed within 28 days on the grounds of the Kerala State Nirmithi Kendra. The 3D-printed building at the Kerala State Nirmithi Kendra in Thiruvananthapuram. (Photo: Shekunj)  The inauguration of Kerala's inaugural 3D-printed structure, a 380-square-foot single-room summer house, is scheduled to take place on October 10 at the Kerala State Nirmithi Kendra (Kesnik) campus located in PTP Nagar, Thiruvananthapuram.  Conceived as a showcase initiative, the summer house named AMAZE-28 was successfully finished within a mere 28 days. This impressive project was executed by Tvasta, a construction technology startup based in Chennai, founded by alumni of IIT-Madras, who have entered into a memorandum of understanding (MoU) with Kesnik.  AMAZE-28 is perched upon a concrete foundation atop a gentle elevation within the Kesnik campus. Febi Varghese, the Director and Chief Executive Officer of...

Using the ultrasonic sensor with Arduino - Complete Guide in 2023

 Ultrasonic sensors have become increasingly popular in recent years, particularly in the field of IoT. They are used in a wide range of applications such as distance measurement, object detection, and obstacle avoidance. The ultrasonic sensor works by emitting high-frequency sound waves and then measuring the time it takes for the waves to bounce back. This information can then be used to calculate the distance between the sensor and the object.

In this article, I will provide a complete guide on using an ultrasonic sensor with Arduino. I will discuss the principles behind ultrasonic sensors and provide a step-by-step guide on how to connect and use the ultrasonic sensor with Arduino. I will also provide a circuit diagram and complete code to help you get started.

Principles Behind Ultrasonic Sensors HC-SR04

Ultrasonic sensors work by emitting high-frequency sound waves that are beyond the range of human hearing. These sound waves bounce off any object in their path and return to the sensor. By measuring the time it takes for the sound waves to return, the sensor can determine the distance between the object and the sensor.

The ultrasonic sensor consists of two main parts, the transmitter and the receiver. The transmitter emits the sound waves, while the receiver detects the waves that bounce back. The sensor calculates the distance by measuring the time it takes for the sound waves to travel to the object and back.


Common Arduino Problems and How to Fix Them – Complete Detail


Using Ultrasonic Sensor with Arduino

Now that we understand the principles behind ultrasonic sensors let's move on to connecting and using the sensor with an Arduino.

Hardware Required

Ultrasonic Sensor with Arduino Circuit Diagram

To get started, we need to connect the ultrasonic sensor with Arduino. Follow the circuit diagram below to make the necessary connections:

Ultrasonic Sensor HC-SR04 PinsArduino UNO Pins
VCC+5V
GNDGND
TRIGD9
ECHOD10

The ultrasonic sensor HC-SR04 has four pins: VCC, GND, TRIG, and ECHO. Connect the VCC and GND pins of the sensor to the +5V and GND pins of the Arduino, respectively. Connect the TRIG and ECHO pins of the sensor to pins 9 and 10 of the Arduino, respectively.

 


Ultrasonic Sensor Arduino Code

Once you have made the necessary connections, it's time to upload the ultrasonic sensor code to the Arduino. Open the Arduino IDE and copy the following code:

#define trigPin 9
#define echoPin 10

void setup() {
  Serial.begin(9600); // initialize serial communication
  pinMode(trigPin, OUTPUT); // set the trig pin as output
  pinMode(echoPin, INPUT); // set the echo pin as input
}

void loop() {
  // set trig pin to high for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // measure the time taken for the sound wave to bounce back
  long duration = pulseIn(echoPin, HIGH);

  // calculate the distance based on the time taken
  float distance = duration * 0.034 / 2;

  // print the distance to the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // wait for 500 milliseconds before taking another measurement
  delay(500);
}

The code above initializes the serial communication and sets the TRIG and ECHO pins as output and input, respectively. In the loop function, the code sends a sound wave from the TRIG pin and measures the time taken for the wave to bounce back using the ECHO pin. The distance is then calculated based on the time taken and printed to the serial monitor. Finally, the code waits for 500 milliseconds before taking another measurement.


Speed Control of DC Motor using Arduino and L293D Motor Driver


Explanation of Code

Let's take a closer look at the code and how it works.

#define trigPin 9
#define echoPin 10

Here, we define the trig and echo pins as 9 and 10, respectively.

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

In the setup function, we initialize the serial communication and set the trig and echo pins as output and input, respectively.

void loop() {
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);

  float distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}

In the loop function, we send a 10 microsecond pulse from the TRIG pin. The sound wave then travels to the object and bounces back to the sensor. The duration of the pulse is measured using the pulseIn function and stored in the duration variable.

The distance is then calculated using the formula

distance = duration * 0.034 / 2

This formula is based on the speed of sound in air, which is approximately 340 m/s. Since the sound wave has to travel to the object and back, we divide the duration by 2. The result is the distance in centimeters.

Finally, we print the distance to the serial monitor and wait for 500 milliseconds before taking another measurement.

Conclusion

In this article, we discussed the principles behind ultrasonic sensors and how to use them with an Arduino. We provided a step-by-step guide on how to connect the sensor to the Arduino and wrote a complete code to measure the distance to an object using the sensor. With this knowledge, you can now start experimenting with ultrasonic sensors and develop your own projects.

Note: It's important to note that the ultrasonic sensor HC-SR04 sensor requires a 5V power supply and is not compatible with 3.3V Arduino boards. Make sure you check the voltage compatibility of your components before connecting them.

FAQs

  1. Can I use any ultrasonic sensor with Arduino? No, not all ultrasonic sensors are compatible with Arduino. You need to make sure that the sensor you are using operates at 5V and has separate TRIG and ECHO pins. The HC-SR04 sensor is a popular choice for Arduino projects and is easy to use.
  2. How accurate are ultrasonic sensors? The accuracy of ultrasonic sensors depends on a variety of factors such as the quality of the sensor, the distance to the object being measured, and environmental conditions such as temperature and humidity. Generally, ultrasonic sensors are accurate to within a few millimeters.
  3. What is the maximum range of an ultrasonic sensor? The maximum range of an ultrasonic sensor varies depending on the model and specifications of the sensor. The HC-SR04 sensor has a maximum range of 4 meters, while some other sensors can measure distances up to 10 meters or more.
  4. How do I troubleshoot my ultrasonic sensor? If you are having trouble getting accurate readings from your ultrasonic sensor, check the wiring to make sure it is connected correctly. Also, make sure that the sensor is powered with a stable 5V supply. If the problem persists, you can try using a different sensor or adjusting the code to improve the accuracy of the readings.
  5. Can I use multiple ultrasonic sensors with one Arduino board? Yes, you can use multiple ultrasonic sensors with one Arduino board by connecting each sensor to a separate set of TRIG and ECHO pins. You can then modify the code to take readings from each sensor in sequence or simultaneously, depending on your requirements.

Note - Some of the links in this article are affiliate links, which means that if you click on them and make a purchase, I may receive a commission. The commission I receive helps support the maintenance of this website. Thank you for your support!

Comments

Popular posts from this blog

Controlling LEDs over WiFi using NodeMCU and Blynk App.

This article is the base on the Internet of things ( IoT ) . IoT describes the network of physical objects—“things” or objects—that are embedded with sensors, software, and other technologies for the purpose of connecting and exchanging data with other devices and systems over the Internet. In this article 3 LEDs will be controlled by an android application (made using Blynk) over a wifi connection. How to use NodeMCU with Blynk  If you want to start learn the Internet of Things (IoT) concept, then controlling a LED over the  internet is the " Hello World!" of the IoT journey. Performing the " Hello World" task will be easy using NodeMCU micro-controller as the first client and Blynk as the 'broker' or server and Blynk android application as the second client. Now communication between two clients will happen through broker over the internet. Now lets, make this happen.  Components Required For this activity we will need following components: Nod...

How to Know the NodeMCU IP Address for Your Next IoT Project

  NodeMCU IP Address: The NodeMCU is a popular development board for IoT projects. It’s small, affordable, and comes with built-in Wi-Fi connectivity, making it the perfect choice for creating connected devices. But before you can start building your NodeMCU projects, you need to know the IP address of your device. This IP address is essential for communicating with the NodeMCU from another device, such as a computer or smartphone. In this article, we’ll show you how to find the NodeMCU IP address, so you can get started with your next IoT project. Method 1: To Know the NodeMCU IP Address One of the easiest ways to find the NodeMCU IP address is by using the serial monitor in the Arduino IDE. First, connect your NodeMCU board to your computer using a micro-USB cable. Then, upload the following code to the NodeMCU: #include <ESP8266WiFi.h> void setup() { Serial.begin(115200); Serial.println(); Serial.print("Connecting to "); Serial.println(...

Blinking of LED Using Arduino UNO

Hi !!!! Here we are going to learn very very simple use of Arduino, that is blinking of a LED. Here we will do simplest project using Arduino. So, here we go.... COMPONENTS REQUIRED - Bread Board Arduino UNO Jumper Wires Power Source (5V) / Battery Resistor (1K) CIRCUIT PREPA RATION - Take bread board and Arduino. Connect the +5V from Arduino to the +Ve line on Bread Board and GROUND from Arduino to -Ve line on bread board. Red Wire     : +5 V Black Wire  : GROUND     Now put a LED on bread board. Connect the Anode (+ / Longer terminal) of led to the pin 13 of Arduino. Connect Cathode (- / Shorter terminal)  to 1K ohm resistor and connect another terminal of resistor to the GND on breadboard.   Now our circuit is ready to work.  Our next aim is to build the code. Let's make our code. CODE -     To download the complete code -  CLICK HERE -----------------------------...