Arduino UNO R4 - Ultrasonic Sensor - Relay

This tutorial instructs you how to use the Arduino Uno R4 to work with an ultrasonic sensor and a relay. You’ll learn to:

With this setup, you can connect the relay to cool stuff like lights, motors, or other gadgets and control them using the Arduino and sensor combo!

Overview of Relay and Ultrasonic Sensor

If you're unfamiliar with how a relay and ultrasonic sensor work, including their pinout and programming, you can learn more in the following tutorials:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Ultrasonic Sensor Relay

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-ultrasonic-sensor-relay */ #define TRIG_PIN 7 // The Arduino UNO R4 pin connected to the ultrasonic sensor's TRIG pin #define ECHO_PIN 6 // The Arduino UNO R4 pin connected to the ultrasonic sensor's ECHO pin #define RELAY_PIN A5 // The Arduino UNO R4 pin connected to Relay's pin #define DISTANCE_THRESHOLD 50 // centimeters float duration_us, distance_cm; void setup() { Serial.begin (9600); // initialize serial port pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; if(distance_cm < DISTANCE_THRESHOLD) digitalWrite(RELAY_PIN, HIGH); // turn on Relay else digitalWrite(RELAY_PIN, LOW); // turn off Relay // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); delay(500); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code provided and paste it in the Arduino IDE.
  • Press the Upload button in the Arduino IDE to transfer the code to your Arduino UNO R4.
Arduino IDE Upload Code
  • Put your hand in front of the sensor
  • Watch the relay's state change

Code Explanation

Check the explanation in the comments of the source code, line by line!

※ NOTE THAT:

The code provided is meant for educational use. The ultrasonic sensor reacts strongly to noise. To effectively use the ultrasonic sensor, it's important to reduce this noise. You can learn how to do this at how to filter noise for ultrasonic sensor.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!