Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino-Controlled Servo with Ultrasonic Distance Sensing

Image of Arduino-Controlled Servo with Ultrasonic Distance Sensing

Circuit Documentation

Summary of the Circuit

This circuit is designed to interface an HC-SR04 Ultrasonic Sensor and a Servo motor with an Arduino UNO microcontroller. The HC-SR04 sensor is used to measure distance, and the Servo motor is controlled based on the distance measurements. The Arduino UNO is programmed to trigger the ultrasonic sensor to send a pulse and then listen for the echo. The time taken for the echo to return is used to calculate the distance to an object. If the measured distance is less than 10 cm, the servo is instructed to move to a 90-degree position. Otherwise, the servo returns to the 0-degree position. The circuit is powered by a 5v Battery.

Component List

Servo

  • Description: A motor that can be positioned to specific angular positions by sending a pulse-width modulated (PWM) signal on its control wire.
  • Pins: gnd, vcc, pulse

HC-SR04 Ultrasonic Sensor

  • Description: A sensor that measures distance by emitting ultrasonic waves and measuring the time taken for the echo to return.
  • Pins: VCC, TRIG, ECHO, GND

5v Battery

  • Description: A power source that provides a stable 5V supply to the circuit.
  • Pins: + (positive), - (negative)

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P, with a variety of digital and analog I/O pins.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13

Comment

  • Description: A placeholder for additional notes or comments within the circuit design. This component does not have physical representation or pins.

Wiring Details

Servo

  • GND: Connected to Arduino UNO GND
  • VCC: Powered by the 5v Battery
  • Pulse: Controlled by Arduino UNO pin D9

HC-SR04 Ultrasonic Sensor

  • VCC: Powered by the 5v Battery
  • TRIG: Connected to Arduino UNO pin D12
  • ECHO: Connected to Arduino UNO pin D11
  • GND: Connected to Arduino UNO GND

5v Battery

  • + (positive): Provides power to Arduino UNO 5V and HC-SR04 VCC
  • - (negative): Connected to Arduino UNO GND and HC-SR04 GND

Arduino UNO

  • 5V: Receives power from the 5v Battery
  • GND: Common ground for the circuit
  • D9: Controls the Servo pulse
  • D12: Sends trigger pulse to HC-SR04 TRIG
  • D11: Receives echo signal from HC-SR04 ECHO

Documented Code

#include <Servo.h>
#define trigPin 12
#define echoPin 11
Servo servo;
int sound = 250;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo.attach(9);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  
  if (distance < 10) {
    Serial.println("the distance is less than 10");
    servo.write(90);
    delay(1500);
  }
  else {
    servo.write(0);
  }
  
  if (distance > 60 || distance <= 0){
    Serial.println("The distance is more than 60");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  
  delay(500);
}

This code is written for the Arduino UNO and is responsible for controlling the HC-SR04 Ultrasonic Sensor and the Servo motor. The setup() function initializes the serial communication, configures the trigger and echo pins, and attaches the servo to pin D9. The loop() function continuously measures the distance and moves the servo accordingly.