0% found this document useful (0 votes)
19 views

Ultrasonic Sensor With Relay - Arduino Tutorial

Ultrasonic

Uploaded by

kawisojames10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Ultrasonic Sensor With Relay - Arduino Tutorial

Ultrasonic

Uploaded by

kawisojames10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...

About Our Terms and Privacy Contact


Disclaimer Copyright Career Advertise
Us Team Conditions Policy Us

 Menu

ARDUINO ESP32 ESP8266 ESP32-CAM ESP01 MICROPYTHON RASPBERRY PI

ATMEGA328P ATTINY85 IOT DATASHEETS

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

Ultrasonic Sensor with Relay – Arduino Tutorial


 12 months ago  by James Fuller  4,888 views

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...

sensor and control the SPDT relay based on the distance.

Subscribe For Weekly Newsle�ers


With Industry Trends and Updates

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

2. USB Cable Type A to B – 1

3. Ultrasonic Sensor – 1

4. Relay – 1

5. Warning Light Bright Waterproof – 1

6. Power Adapter 12V 1

7. DC Power Jack – 1

8. Power Adapter for Arduino 9V 1

9. Jumper Wires – 1

Learn From Industry Experts About New


Innovations & The Future Of The Electronics
Industry


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...

Ultrasonic Sensor Relay with Arduino


1. Connect the HC-SR04 ultrasonic sensor to the Arduino Uno as follows:

• VCC pin to 5V
• Trig pin to digital pin 2
• Echo pin to digital pin 3
• GND pin to GND

2. Connect the 5V SPDT Relay to the Arduino Uno as follows:

• IN1 pin to digital pin 4


• 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:

1 Ultrasonic ultrasonic(2, 3);

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() {

2 int distance = ultrasonic.distanceRead();


if (distance < threshold) { 
3

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 ...

8 int relay_status = digitalRead(relay_pin);

9 if (relay_status == HIGH) {

10 Serial.println("Relay is on");

11 } else {

12 Serial.println("Relay is off");

13 }

14 ...

15 }

7. Finally, upload the code to your Arduino Uno.

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

GND GND GND

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...

Installing Arduino IDE


First, you need to install Arduino IDE Software from its official website Arduino. Here is
a simple step-by-step guide on “How to install Arduino IDE“.

Code
Now copy the following code and upload it to Arduino IDE Software.

1 const int TRIG_PIN = 7; // Arduino pin connected to Ultrasonic


Sensor's TRIG pin

2 const int ECHO_PIN = 6; // Arduino pin connected to Ultrasonic


Sensor's ECHO pin

3 const int RELAY_PIN = A5; // Arduino pin connected to Relay's pin

4 const int DISTANCE_THRESHOLD = 50; // centimeters

5 // variables will change:


6
7 float duration_us, distance_cm;

8 void setup() {
9
10 Serial.begin (9600); // initialize serial port

11 pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode

12 pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode

13 pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode

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);

21 // measure duration of pulse from ECHO pin


22
23 duration_us = pulseIn(ECHO_PIN, HIGH);

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

555 Timer Circuits [493]

Alarm Circuits [220]

Audio Amplifier Circuits [211]

Battery Charger Circuits [117] 

12 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...

Battery Monitor Circuits [15]

Electronics Projects [150]

Electronics Tutorial [31]

FM Transmitter Circuits [38]

Home Automation Projects [6]

Inverter Circuits [29]

LED & Light Circuits [286]

Mobile Charger Circuits [12]

Motor Speed Control Circuits [41]

Power Banks Circuits [15]

Power Supply Circuits [110]

Printed Circuit Board – PCB [179]

Radio and RF Circuits [68]

Safety & Security Circuits [55]

Sensors and Modules [149]

Simple Electronic Circuits [536]

Test and Measurement Circuits [147]

Top Electronics Projects [96]

Water Level Indicator Circuits [24]

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...

Explaining the Three Types of Cricket

Distance Meter using HCSR04 Ultrasonic Sensor & ESP32

Voice Recording with ISD1820 Module and ESP01


18 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...

Laser Tripwire Home Alarm System

How to make your own ESP32 Breakout Board – Bare Minimum

19 of 25 4/4/24, 18:54
Ultrasonic Sensor with Relay - Arduino Tutorial https://www.circuits-diy.com/ultrasonic-sensor-with-relay-a...

2n3904 555 timer Adjustable Regulator arduino arduino uno



audio amplifier battery charger bc547 bridge rectifier cd4017 cmos counter

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

npn transistor operational amplifier pcb pnp transistor


power amplifier power MOSFET power supply power transistor
printed circuit board relay switch temperature sensor triac Ultrafast Diode

voltage regulator zener diode

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

Terms and Conditions

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.

      

© 2022 Circuits-DIY licensed under UK (13618262) - All Rights Reserved

25 of 25 4/4/24, 18:54

You might also like