Tutorial

Arduino Uno

Contact us at any time

Door No.510
Samastha jubilee memorial soudham
Mele-thampanoor
Thiruvananthapuram
Kerala-695001
Ph:+91 9633 11 80 80
Email:info@hexcodeplus.com

Enquire and suggestions

Whatsapp +91 9633 11 80 80

Arduino interface with Ultrasonic sensor

Let's study how to interface Ultrasonic sensor with Arduino uno. Refer the connection diagram

Arduino connection



Arduino Program

int trigPin = 4;
int echoPin = 3;
long duration, cm ;

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

void loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  cm = (duration / 2) / 29.1;

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

  delay(250);
}

open arduino New page and upload this code.
after upload the code open arduino Serial monitor

Tools --> Serial Monitor


Follow us

Tutorials / Code