Select Page
[otw_is sidebar=otw-sidebar-1]
In this post I will discuss about the ultrasonic sensor and the simulation model of the Ultrasonic sensor in the Proteus library. In the previous post I have discussed the Printed Circuit Board designing in the Proteus and this post will be oriented on the simulation of the ultrasonic sensor in the Proteus.
After reading this post you will be able to learn the simulation of the circuit based on the Ultrasonic sensor and the importance of the simulation model of different modules. So sit back, keep reading and enjoy learning.

[otw_is sidebar=otw-sidebar-2]

#### Simulation Models Libraries

Before diving deep into the discussion on the Ultrasonic sensor and its simulation in the Proteus let us first learn what are the simulation models and why they are so important to be present in the library of the Proteus software.

#### Ultrasonic Sensor Library in Proteus

Before you build the prototype of the circuit it is always beneficial that you simulate it. Simulation is the imitation of the circuit behavior just as it behaves when it is implemented physically in real life. So by simulation of the circuit or embedded system you can pin point the glitches of the coding and also the abnormal behavior of the circuit. There are a lot of the software and online tools available for simulating the circuit or embedded systems, among them Proteus holds the significant position which is equally popular among students and professionals. While simulating any circuit it is important to keep in mind that the simulation models of all the components that are being used in your circuit or embedded system must be present in the library of the software. If you have the component in your circuit whose simulation model is not present in the library of your software then the software will be unable to simulate the behavior of the circuit. In short you must have the simulation models of the components in order to imitate their actual behavior.

#### What is Ultrasonic Sensor

Ultrasonic sensor as the name suggest is based on the Ultrasonic waves and the working principle of this sensor is based on the echo effect. The ultrasonic sensor has a transmitter and the receiver integrated on the same module. The transmitter of the ultrasonic sensor transmits the Ultrasonic waves from the Ultrasonic sensor. When the waves that are being transmitted from the transmitter bounce back after striking any obstacle in the vicinity of the sensor these waves are read by the receiver. The time it takes on transmitting and receiving the waves is measured and used to calculate the distance of the objects from the sensor. The Ultrasonic sensor in the modular form looks like the one in the following image:

[otw_is sidebar=otw-sidebar-3]

Ultrasonic Sensor,Ultrasonic Sensor pinout,Ultrasonic Sensor datasheet

Notice in the figure that the Ultrasonic sensor has four pins in which two of the four pins are used to provide power to the module and the other two pins are control pins which help control the measurement process.

#### Ultrasonic Sensor HC-SR04 Pin Description

Let us now discuss the pin description of the HC-SR04 module pin description:
GND:
This pin is supposed to be connected to the ground pin of the power supply.
Vcc:
This pin is supposed to be connected to the positive end of the power supply.
Echo:
This pin is output in terms of functionality. This pin remains high for the time it takes for the Ultrasonic wave bounce back after striking the obstacle. This pin is supposed to be connected to the input of the Micro controller and thus take account of the time and this distance betwwen the sensor and the obstacle.
Trig:
Trig is the abbreviation of the trigger and this pin is input in terms of functionality. This pin should be kept high for 10 micro seconds in order to initialize the measurement by sending the Ultrasonic waves. This pin is supposed to be connected to the output of the of the microcontroller.

#### Arduino ultrasonic sensor library in proteus

[otw_is sidebar=otw-sidebar-3]

It is always advisable that the circuit should be simulated before developing the Prototype so I will discuss the simulation of the embedded system based on the Ultrasonic sensor. As I described in the above sections that the software should have the simulation model of the component to be simulated in its library otherwise it will not simulate the behavior of the circuit so the simulation model of the Ultrasonic sensor should be available in Proteus library if we want to simulate the behavior of the Ultrasonic sensor in the Proteus. But the simulation model of the Ultrasonic Sensor is not present in the Proteus library by default and we need to download it first. There are a number of resources available online from which you can download the simulation model of the Ultrasonic Sensor. After you have downloaded the simulation model of the ultrasonic sensor it is now time to add the model in the library of the Proteus software. Now pick the component from the library and place it in the workspace as shown in the following figure.

Notice in the figure above that the symbol of the Ultrasonic sensor has an extra pin called the TestPin. This pin is pulled up or pulled down to imitate the bouncing of the Ultrasonic waves as we cannot place the actual component in simulation.

#### Ultrasonic Sensor Library for Proteus

Now let us develop the circuit in order to test the behavior of the Ultrasonic sensor. For this follow the following steps:

##### Step1:

Double click on the symbol and the dialog box will appear as shown in the following figure:

Add the path as shown in the following figure:

#### Circuit for Ultrasonic Sensor

Now draw the circuit as shown in the following figure:

##### Step3:

After you have drawn the schematics in the Proteus it is now time to write the code in Arduino and thus have the HEX file. You can copy and paste the following code in Arduino Integrated Development Environment and can have the HEX file of the code:

// This Code is just written to test the Simulation of the Ultasonic Sensor in the Proteus.
// it may not work for the actual circuit. The Code have been Written by ProjectIoT123.com

```//                                          This Code is just written to test the Simulation of the Ultasonic Sensor in the Proteus.
//                                          it may not work for the actual circuit. The Code have been Written by ProjectIoT123.com

int TrigPin = 7; // Trigger Pin of Ultrasonic Sensor
int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup()
{
pinMode(TrigPin, OUTPUT);

pinMode(echoPin, INPUT);

Serial.begin(9600); // Starting Serial Terminal
}

void loop()
{
long duration, inches, cm;

digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(10);
digitalWrite(TrigPin, LOW);

duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
```

The HEX file of the code can be added in the Proteus Arduino component as shown in the following. If you are not aware of the HEX file and how to add obtain and add HEX file in the Proteus you can read my Article on this Topic.

##### Step4:

Hit the Simulation button to see the values on the Virtual Terminal of the Proteus as shown in the following figure:

#### Obstacle Detection Using Ultrasonic Sensor Arduino Project

in this project we will interfacing ultrasonic sensor with arduino and display on lcd.

```#include <LiquidCrystal.h>

#define trigPin 8
#define echoPin 9
#define led 13
#define led2 12

//                BS  E  D4 D5  D6 D7
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int pos = 0;    // variable to store the servo position

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

lcd.begin(20, 4);

lcd.setCursor(0, 1);
lcd.print("Ultrasonic Sensor Library For Proteus ");
// lcd.print("ROBBOT");

delay(400);
lcd.clear();
delay(1000);
}

void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);  // Added this line
digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 24) {  // This is where the LED On/Off happens
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off

lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Hurdle Detected");
Serial.println(" Hurdle Detected");

delay(400);
lcd.clear();
digitalWrite(led,LOW);
delay(200);
}
else {
digitalWrite(led,LOW);
}
lcd.setCursor(1, 0);
lcd.print("Distance= ");

lcd.print(distance);
lcd.print("cm");

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

delay(500);
}```

##### Related Articles

That is all for now I hope this post would be helpful for you. In the next post I will come up with more interesting topics and simulations in Proteus till then stay connected, keep reading and enjoy learning.