Light Control using Ultrasonic Sensor and Relay
Last updated
Last updated
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.
Arduino Uno
Ultrasonic sensor (HC-SR04)
Relay module
Jumper wires
Light bulb or LED with appropriate power source
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.
Upload the following code to the Arduino Uno using the Arduino IDE:
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.
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.
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 to HIGH
and vice versa (i.e., change digitalWrite(RELAY_PIN, LOW)
to digitalWrite(RELAY_PIN, HIGH)
and digitalWrite(RELAY_PIN, HIGH)
to digitalWrite(RELAY_PIN, LOW)
).