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

Arduino UNO Controlled Servo Motor with HC-SR04 Ultrasonic Sensor for Proximity-Based Automation

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Servo Motor with HC-SR04 Ultrasonic Sensor for Proximity-Based Automation

Circuit Documentation

Summary

This circuit is designed to control a micro servo motor using an HC-SR04 ultrasonic distance sensor and an Arduino UNO. When an object is detected within a certain range, the servo motor closes. After 10 seconds, the servo motor opens again. The servo motor movement is slowed down by 50%.

Component List

  1. HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

    • Description: Ultrasonic sensor used to measure distance.
    • Pins: VCC, TRIG, ECHO, GND
  2. Arduino UNO

    • Description: 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
  3. Micro servo 9G

    • Description: Small servo motor used for precise control of angular position.
    • Pins: GND, +5V, PWM

Wiring Details

HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

Arduino UNO

Micro servo 9G

Code Documentation

/*
 * This Arduino sketch controls a micro servo motor using an HC-SR04 ultrasonic
 * distance sensor. When an object is detected within a certain range, the servo
 * motor closes. After 10 seconds, the servo motor opens again. The servo motor
 * movement is slowed down by 50%.
 */

#include <Servo.h>

// Pin definitions
const int trigPin = 2;
const int echoPin = 3;
const int servoPin = 9;

// Servo object
Servo myServo;

// Variables for distance measurement
long duration;
int distance;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Set pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  // Attach the servo to the pin
  myServo.attach(servoPin);
  
  // Initialize the servo to the open position
  myServo.write(90);
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Set the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Read the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculate the distance
  distance = duration * 0.034 / 2;
  
  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  
  // Check if the distance is within the threshold (e.g., 10 cm)
  if (distance < 10) {
    // Close the servo slowly
    for (int pos = 90; pos >= 0; pos--) {
      myServo.write(pos);
      delay(30); // Slow down by 50%
    }
    
    // Wait for 10 seconds
    delay(10000);
    
    // Open the servo slowly
    for (int pos = 0; pos <= 90; pos++) {
      myServo.write(pos);
      delay(30); // Slow down by 50%
    }
  }
  
  // Small delay before the next loop
  delay(500);
}

This code initializes the ultrasonic sensor and the servo motor, measures the distance to an object, and controls the servo motor based on the measured distance. The servo motor closes when an object is detected within 10 cm and opens after 10 seconds. The movement of the servo motor is slowed down by 50% to ensure smooth operation.