Welcome Guest | RSS

InloveTech

Friday, 2024-04-19, 6:11 PM

Ultrasonic Measuring Device:


(for information on this sensor, click 
Things needed:-
1) Arduino
2) Ultrasonic Sensor(in this project HC SR04 model is used)

Connections:-

There are 4 pins on this Ultrasonic sensor.
Connect VCC        to arduino 5V
               GND        to arduino GND
               ECHO      to arduino pin 12
               TRIGGER  to arduino pin 13

[Note:- These sensor pins arrangement is for HC SR04 model. The pins may vary for some models.]

Coding:-

/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://inlovetech.ucoz.com
 */

#define trigPin 13
#define echoPin 12

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

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line

  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}