Wiring / Connections: 1.problem Statement
Wiring / Connections: 1.problem Statement
Wiring / Connections: 1.problem Statement
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 (here
LEDs) based on that distance. The threshold distance is set in cm 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 sensor and control the SPDT relay
based on the distance.
WIRING / CONNECTIONS
5V VCC VCC
D7 ECHO -
D6 TRIG -
A2 – A5 - IN1- IN4
1.PROBLEM STATEMENT: Write a program to turn ON (and remain ON for 10 secs) and
then turn OFF specified Relay LEDs as per the following conditions:
0< Distance from sensor <= 10 cm = LED1 is actuated
10< Distance from sensor <=20 cm = LED2 is actuated
20< Distance from sensor <=30 cm = LED3 is actuated
30< Distance from sensor <=40 cm = LED4 is actuated
CODE
const int relayPins[ ]={2,3,4,5};
const int trigPin=6;
const int echoPin=7;
int distance;
long duration;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for (int i=0; i < 4; i++)
{ pinMode (relayPins[i],
OUTPUT);
digitalWrite(relayPins[i], HIGH);
}
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigPin,LOW);
delayMicroseconds (2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin,HIGH);
distance=duration*0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println("cm");
if(distance>0 &&
distance<=10){digitalWrite(rela
yPins[0], LOW); delay(1000);
digitalWrite(relayPins[0], HIGH);
}
else if (distance>10 && distance<=20)
{digitalWrite(relayPins [1], LOW);
delay(1000);
digitalWrite(relayPins[1], HIGH);
}
else if (distance>20 && distance<=30)
{digitalWrite(relayPins [2], LOW);
delay(1000);
digitalWrite(relayPins [2], HIGH);
}
else if (distance>30 && distance <=40)
{digitalWrite(relayPins [3], LOW);
delay(1000);
digitalWrite(relayPins [3], HIGH);
}
delay(1000);
}
FOLLOW -UP QUESTION:
Update the code as follows:
0< Distance from sensor <= 10 cm = LED3 is actuated
10< Distance from sensor <=20 cm = LED1 is actuated
20< Distance from sensor <=30 cm = LED4 is actuated
30< Distance from sensor <=40 cm = LED2 is actuated
APPLICATIONS
Obstacle Detection
Automated Door Control
Level Detection
Parking Assistance
Automatic Light Control
Proximity Alarm
Automatic Faucet Control
Robot Navigation
CONCLUSION.
By performing this experiment, we learnt about the interfacing of
an analog sensor (Ultrasonic Sensor) with the Arduino and further
to digitally control any externally attached equipment via relay
switches using the same sensor.