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

Arduino UNO-Based Ultrasonic Distance Sensor with Servo Motor Control

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

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller, an HC-SR04 ultrasonic sensor, two Micro servo 9G motors, a pushbutton, and a resistor. The circuit is designed to detect objects within a certain range using the ultrasonic sensor and control the servo motors based on the detection and button press.

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. Micro servo 9G (Servo 1)

    • Description: A small servo motor used for precise control of angular position.
    • Pins: GND, +5V, PWM
  4. Micro servo 9G (Servo 2)

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

    • Description: A simple pushbutton used to provide user input.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4
  6. Resistor

    • Description: A 200 Ohms resistor used to limit current.
    • Pins: pin1, pin2
    • Properties: Resistance: 200 Ohms

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Micro servo 9G (Servo 1)

Micro servo 9G (Servo 2)

Pushbutton

Resistor

Code Documentation

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

// Pin configuration
const int trigPin = 2;    // Ultrasonic sensor Trig pin
const int echoPin = 3;    // Ultrasonic sensor Echo pin
const int buttonPin = 4;  // Button pin (using pull-up resistor)
Servo servo1;             // Servo motor 1 (connected to digital pin 9)
Servo servo2;             // Servo motor 2 (connected to digital pin 10)

// Variable declarations
int detect = 0;           // Object detection status (0: not detected, 1: detected)
long duration;            // Ultrasonic sensor signal duration
float distance;           // Distance to object (cm)
bool buttonPressed = false; // Button status
int servo2Position = -30; // Initial position of servo motor 2

void setup() {
  // Pin mode configuration
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Button pin in pull-up mode
  
  // Attach servos
  servo1.attach(9);   // Attach servo motor 1 to pin 9
  servo2.attach(10);  // Attach servo motor 2 to pin 10
  
  // Initial settings
  servo1.write(0);       // Initial position of servo motor 1
  servo2.write(servo2Position + 90); // Initial position of servo motor 2 (-30 degrees)
  Serial.begin(9600);    // Start serial communication (for debugging)
}

void loop() {
  // Measure distance using ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2; // Calculate distance
  
  // Object detection
  if (distance > 0 && distance <= 200) { // Detect within 200cm
    detect = 1;
  } else {
    detect = 0;
  }
  
  // Actions based on detection status
  if (detect == 1) {
    // Move servo motor 2 to 30 degrees when button is pressed
    if (digitalRead(buttonPin) == LOW && !buttonPressed) { // Check if button is pressed
      buttonPressed = true; // Record button press
      servo2Position = 0;   // Move servo motor to 0 degrees
      servo2.write(servo2Position + 90); // Set servo motor position
      Serial.println("Button pressed: Servo 2 moved to 0 degrees.");
    }
  } else {
    buttonPressed = false; // Reset button status
  }

  // Debugging output
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm, Detect: ");
  Serial.println(detect);
  delay(100);
}

This code initializes the pins and servos, measures the distance using the ultrasonic sensor, and controls the servo motors based on the distance and button press. The serial output is used for debugging purposes.