0% found this document useful (0 votes)
21 views3 pages

Why This Code Is Useful

The document outlines an Arduino code that utilizes an ultrasonic sensor to measure distance and activate a buzzer and LED based on proximity. It defines pin constants, initializes the pins in the setup function, and continuously measures distance in the loop function, activating the buzzer and LED if the distance is 5 units or less. The measured distance is also printed to the Serial Monitor for monitoring purposes.

Uploaded by

flowerblair3
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)
21 views3 pages

Why This Code Is Useful

The document outlines an Arduino code that utilizes an ultrasonic sensor to measure distance and activate a buzzer and LED based on proximity. It defines pin constants, initializes the pins in the setup function, and continuously measures distance in the loop function, activating the buzzer and LED if the distance is 5 units or less. The measured distance is also printed to the Serial Monitor for monitoring purposes.

Uploaded by

flowerblair3
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/ 3

Explanations About the code:

const int trigPin = 9;

const int echoPin = 10;

const int buzzer = 11;

const int ledPin = 13;

- These lines define four constants: `trigPin`, `echoPin`, `buzzer`, and `ledPin`. They represent the pin
numbers to which components are connected.

2. ```cpp

long duration;

int distance;

int safetyDistance;

These lines declare three variables: duration, distance, and safetyDistance. These variables will be used
to store values related to the operation of the ultrasonic sensor.

void setup() {

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

pinMode(buzzer, OUTPUT);

pinMode(ledPin, OUTPUT);

Serial.begin(9600); // Starts the serial communication

}
- The `setup()` function initializes the pins. It sets `trigPin` as an output pin, `echoPin` as an input pin,
and configures `buzzer` and `ledPin` as output pins. It also starts serial communication at a baud rate of
9600.

4. ```cpp

void loop() {

digitalWrite(trigPin, LOW); // Clears the trigPin

delayMicroseconds(2);

digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 10 microseconds

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in
microseconds

distance = duration * 0.034 / 2; // Calculating the distance

safetyDistance = distance;

if (safetyDistance <= 5) {

digitalWrite(buzzer, HIGH);

digitalWrite(ledPin, HIGH);

} else {

digitalWrite(buzzer, LOW);

digitalWrite(ledPin, LOW);

}
Serial.print("Distance: "); // Prints the distance on the Serial Monitor

Serial.println(distance);

The loop() function is where the main operation of the code occurs:

It sends a short pulse to the ultrasonic sensor to trigger a measurement.

It measures the duration of the echo pulse, which corresponds to the time taken for the ultrasonic signal
to travel to the object and back.

It calculates the distance based on the duration of the echo pulse.

It checks if the calculated distance is less than or equal to 5 units (presumably in centimeters or inches).
If so, it activates the buzzer and LED to indicate that an object is too close. Otherwise, it turns off the
buzzer and LED.

It prints the calculated distance to the Serial Monitor for debugging or monitoring purposes.

You might also like