πLight Control using Ultrasonic Sensor and Relay
This project utilizes an Arduino Uno development board along with an ultrasonic sensor and a relay module to create an automated light control system. The system turns on the light connected to the relay if the ultrasonic sensor detects an object within 50 cm. This README provides a comprehensive guide to set up and use the system effectively.
Components Required
Arduino Uno
Ultrasonic sensor (HC-SR04)
Relay module
Jumper wires
Light bulb or LED with appropriate power source
Circuit Diagram
Ensure to connect the components as shown in the circuit diagram:
Connect the VCC and GND of the ultrasonic sensor to the 5V and GND pins of the Arduino Uno.
Connect the TRIG pin of the ultrasonic sensor to pin 9 of the Arduino Uno.
Connect the ECHO pin of the ultrasonic sensor to pin 8 of the Arduino Uno.
Connect the IN pin of the relay module to pin 7 of the Arduino Uno.
Connect the relay module's VCC and GND to the 5V and GND of the Arduino Uno.
Connect the light bulb or LED to the relay module.
Code
Upload the following code to the Arduino Uno using the Arduino IDE:
const int trigPin = 9;
const int echoPin = 8;
const int relay=7;
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(relay,OUTPUT);
}
void loop() {
long duration;
int distance;
// Trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo signal
duration = pulseIn(echoPin, HIGH);
// Convert the duration to distance (in cm)
distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
digitalWrite(relay,HIGH);
if(distance<=50)
{
digitalWrite(relay,LOW);
delay(5000);
}
else
{
digitalWrite(relay,HIGH);
}
delay(100); // delay before the next measurement
}
Setting Up
Install the Arduino IDE from the official Arduino website.
Connect the Arduino Nano to your computer via USB.
Open the Arduino IDE and paste the code above into a new sketch.
Select the appropriate board and port from the Tools menu.
Click the upload button to upload the code to the Arduino Nano.
Usage
Once the code is uploaded, the system will continuously monitor the distance detected by the ultrasonic sensor. If the sensor detects an object within 50 cm, the relay will activate, turning on the light. If the object moves beyond 50 cm, the relay will deactivate, turning off the light.
Troubleshooting
Ensure all connections are secure and correct.
Check the power supply for the Arduino Nano and the relay module.
Use the Serial Monitor in the Arduino IDE to debug and monitor the distance readings from the ultrasonic sensor.
If the light turns on when the ultrasonic sensor detects a distance greater than 50 cm, switch the
LOW
written for the relay toHIGH
and vice versa (i.e., changedigitalWrite(RELAY_PIN, LOW)
todigitalWrite(RELAY_PIN, HIGH)
anddigitalWrite(RELAY_PIN, HIGH)
todigitalWrite(RELAY_PIN, LOW)
).
Last updated