Thanks to visit codestin.com
Credit goes to www.scribd.com

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

ChatGPT Code

This document contains an Arduino code that utilizes an ultrasonic sensor to measure distance. If an object is detected within 30 cm, it activates a buzzer and an LED; otherwise, both remain off. The code also prints the measured distance to the serial monitor for testing purposes.

Uploaded by

jivitesh941
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)
30 views2 pages

ChatGPT Code

This document contains an Arduino code that utilizes an ultrasonic sensor to measure distance. If an object is detected within 30 cm, it activates a buzzer and an LED; otherwise, both remain off. The code also prints the measured distance to the serial monitor for testing purposes.

Uploaded by

jivitesh941
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

const int trigPin = 9; // Pin connected to the trigger pin of the ultrasonic sensor

const int echoPin = 10; // Pin connected to the echo pin of the ultrasonic sensor
const int buzzerPin = 7; // Pin connected to the positive terminal of the buzzer
const int ledPin = 13; // Pin connected to the LED

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);

Serial.begin(9600);
}

void loop() {
// Triggering the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reading the distance from the ultrasonic sensor


long duration = pulseIn(echoPin, HIGH);
int distance = duration * 0.034 / 2;

// Checking if an object is within a certain range (adjust as needed)


if (distance < 30) {
// Object detected, activate buzzer and LED
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
} else {
// No object detected, turn off buzzer and LED
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}

// Print distance to serial monitor for testing (optional)


Serial.print("Distance: ");
Serial.println(distance);

// Add a delay to avoid rapid triggering


delay(500);
}

You might also like