Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Automated Gate with Ultrasonic Sensor and Servo Motor

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Automated Gate with Ultrasonic Sensor and Servo Motor

Circuit Documentation

Summary

This circuit utilizes an Arduino UNO microcontroller to control a Tower Pro SG90 servo motor and an HC-SR04 ultrasonic sensor. The ultrasonic sensor measures the distance to an object, and based on this distance, the servo motor is used to open or close a gate. The Arduino UNO reads the distance from the ultrasonic sensor and controls the servo motor accordingly.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. HC-SR04 Ultrasonic Sensor

    • Description: A sensor used to measure distance by using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND
  3. Tower Pro SG90 Servo

    • Description: A small servo motor used for precise control of angular position.
    • Pins: Signal, +5V, GND

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Tower Pro SG90 Servo

Code Documentation

#include <Servo.h>  // Include Servo library

Servo gateServo;  // Create a servo object
int trigPin = 9;  // Pin for Trig of ultrasonic sensor
int echoPin = 10;  // Pin for Echo of ultrasonic sensor
long duration;  // Store the duration of the pulse (long)
float distance;  // Store the calculated distance (float)

void setup() {
  gateServo.attach(6);  // Attach servo to pin 6
  pinMode(trigPin, OUTPUT);  // Set Trig pin as output
  pinMode(echoPin, INPUT);  // Set Echo pin as input
  Serial.begin(9600);  // Start the serial communication
}

void loop() {
  digitalWrite(trigPin, LOW);  // Clear the Trig pin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);  // Send pulse to trigger
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);  // Stop sending pulse

  duration = pulseIn(echoPin, HIGH);  // Measure the duration of the pulse
  distance = duration * 0.034 / 2;  // Calculate the distance (float)

  Serial.print("Distance: ");
  Serial.println(distance);  // Print the distance to serial monitor

  if (distance < 20.0) {  // If an object is detected within 20cm
    gateServo.write(90);  // Open the gate (servo at 90 degrees)
  } else {
    gateServo.write(0);  // Close the gate (servo at 0 degrees)
  }
  
  delay(500);  // Wait for half a second before checking again
}

This code controls the servo motor based on the distance measured by the ultrasonic sensor. The servo motor opens the gate when an object is detected within 20 cm and closes it otherwise. The distance is also printed to the serial monitor for debugging purposes.