0% found this document useful (0 votes)
18 views1 page

Arduino Ultrasonic Gate Control Code

This document contains an Arduino sketch for controlling a servo gate using an ultrasonic sensor. The code initializes the sensor and servo, measures distance, and opens the gate if a car is detected within 20 cm. The gate remains open for 3 seconds before closing again, with delays to stabilize the sensor and manage operations.

Uploaded by

abhishek163280
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views1 page

Arduino Ultrasonic Gate Control Code

This document contains an Arduino sketch for controlling a servo gate using an ultrasonic sensor. The code initializes the sensor and servo, measures distance, and opens the gate if a car is detected within 20 cm. The gate remains open for 3 seconds before closing again, with delays to stabilize the sensor and manage operations.

Uploaded by

abhishek163280
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <Servo.

h>

Servo gateServo;

const int trigPin = 7;


const int echoPin = 6;
const int servoPin = 9;

long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
[Link](servoPin);
[Link](0); // Gate initially closed
[Link](9600);
}

void loop() {
// Send ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read pulse duration


duration = pulseIn(echoPin, HIGH);

// Calculate distance in cm
distance = duration * 0.034 / 2;

[Link]("Distance: ");
[Link](distance);
[Link](" cm");

if (distance < 20) { // Car detected


[Link](90); // Open gate
delay(3000); // Keep open for 3 seconds
[Link](0); // Close gate
delay(1000); // Small delay before next detection
}

delay(100); // Small delay to stabilize sensor


}

You might also like