Fouad_Roboticist
Published

Range Finder

Setting Up Ultrasonic Sensor on Arduino

BeginnerFull instructions provided370
Range Finder

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Breadboard, Plastic
Breadboard, Plastic
Premium Male/Male Jumper Wires, 40 x 3" (75mm)
Premium Male/Male Jumper Wires, 40 x 3" (75mm)

Story

Read more

Custom parts and enclosures

Components Required

Serial Monitor

Extracting Data

Schematics

Circuit Diagram

Code

Ultrasonic.ino

Arduino
// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3
// Define variables:
long duration;
int distance;
void setup() {
  // Define inputs and outputs:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //Begin Serial communication at a baudrate of 9600:
  Serial.begin(9600);
}
void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  // Calculate the distance:
  distance= duration*0.034/2;
  // Print the distance on the Serial Monitor
  Serial.print("Distance = ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000);
}

Credits

Fouad_Roboticist

Fouad_Roboticist

1 project • 1 follower

Comments