Ultrasonic sensor with Arduino Nano – How does work Ultrasonic sensor

Hello guys, 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.

(adsbygoogle = window.adsbygoogle || []).push({});

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.

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

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

(adsbygoogle = window.adsbygoogle || []).push({});

Step 1

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.

Step 2

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

Step 3

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

(adsbygoogle = window.adsbygoogle || []).push({});

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.

Step 4

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

  • The complete program of this project – Download
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.

Step 5

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

Step 6

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.

Ultrasonic sensor with Arduino Nano – How does work Ultrasonic sensor

Similar Posts

2 Comments

  1. I am a retired senior from India. I have started to learn to make Arduino projects from your Videos. i love to watch your understandable and simple language. I will try to learn coding step by step. Thank you.
    I want to make a project to display the distance distance on lcd

Leave a Reply

Your email address will not be published. Required fields are marked *