Ultrasonic Sensor With Relay - Arduino Tutorial
Ultrasonic Sensor With Relay - Arduino Tutorial
Menu
Learn More
Electronic Design News (EDN) Is An Electronics
Community For Engineers, By Engineers EDN
1 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
Electronic Design
Magazine
EDN
2 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
[ hide ]
1 Introduction
2 Hardware Components
3 Ultrasonic Sensor Relay with Arduino
4 Applications
5 Conclusion.
Introduction
An HC-SR04 ultrasonic sensor distance threshold dependent 5V SPDT relay using an
Arduino UNO is a system that uses an ultrasonic sensor (HC-SR04) to measure the
distance of an object in front of it and a 5V Single Pole Double Throw (SPDT) relay to
control an external device based on that distance. The threshold distance is set in
centimeters and can be adjusted as desired.
The ultrasonic sensor sends out a sound wave and measures the time it takes for the
sound wave to bounce back to the sensor to calculate the distance of the object. The
Arduino UNO is used as a microcontroller to read the distance from the ultrasonic
3 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
EDN
Hardware Components
You will require the following hardware for Ultrasonic Sensor Relay.
4 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
1. Arduino UNO – 1
3. Ultrasonic Sensor – 1
4. Relay – 1
7. DC Power Jack – 1
9. Jumper Wires – 1
EDN
5 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
• VCC pin to 5V
• Trig pin to digital pin 2
• Echo pin to digital pin 3
• GND pin to GND
3. In the Arduino IDE, include the following library at the beginning of the code:
1 #include <Ultrasonic.h>
4. Define the pin numbers for the ultrasonic sensor and the relay in the setup
function:
2 int relay_pin = 4;
3 pinMode(relay_pin, OUTPUT);
5. In the loop function, use the distanceRead() function to get the distance in
centimeters from the ultrasonic sensor, and store it in a variable. Then, use an if
statement to check if the distance is less than a threshold value (in
centimeters). If the distance is less than the threshold, turn on the relay using
the digitalWrite() function with value HIGH, otherwise, turn it off using the
digitalWrite() function with value LOW.
1 void loop() {
6 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
4 digitalWrite(relay_pin, HIGH);
5 } else {
6 digitalWrite(relay_pin, LOW);
7 }
8 }
6. To post the status of the relay on the serial monitor, use the Serial.begin()
function in the setup function to start serial communication, then use the
Serial.print() function to print the status of the relay in the loop function.
1 void setup() {
2 Serial.begin(9600);
3 ...
4 }
5 void loop() {
6
7 ...
9 if (relay_status == HIGH) {
10 Serial.println("Relay is on");
11 } else {
12 Serial.println("Relay is off");
13 }
14 ...
15 }
Schematic
Make connections according to the circuit diagram given below.
7 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
Wiring / Connections
5V VCC VCC
D7 TRIG
D6 ECHO
A5 SIG
8 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
Code
Now copy the following code and upload it to Arduino IDE Software.
8 void setup() {
9
10 Serial.begin (9600); // initialize serial port
14 }
15 void loop() {
16
17 // generate 10-microsecond pulse to TRIG pin
18 digitalWrite(TRIG_PIN, HIGH);
19 delayMicroseconds(10);
20 digitalWrite(TRIG_PIN, LOW);
Working Explanation
In the setup function, the Arduino’s serial communication is initialized with
Serial.begin(9600) and the ultrasonic sensor and relay pins are defined. The
9 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
ultrasonic sensor is initialized by creating an object of the Ultrasonic class and passing
the trigger and echo pin numbers. The relay pin is defined as an output pin using the
pinMode() function.
In the loop function, the distance from the ultrasonic sensor is read using the
distanceRead() function from the Ultrasonic library and stored in a variable. Then, an if
statement is used to check if the distance is less than the threshold distance (in
centimeters). If the distance is less than the threshold, the relay is turned on by setting
the relay pin to HIGH with the digitalWrite() function, otherwise the relay is turned off
by setting the relay pin to LOW. The status of the relay is also posted on the serial
monitor by using the Serial.println() function.
Applications
• Obstacle Detection
• Automated Door Control
• Level Detection
• Parking Assistance
• Automatic Light Control
• Proximity Alarm
• Automatic Faucet Control
• Robot Navigation
Conclusion.
We hope you have found this Ultrasonic Sensor Relay Circuit very useful. If you feel any
difficulty in making it feel free to ask anything in the comment section.
Related posts:
10 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
11 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
Electronic
Design Magazine
EDN
Electronic
Design Magazine
EDN
12 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
13 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
14 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
15 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
16 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
17 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
18 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
19 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
20 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
darlington transisor EEPROM EPROM Fast Recovery Diode fm transmitter high voltage jfet ldo LDR
LED led flasher light lm317 lm358 lm741 N Channel MOSFET NE555
21 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
22 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
23 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
About
Our Team
Contact
Advertise
Electronics
Simple Electronics
Electronics Projects
Privacy Policy
Disclaimer
Copyright
24 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...
We do everything with our core values of honesty, hard work, and trust.
25 of 25 4/4/24, 18:54