World of Electronics

Full Tutorial: How To Make Your Own Social Distancing Device!

For this tutorial, you’ll need:

  1. Ultrasonic Sensor HC-SR04
  2. Led 5MM Blue & Yellow
  3. Arduino UNO
  4. 2 x Resistor 330E
  5. BreadBoard
  6. Jumper Wires
  7. Battery 9V
  8. Snap to DC cable

Watch this full video:

This video is sponsored by C.B.Electronics & Produced by Lebanese Park

Code:

long d; //distance variable
long DistanceSensor (int, int); //trigger echo
define T 8 // pin 8 is trigger for sensor
define E 7 //// pin 7 is echo for sensor
define ok 11 // ok digital pin
define no 12 // no digital pin
define sd 200 // safe distance = 200 cm
void setup() {
Serial.begin(9600);
pinMode(E, INPUT); //echo sensor
pinMode(T, OUTPUT); //trigger sensor
pinMode(ok, OUTPUT);
pinMode(no, OUTPUT);
digitalWrite(ok, LOW);
digitalWrite(no, LOW);
}
void loop() {
d=DistanceSensor(T,E);
Serial.println(d);
delay(1000);
if(d<sd)
{
digitalWrite(ok, LOW);
digitalWrite(no, HIGH);
}
else
{
digitalWrite(no, LOW);
digitalWrite(ok, HIGH);
}
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}
long DistanceSensor (int trigger, int echo)
{
long duration, inches, cm;
digitalWrite(trigger, HIGH); delayMicroseconds(5); digitalWrite(trigger, LOW); delayMicroseconds(5);
duration = pulseIn(echo, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
return cm;
}