Arduino Tutorials

How to simulate HC-SR04 ultrasonic sensor on Proteus

The HC-SR04 is a popular ultrasonic distance sensor that is used in project designs to measure distance. I have used it to design a distance measurement device and also a water level indicator using Arduino. But, do you know you can simulate ultrasonic sensor projects on Proteus before carrying out the real design? Watch the video below and see how it is done.

To do that, you should have Proteus already installed on your computer system, then add Arduino and ultrasonic sensor libraries to the Proteus library folder, once you have done that, you open Proteus and draw the circuit diagram as shown below.

HC-SR04 simulation on proteus
Figure 1: HC-SR04 simulation on Proteus

You must make sure that the virtual terminal has same baud rate as the Arduino code as explained in the video, to see how to do that and how to upload the Arduino code to the virtual Arduino used on the Proteus watch the video above. The Arduino code for the simulation is shown below.

// defines pins numbers
const int trigPin = 10; // pin on the arduinowhere the trigger pin is connected
const int echoPin = 9;// pin on the arduino where the echo pin is connected

// defines variables
long duration; // variable where the the reflection time of the ultrasound is stored
int distance; // variable where the distance of the measured object is stored


void setup()
{
         pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
           pinMode(echoPin, INPUT); // Sets the echoPin as an Input
         Serial.begin(9600); // Starts the serial communication
         }
     
     void loop()
 {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
     
     // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
     
// Reads the echoPin, returns the sound wave travel time in microseconds
   duration = pulseIn(echoPin, HIGH);
   
// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay (100);

 }

You can as well check out other nice Proteus simulation videos like: how to simulate a step-down transformer, how to simulate Arduino on Proteus.

Tags

Related Articles

Leave a Reply

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

Back to top button
Close