user1891676
Published © GPL3+

Interfacing ultrasonic sensor with Arduino

This is a basic tutorial on how to interface ultrasonic sensor (HCSR04) with Arduino uno. So let's start !!!

BeginnerFull instructions provided11,330
Interfacing ultrasonic sensor with Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit digram

This is the circuit diagram for interfacing ultrasonic sensor and Arduino uno made by using the software tinkercad.

Ultrasonic sensor with arduino

Code

Code

C/C++
This is code for the interfacing ultrasonic sensor with Arduino uno.
// Interfacing Ultrasonic sensor with Arduino uno


#define echoPin 12 //connect echo pin of ultrasonic sensor to D12 of Arduino
#define trigPin 10 //connect trigger pin of ultrasonic sensor to D10 of Arduino

long duration;  // declare variables to hold duration and distance
int distance;

void setup() //setup() is used for initialization

{
  Serial.begin(9600);  //set the baud rate of serial communication to 9600
  pinMode(trigPin,OUTPUT); //set trigPin as output pin of Arduino
  pinMode(echoPin,INPUT);  //set echoPin as output pin of Arduino
 }
void loop(){
  digitalWrite(trigPin,LOW); //generate square wave at trigger pin
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  
 duration=pulseIn(echoPin,HIGH);//calculation of distance of obstacle
  distance=(duration*0.034/2);
  Serial.print("Distance : ");
  Serial.print(distance);
  Serial.println(" cm ");
  delay(1000);
               
}

Credits

user1891676

user1891676

0 projects • 0 followers

Comments