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

Arduino Ultrasonic Sensor & Buzzer Code

The document contains Arduino code for two separate functionalities. The first part measures distance using an ultrasonic sensor and controls an LED based on the distance, while the second part generates a series of tones on a buzzer with increasing frequency. Both functionalities are implemented in their respective loop functions with specific pin configurations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Arduino Ultrasonic Sensor & Buzzer Code

The document contains Arduino code for two separate functionalities. The first part measures distance using an ultrasonic sensor and controls an LED based on the distance, while the second part generates a series of tones on a buzzer with increasing frequency. Both functionalities are implemented in their respective loop functions with specific pin configurations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#define trigPin 12

#define echoPin 11
#define led 13

void setup() {
[Link](9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}

void loop(){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;

if (distance < 100){


digitalWrite(led, HIGH);
}else {
digitalWrite(led, LOW);
}

[Link](distance);
[Link](" cm");
delay(100);
}
int buzzer = 9;
int i = 0;
int frequency = 1000;
int duration = 500;

void setup() {
pinMode(buzzer, OUTPUT);
}

void loop() {
for (i = 0; i < 5; i++) {
tone(buzzer, frequency);
delay(duration);
noTone(buzzer);
delay(500);
frequency += 100;
if (frequency > 2000) {
frequency = 1000;
}
}
}

You might also like