/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial:
Tutorial: Arduino Distance Detector With a Buzzer and LED's!
The purpose of this sketch is to detect the distance. As your hand gets closer to the ultrasonic sensor,
the LEDs should progressively light up, and the buzzer should produce a higher tone with each approach.
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connections from the ultrasonic sensor:
• ultrasonic sensor VCC pin → Breadboard 5V line
• ultrasonic sensor GND pin → Breadboard ground line
• ultrasonic sensor Trig pin → Arduino pin 13
• ultrasonic sensor Echo pin → Arduino pin 12
Connections from the Arduino to the passive buzzer:
• Arduino GND pin → buzzer GND pin (- pin)
• Arduino pin 11 → buzzer VCC pin (+ pin)
Connections from the LED :
• LED_1 anode pin → Arduino pin 2
• LED_1 cathode pin → 330 ohm resistor first pin → Breadboard ground line
• LED_2 anode pin → Arduino pin 3
• LED_2 cathode pin → 330 ohm resistor first pin → Breadboard ground line
• LED_3 anode pin → Arduino pin 4
• LED_3 cathode pin → 330 ohm resistor first pin → Breadboard ground line
• LED_4 anode pin → Arduino pin 5
• LED_4 cathode pin → 330 ohm resistor first pin → Breadboard ground line
• LED_5 anode pin → Arduino pin 6
• LED_5 cathode pin → 330 ohm resistor first pin → Breadboard ground line
• LED_6 anode pin → Arduino pin 7
• LED_6 cathode pin → 330 ohm resistor first pin → Breadboard ground line
*/
#define trigPin 13 // Pin number for the trigger pin of the ultrasonic sensor
#define echoPin 12 // Pin number for the echo pin of the ultrasonic sensor
#define led1 2 // Pin number for LED 1
#define led2 3 // Pin number for LED 2
#define led3 4 // Pin number for LED 3
#define led4 5 // Pin number for LED 4
#define led5 6 // Pin number for LED 5
#define led6 7 // Pin number for LED 6
#define buzzer 11 // Pin number for the buzzer
int sound = 250; // Variable to store the frequency of the sound
void setup() {
Serial.begin(9600); // Initialize the serial communication
pinMode(trigPin, OUTPUT); // Set the trigger pin as output
pinMode(echoPin, INPUT); // Set the echo pin as input
pinMode(led1, OUTPUT); // Set LED 1 pin as output
pinMode(led2, OUTPUT); // Set LED 2 pin as output
pinMode(led3, OUTPUT); // Set LED 3 pin as output
pinMode(led4, OUTPUT); // Set LED 4 pin as output
pinMode(led5, OUTPUT); // Set LED 5 pin as output
pinMode(led6, OUTPUT); // Set LED 6 pin as output
pinMode(buzzer, OUTPUT); // Set the buzzer pin as output
}
void loop() {
long duration, distance; // Variables to store the duration and distance
digitalWrite(trigPin, LOW); // Set the trigger pin to low
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(trigPin, HIGH); // Set the trigger pin to high
delayMicroseconds(10); // Wait for 10 microseconds
digitalWrite(trigPin, LOW); // Set the trigger pin to low again
duration = pulseIn(echoPin, HIGH); // Measure the duration of the echo pulse
distance = (duration / 2) / 29.1; // Calculate the distance based on the duration
// Check the distance and control the LEDs and sound accordingly
if (distance <= 30) {
digitalWrite(led1, HIGH); // Turn on LED 1
sound = 500; // Set sound frequency to 500
} else {
digitalWrite(led1, LOW); // Turn off LED 1
}
if (distance < 25) {
digitalWrite(led2, HIGH); // Turn on LED 2
sound = 1000; // Set sound frequency to 1000
} else {
digitalWrite(led2, LOW); // Turn off LED 2
}
if (distance < 20) {
digitalWrite(led3, HIGH); // Turn on LED 3
sound = 1500; // Set sound frequency to 1500
} else {
digitalWrite(led3, LOW); // Turn off LED 3
}
if (distance < 15) {
digitalWrite(led4, HIGH); // Turn on LED 4
sound = 2000; // Set sound frequency to 2000
} else {
digitalWrite(led4, LOW); // Turn off LED 4
}
if (distance < 10) {
digitalWrite(led5, HIGH); // Turn on LED 5
sound = 2500; // Set sound frequency to 2500
} else {
digitalWrite(led5, LOW); // Turn off LED 5
}
if (distance < 5) {
digitalWrite(led6, HIGH); // Turn on LED 6
sound = 3000; // Set sound frequency to 3000
} else {
digitalWrite(led6, LOW); // Turn off LED 6
}
// Check if the distance is out of range
if (distance > 30 || distance <= 0) {
Serial.println("Out of range"); // Print "Out of range" to the serial monitor
noTone(buzzer); // Turn off the buzzer
} else {
Serial.print(distance);
Serial.println(" cm"); // Print the distance in centimeters to the serial monitor
tone(buzzer, sound); // Generate sound with the specified frequency
}
delay(500); // Delay for 500 milliseconds before the next iteration
}