0% found this document useful (0 votes)
17 views2 pages

LAB10

This code interfaces an ultrasonic sensor with an Arduino to measure distance. It defines the trigger and echo pins, sets the trigger pin high for 10 microseconds to send a pulse, then measures the time it takes for the echo to return on the echo pin. It calculates the distance in centimeters by dividing the travel time by 2 and multiplying by the speed of sound. The distance is then printed to the serial monitor.

Uploaded by

kanz ul emaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

LAB10

This code interfaces an ultrasonic sensor with an Arduino to measure distance. It defines the trigger and echo pins, sets the trigger pin high for 10 microseconds to send a pulse, then measures the time it takes for the echo to return on the echo pin. It calculates the distance in centimeters by dividing the travel time by 2 and multiplying by the speed of sound. The distance is then printed to the serial monitor.

Uploaded by

kanz ul emaan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

LAB 11

CIRCUIT:

CODE:

/*
* Ultrasonic Sensor HC-SR04 interfacing with Arduino.
*/
// defining the pins
const int trigPin = 9;
const int echoPin = 10;
// defining variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
LAB 11

// Sets the trigPin on HIGH state for 10 micro seconds


digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

You might also like