madhav133
Published © GPL3+

Ultrasonic Vehicle Gateway

An automatic ultrasonic vehicle gateway.

BeginnerShowcase (no instructions)260
Ultrasonic Vehicle Gateway

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×2
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Resistor 100 ohm
Resistor 100 ohm
×1
LED (generic)
LED (generic)
×1

Story

Read more

Schematics

Circuit Diagram

Made with AUTODESK Tinkercad

Schematic Diagram

Made with AUTODESK Tinkercad

Code

UltrasonicVehicleGateway.ino

C/C++
# include <Servo.h>

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 2;

// defines variables
long duration;
int distance;

//servo motor
Servo servo;

void setup() {
  servo.attach(12); // Attaches the servo motor
  servo.write(0);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(ledPin, OUTPUT);
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  digitalWrite(ledPin, LOW);
}

void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = duration * 0.034 / 2;

  if (distance < 25) {
    servo.write(90);
    digitalWrite(ledPin, HIGH);
  } else {
    servo.write(0);
    digitalWrite(ledPin, LOW);
  }
}

Credits

madhav133
0 projects • 1 follower

Comments