0% found this document useful (0 votes)
2 views

write a Arduino program for implement Distance measure using Ultrasonic sensor

The document provides an Arduino program to measure distance using an ultrasonic sensor. It defines the necessary pins for triggering and receiving the ultrasonic pulse, sets up the serial communication, and calculates the distance based on the time taken for the pulse to return. The measured distance is then printed to the serial monitor in centimeters.

Uploaded by

shihankv807
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

write a Arduino program for implement Distance measure using Ultrasonic sensor

The document provides an Arduino program to measure distance using an ultrasonic sensor. It defines the necessary pins for triggering and receiving the ultrasonic pulse, sets up the serial communication, and calculates the distance based on the time taken for the pulse to return. The measured distance is then printed to the serial monitor in centimeters.

Uploaded by

shihankv807
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

write a Arduino program for implement Distance measure using Ultrasonic

sensor .

Arduino Code
#define echoPin 2 // Attach pin D2 Arduino to pin Echo

#define trigPin 3 // Attach pin D3 Arduino to pin Trig

long duration; // Time taken for the pulse to reach receiver

int distance; // Calculated distance

void setup() {

pinMode(trigPin, OUTPUT); // Set trigPin as OUTPUT

pinMode(echoPin, INPUT); // Set echoPin as INPUT

Serial.begin(9600); // Start serial communication

Serial.println("Distance measurement using Arduino Uno.");

delay(500); // Short delay

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(2); // Wait for 2 microseconds

digitalWrite(trigPin, HIGH); // Turn on Trigger to generate pulse

delayMicroseconds(10); // Keep the trigger "ON" for 10 microseconds


digitalWrite(trigPin, LOW); // Turn off pulse trigger

duration = pulseIn(echoPin, HIGH); // Time taken by pulse to reach receiver

distance = duration * 0.0344 / 2; // Calculate distance using time

Serial.print("Distance: ");

Serial.print(distance); // Print distance in serial monitor

Serial.println(" cm");

delay(100); // Short delay

You might also like