Dithira Hettiarachchi
Created November 4, 2020

Distance switch

this system is basically switches on a relay when an object comes near the ultrasonic sensor.....

Distance switch

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
5v relay module
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED Light Bulb, Frosted GLS
LED Light Bulb, Frosted GLS
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Multitool, Screwdriver
Multitool, Screwdriver

Story

Read more

Schematics

the circuit diagram

instead of the bulb you can fix anything.

Code

the code

Arduino
#define trig 2
#define echo 4
#define relay 8

void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay, OUTPUT);
  
 Serial.begin (9600);
}

void loop() {
 digitalWrite(trig, LOW);
 delayMicroseconds(2);
 digitalWrite(trig, HIGH); 
 delayMicroseconds(10);
 digitalWrite(trig, LOW);

long t = pulseIn (echo,HIGH);

long inches = t/74/2;
long cm = t/29/2;

Serial.print(inches);
Serial.print("in\t");
Serial.print(cm);
Serial.println("cm");


if (cm>30){
  digitalWrite(relay,HIGH);
 
  
}
else {
digitalWrite(relay, LOW);
}

}

Credits

Dithira Hettiarachchi
7 projects • 6 followers
Python Cooking Web3

Comments