How to Use Ultrasonic Sensor With Arduino Nano

by STEAM-DIY in Circuits > Arduino

144 Views, 0 Favorites, 0 Comments

How to Use Ultrasonic Sensor With Arduino Nano

121212.png

Welcome back to my blog. We have described INPUT and OUTPUT in previous posts. We will create projects using this knowledge. Ok, let’s go to today’s post. Today we are going to talk about how to connect an ultrasonic sensor to an Arduino board. So it is a very useful sensor for making robots. This sensor can be easily connected to an Arduino board.

Supplies


What is the ultrasonic sensor?

The main process of this sensor is to calculate the distance to an obstacle. The sensor emits a wave and calculates the distance by the time it takes to reflect in the face of an obstacle. For example, the reader system and the bats used this method. This ultrasonic sensor includes two knobs. One knob emits a pulse and the other knob detects the pulse. This sensor cannot be used for long distances. The maximum value is 25-30 cm. I am not satisfied with the values ​​beyond that. It is more suitable for short distances. If you need long-distance sensors, there are various sensors available in the market. Waterproof sensors are also available in the market. Below is how the ultrasonic sensor works with the Arduino board.

Step 1

 let’s do this project step by step. The required components are given below.

  1. Arduino Nano board
  2. Ultrasonic sensor
  3. Breadboard  
  4. Jumper Wire

Firstly, identify these components.

Arduino Nano board

The Arduino Nano board is a very low-cost board. That is why I have used this board. You can use any other Arduino board for this project.

Ultrasonic sensor

This component has been described above.

Breadboard

This is used for the easy mounting of the component.

Jumper wires

I have used four male-to-male upper wires.

USB cable

It is used to connect the Arduino board to the computer.

Secondly, connect the ultrasonic sensor and the Arduino board to the breadboard.


Thirdly, connect the ultrasonic sensor to the Arduino board using the jumper wire. For that, see the circuit diagram below.

Connect the TRIG pin of the ultrasonic sensor to the D2 pin and the ECHO pin to the D4 pin. The GND pin connects to the GND pin and the VCC pin connects to the VIN or 5v pin.

Now, copy and paste the following program to the Arduino IDE.

https://youtube.com/@steam-diy?si=XOvJ1l3gY9-QSK6G (get the code from the link )

  1. The complete program of this project
void setup() {
pinMode(2, OUTPUT);//define arduino pin
pinMode(4, INPUT);//define arduino pin
Serial.begin(9600);//enable serial monitor

}
void loop() {
//pulse output
digitalWrite(2, LOW);
delayMicroseconds(4);
digitalWrite(2, HIGH);
delayMicroseconds(10);
digitalWrite(2, LOW);

long t = pulseIn(4, HIGH);//input pulse and save it veriable

long inches = t / 74 / 2; //time convert distance
long cm = t / 29 / 2; //time convert distance
String inch = " inches t";
String CM = " cm";

Serial.print(inches +inch);//print serial monitor inches
Serial.println(cm +CM);//print serial monitor cm
Serial.println();//print space
delay(100);//delay
}

Let’s look at this code one by one.

void setup() {
pinMode(2, OUTPUT);//define arduino pin
pinMode(4, INPUT);//define arduino pin
Serial.begin(9600);//enable serial monitor

}

This code informs the Arduino board that pin D2 is the output pin and pin D4 is the input pin. Also, the serial monitor is activated.

long t = pulseIn(4, HIGH); -- This code gets the pulse time and it saves in the long variable.
long inches = t / 74 / 2; ---- This code converts time into inches.
long cm = t / 29 / 2; --- This code converts time into centimeters.
String inch = " inches t";
String CM = " cm";
Serial.print(inches +inch);
Serial.println(cm +CM);
Serial.println();
delay(100);

This code prints the values ​​to the serial monitor.

Next, select the board and port. After, click the upload button.

Now, open the serial monitor. Then you can see the ultrasonic readings.

OK, enjoy this project. The full video guide is below. So, we hope to see you in the next project or tutorial. Have a good day.