Ultrasonic sensor with Arduino Nano – How does work Ultrasonic sensor

Ultrasonic sensor with Arduino Nano

        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.

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. The components required for this project and how to purchase these components are given below. we can use the serial monitor to watch the sensor readings.

Components

Step 1
Let’s identify the components one by one.
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
Connect the ultrasonic sensor and the Arduino board to the breadboard.

Step 3 

OK, we 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 the GND pin and the VCC pin connects to the VCC or 5v pin.
Step 4
Okay, let’s look at the code below.
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

Select board and port.
 

Step 6

Now, upload this code and run the Serial monitor.

OK, We did these projects. I hope you enjoy this project. Please comment below on all questions. The full video of this project is below. Please watch it. We will meet in the next post. Have a good day. Bye-bye.

Leave a Reply

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