This circuit is designed to control a 12V Power LED using an Arduino Nano and an HC-SR04 Ultrasonic Sensor. The LED is powered by a 12V battery and is controlled via a relay module. The ultrasonic sensor measures the distance to an object, and if the object is within a specified safety distance, the relay is triggered to turn off the LED.
Arduino Nano
HC-SR04 Ultrasonic Sensor
Relay Module 1 Channel
Battery 12V
Power LED 12V 10W 0.8-0.9A
GND is connected to:
D8 is connected to:
D9 is connected to:
D10 is connected to:
5V is connected to:
GND is connected to:
TRIG is connected to:
ECHO is connected to:
VCC is connected to:
GND is connected to:
S is connected to:
5V is connected to:
COM is connected to:
NO is connected to:
+ is connected to:
- is connected to:
+ is connected to:
- is connected to:
const int trigPin = 9;
const int echoPin = 10;
const int relayPin = 8;
const int safetyDistance = 100; // Distance in cm (1 meter)
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Keep TV on initially
Serial.begin(9600);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration * 0.034) / 2;
Serial.print("Distance: ");
Serial.println(distance);
if (distance <= safetyDistance) {
digitalWrite(relayPin, HIGH); // Turn off TV
} else {
digitalWrite(relayPin, LOW); // Keep TV on
}
delay(500); // Delay for stability
}
This code is designed to control the relay based on the distance measured by the HC-SR04 Ultrasonic Sensor. If the measured distance is less than or equal to the safety distance (100 cm), the relay is triggered to turn off the LED. Otherwise, the LED remains on.