Triggering A Relay With An Hc-Sr04 Ultrasonic Sensor: CS 11/group - 4 - Borromeo, Galanida, Pabilan, Paypa, Tejero
Triggering A Relay With An Hc-Sr04 Ultrasonic Sensor: CS 11/group - 4 - Borromeo, Galanida, Pabilan, Paypa, Tejero
Triggering A Relay With An Hc-Sr04 Ultrasonic Sensor: 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.
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
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.)
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.
#define BUZZER_PIN 6
#define RELAY_LINE2_PIN 8
#include "NewPing.h"
bool state = 0;
void setup() {
pinMode(RELAY_LINE2_PIN, OUTPUT);
void loop() {
ReadDistance();
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero
Serial.print("Ultrasonic: ");
Serial.println("cm");
ReadDistance();
if (state)
Serial.println("Relay Open!");
digitalWrite(BUZZER_PIN, HIGH);
else
Serial.println("Relay Closed!");
digitalWrite(BUZZER_PIN, LOW);
}
CS 11/Group_4_Borromeo,Galanida,Pabilan,Paypa,Tejero
void ReadDistance()
// Read 7 values from the ultrasonic and get the median value (
median filter )
distance = sonar.convert_cm(sonar.ping_median(7));
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