Ultrasonic Distance-Based LED Switch
Concept: This project uses an HC-SR04 ultrasonic sensor and an Arduino to activate an LED when an object is within 10 cm. This distance-based switch can serve as a touchless light control.
Materials
– Arduino Uno
– HC-SR04 Ultrasonic Sensor
– LED
– 220Ω Resistor
– Breadboard and Jumper Wires
Process:
1. Setup: The ultrasonic sensor’s Trig and Echo pins connect to Digital Pins 12 and 13 on the Arduino, while the LED is connected to Digital Pin 2 with a resistor.
2. Code: The Arduino reads the distance from the sensor. If within 10 cm, it lights the LED; otherwise, it turns off.
3. Testing: The LED successfully turns on when an object is close, providing feedback on proximity.
CODE:
const int echo = 13; // Echo pin of the ultrasonic sensor
const int trig = 12; // Trigger pin of the ultrasonic sensor
int duration = 0; // Variable to store pulse duration
int distance = 0; // Variable to store calculated distance
pinMode(trig, OUTPUT); // Set trig pin as output
pinMode(echo, INPUT); // Set echo pin as input
pinMode(LED, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial monitor for debugging
// Send out a 10 microsecond pulse on the trig pin
digitalWrite(trig, HIGH);
// Read the echo pin and calculate distance
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 29.1; // Convert duration to distance in cm
// Turn on the LED if the distance is less than 10 cm
if (distance < 10 && distance > 0) { // Check distance within range
digitalWrite(LED, HIGH); // Turn on LED
digitalWrite(LED, LOW); // Turn off LED
// Print distance to the serial monitor for debugging
Serial.print("Distance: ");
delay(500); // Delay for stability
const int echo = 13; // Echo pin of the ultrasonic sensor
const int trig = 12; // Trigger pin of the ultrasonic sensor
int LED = 2; // LED pin
int duration = 0; // Variable to store pulse duration
int distance = 0; // Variable to store calculated distance
void setup() {
pinMode(trig, OUTPUT); // Set trig pin as output
pinMode(echo, INPUT); // Set echo pin as input
pinMode(LED, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial monitor for debugging
}
void loop() {
// Send out a 10 microsecond pulse on the trig pin
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Read the echo pin and calculate distance
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 29.1; // Convert duration to distance in cm
// Turn on the LED if the distance is less than 10 cm
if (distance < 10 && distance > 0) { // Check distance within range
digitalWrite(LED, HIGH); // Turn on LED
} else {
digitalWrite(LED, LOW); // Turn off LED
}
// Print distance to the serial monitor for debugging
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Delay for stability
}
const int echo = 13; // Echo pin of the ultrasonic sensor
const int trig = 12; // Trigger pin of the ultrasonic sensor
int LED = 2; // LED pin
int duration = 0; // Variable to store pulse duration
int distance = 0; // Variable to store calculated distance
void setup() {
pinMode(trig, OUTPUT); // Set trig pin as output
pinMode(echo, INPUT); // Set echo pin as input
pinMode(LED, OUTPUT); // Set LED pin as output
Serial.begin(9600); // Initialize serial monitor for debugging
}
void loop() {
// Send out a 10 microsecond pulse on the trig pin
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
// Read the echo pin and calculate distance
duration = pulseIn(echo, HIGH);
distance = (duration / 2) / 29.1; // Convert duration to distance in cm
// Turn on the LED if the distance is less than 10 cm
if (distance < 10 && distance > 0) { // Check distance within range
digitalWrite(LED, HIGH); // Turn on LED
} else {
digitalWrite(LED, LOW); // Turn off LED
}
// Print distance to the serial monitor for debugging
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Delay for stability
}
VIDEO DEMONSTRATION
Reflection: This project demonstrates basic sensor integration and is adaptable for touchless applications in home automation.