You are on page 1of 8

CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

Lab 8
TRIGGERING A RELAY WITH AN HC-SR04 ULTRASONIC SENSOR

INTRODUCTION
Relays are normally used for switching and controlling electrical equipment through control
signals. The relay is fed with low DC voltage and DC current. The relay provides isolation between the
control circuit and electric equipment by magnetic coupling inside it. It is a switch having NO (normally
open) and NC (normally close) contacts.

HC-SR04 ultrasonic sensor is a non-contact distance measurement module which is compatible


with the arduino or other system. It’s designed for easy modular project usage with industrial
performance.

In this activity, we used the ultrasonic sensor for triggering the relay to switch on the bulb which
controlled by the Arduino UNO.

OBJECTIVES
• To execute relay by using ultrasonic sensor, Arduino IDE and the corresponding board
• To switch the bulb and turn on the buzzer by using relay.
• To know the importance of relay when using high voltages.
• To familiarize the parts of the relay.
• To know how ultrasonic sensor works.

MATERIALS
• Arduino UNO
• HC-SR04 Ultrasonic Sensor
• Power Relay (2-channel)
• Regular extension cord
• Study lamp (5W light bulb)
• Buzzer
• Breadboard
• Jumper wires (3 male/female for the relay and 7 male/male for the Arduino and the HC-SR04)
• USB cable to connect the Arduino to the computer
• screwdriver

PROCEDURES
1. Cut the Extension Cord
Separate the extension cord in half, about 10cm/4inches
and cut one of the sides to expose the copper.
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

2. Connect the Arduino to the Breadboard


Connect red jumper wire to the 5V PIN on the Arduino then to the red + rail on the breadboard
and used a Black Jumper wire to connect the GND (ground) PIN on the Arduino to the blue - rail
on the breadboard.

3. Connect the HC-SR04 to the Breadboard and the Arduino.


Place the HC-SR04 Ultrasonic sensor on the edge of the breadboard with the sensors pointing
away from the board.

The HC-SR04 Ultrasonic sensor has 4 pins.


▪ The first pin, the VCC, connected with a red jumper wire to the red + rail on the bread
board.
▪ The second pin, the Trig or trigger, connected to PIN 12 on the Arduino. The trigger pin is
used by the ultrasonic sensor to send out a sound wave.
▪ The third pin, the Echo, connected to PIN 11 on the Arduino. The echo pin is used to pick
up returning sound waves sent by the trigger.
▪ The last pin, the GND or ground, connected with a black jumper wire to the blue - rail on
the breadboard.
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

4. Connect the buzzer to the negative trail of the breadboard and to pin6 of the Arduino.

5. Connect the power relay to the arduino, breadboard and study lamp. (Note: We used an
extension cord for not to damage the study lamp.)

6. Open up the Arduino IDE.


7. Program the Arduino UNO. Encode or copy then edit the code for this section.
8. Plug the Arduino UNO board into the laptop with a USB cable.
9. Upload onto board. Make sure the serial port is set. In this case, COM4 and the correct board is
selected, that is Arduino UNO is selected from the dropdown menu at the top left corner of the
Arduino application. Then upload the code unto the board by clicking upload at the top left
corner or simply, ctrl + U.
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

10. Plug the extension cord in AC wall outlet, the light will turn on if you place an object less than
200 cm in front of the HC-SR04
11. Monitor the Arduino board and the circuit.
12. Put on any object (e.g. hand) at the front of the ultrasonic sensor. Then, move away.
13. Observe the whole circuit.

RESULTS AND DISCUSSIONS


• Here is the code of HC-SR04 Ultrasonic Sensor with relay which controlled by an ARDUINO UNO:

#define TRIG_PIN 12 // Arduino pin tied to trigger pin on the


ultrasonic sensor.

#define ECHO_PIN 11 // Arduino pin tied to echo pin on the


ultrasonic sensor.

#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in


centimeters). Maximum sensor distance is rated at 400-500cm.

#define BUZZER_PIN 6

#define RELAY_LINE2_PIN 8

#include "NewPing.h"

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of


pins and maximum distance.

unsigned int distance = 0; // Current distance of any object facing


the ultrasonic sensor

int critical_distance_cms = 50; // Cutoff distance at which the


light will switch

bool state = 0;

void setup() {

Serial.begin(9600); // Open serial monitor at 115200 baud to see


ping results.

pinMode(RELAY_LINE2_PIN, OUTPUT);

pinMode (BUZZER_PIN, OUTPUT);

digitalWrite(RELAY_LINE2_PIN, HIGH); // Turn the light off

void loop() {

delay(20); // Wait 50ms between pings (about 20


pings/sec). 29ms should be the shortest delay between pings.

ReadDistance();
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

Serial.print("Ultrasonic: ");

Serial.print(distance); // Send ping, get distance in cm and print


result (0 = outside set distance range)

Serial.println("cm");

// Someone is near the door

if (distance < critical_distance_cms)

while (distance < critical_distance_cms)

// Check if they moved away

ReadDistance();

delay(1); // Do nothing until the person moves away from


the sensor

state = !state; // Change the state of the relay

if (state)

Serial.println("Relay Open!");

digitalWrite(RELAY_LINE2_PIN, LOW); // Turn the light on

digitalWrite(BUZZER_PIN, HIGH);

else

Serial.println("Relay Closed!");

digitalWrite(RELAY_LINE2_PIN, HIGH); // Turn the light


off

digitalWrite(BUZZER_PIN, LOW);

}
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

// Updates the value of the Ultrasonic reading

void ReadDistance()

// Read 7 values from the ultrasonic and get the median value (
median filter )

// Gets rid of noisy reading

distance = sonar.convert_cm(sonar.ping_median(7));

// The value 0 indicates that the ultrasonic sensor is reading


nothing in front of it

// Set this distance to max distance so the light doesn't switch


unnecessarily

if (distance == 0)

distance = MAX_DISTANCE;

REFERENCES

http://www.instructables.com/id/Triggering-a-relay-with-an-SR04-ultrasonic-sensor/
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

DOCUMENTATION
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero

You might also like