Switching relay with Ultrasonic sensor
Switching relay with Ultrasonic sensor
Components and supplies
1
Arduino UNO
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
Relay (generic)
Project description
Code
Ultrasonic with relay
c_cpp
Copy and Paste the code.
1const int trigPin = 2; 2const int echoPin = 3; 3const int relay = 10; 4long duration; 5int distance; 6int safetyDistance; 7 8void setup() { 9 // put your setup code here, to run once: 10pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 11pinMode(echoPin, INPUT); 12pinMode(relay, OUTPUT);// Sets the echoPin as an Input 13Serial.begin(9600); // Starts the serial communication 14} 15 16void loop() { 17 // put your main code here, to run repeatedly: 18 19 20// Sets the trigPin on HIGH state for 10 micro seconds 21digitalWrite(trigPin, LOW); 22delayMicroseconds(2); 23 24digitalWrite(trigPin, HIGH); 25delayMicroseconds(10); 26 27digitalWrite(trigPin,LOW); 28 29// Reads the echoPin, returns the sound wave travel time in microseconds 30duration = pulseIn(echoPin,HIGH); 31// Calculating the distance 32distance= duration*0.034/2; 33Serial.println(distance); 34delay(50); 35safetyDistance = distance; 36if((distance>=25)) 37 { 38 digitalWrite(relay, LOW); 39} 40 else if(distance<25) 41 { 42 digitalWrite(relay, HIGH); 43 } 44} 45
Ultrasonic with relay
c_cpp
Copy and Paste the code.
1const int trigPin = 2; 2const int echoPin = 3; 3const int relay = 4 10; 5long duration; 6int distance; 7int safetyDistance; 8 9void setup() 10 { 11 // put your setup code here, to run once: 12pinMode(trigPin, OUTPUT); // 13 Sets the trigPin as an Output 14pinMode(echoPin, INPUT); 15pinMode(relay, OUTPUT);// 16 Sets the echoPin as an Input 17Serial.begin(9600); // Starts the serial communication 18} 19 20void 21 loop() { 22 // put your main code here, to run repeatedly: 23 24 25// Sets 26 the trigPin on HIGH state for 10 micro seconds 27digitalWrite(trigPin, LOW); 28delayMicroseconds(2); 29 30digitalWrite(trigPin, 31 HIGH); 32delayMicroseconds(10); 33 34digitalWrite(trigPin,LOW); 35 36// Reads 37 the echoPin, returns the sound wave travel time in microseconds 38duration = pulseIn(echoPin,HIGH); 39// 40 Calculating the distance 41distance= duration*0.034/2; 42Serial.println(distance); 43delay(50); 44safetyDistance 45 = distance; 46if((distance>=25)) 47 { 48 digitalWrite(relay, LOW); 49} 50 51 else if(distance<25) 52 { 53 digitalWrite(relay, HIGH); 54 } 55} 56
Ultrasonic with relay
c_cpp
Copy and Paste the code.
1const int trigPin = 2; 2const int echoPin = 3; 3const int relay = 10; 4long duration; 5int distance; 6int safetyDistance; 7 8void setup() { 9 // put your setup code here, to run once: 10pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 11pinMode(echoPin, INPUT); 12pinMode(relay, OUTPUT);// Sets the echoPin as an Input 13Serial.begin(9600); // Starts the serial communication 14} 15 16void loop() { 17 // put your main code here, to run repeatedly: 18 19 20// Sets the trigPin on HIGH state for 10 micro seconds 21digitalWrite(trigPin, LOW); 22delayMicroseconds(2); 23 24digitalWrite(trigPin, HIGH); 25delayMicroseconds(10); 26 27digitalWrite(trigPin,LOW); 28 29// Reads the echoPin, returns the sound wave travel time in microseconds 30duration = pulseIn(echoPin,HIGH); 31// Calculating the distance 32distance= duration*0.034/2; 33Serial.println(distance); 34delay(50); 35safetyDistance = distance; 36if((distance>=25)) 37 { 38 digitalWrite(relay, LOW); 39} 40 else if(distance<25) 41 { 42 digitalWrite(relay, HIGH); 43 } 44} 45
Comments
Only logged in users can leave comments