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

Arduino-Based Ultrasonic Sensor and Motor Control System with Servo Integration

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

Circuit Documentation

Summary

This circuit integrates an Arduino UNO microcontroller, an HC-SR04 Ultrasonic Sensor, an L298N DC motor driver, two DC motors, and a servo motor. The Arduino UNO controls the ultrasonic sensor to measure distance and blinks an LED based on the measured distance. The L298N motor driver is used to control the two DC motors, and the servo motor is also controlled by the Arduino.

Component List

  1. HC-SR04 Ultrasonic Sensor

    • Pins: VCC, TRIG, ECHO, GND
    • Description: Ultrasonic distance sensor used to measure the distance to an object.
    • Purpose in Circuit: Measures distance and sends the data to the Arduino.
  2. L298N DC Motor Driver

    • Pins: OUT1, OUT2, 12V, GND, 5V, OUT3, OUT4, 5V-ENA-JMP-I, 5V-ENA-JMP-O, +5V-J1, +5V-J2, ENA, IN1, IN2, IN3, IN4, ENB
    • Description: Dual H-Bridge motor driver used to control the speed and direction of two DC motors.
    • Purpose in Circuit: Controls the two DC motors based on signals from the Arduino.
  3. Arduino UNO

    • 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
    • Description: Microcontroller board based on the ATmega328P.
    • Purpose in Circuit: Central control unit that processes data from the ultrasonic sensor and controls the motors and servo.
  4. Servo

    • Pins: gnd, vcc, pulse
    • Description: Servo motor used for precise control of angular position.
    • Purpose in Circuit: Controlled by the Arduino to perform specific movements.
  5. DC Motor (Motor 1)

    • Pins: pin 1, pin 2
    • Description: Standard DC motor.
    • Purpose in Circuit: Controlled by the L298N motor driver to perform rotational movements.
  6. DC Motor (Motor 2)

    • Pins: pin 1, pin 2
    • Description: Standard DC motor.
    • Purpose in Circuit: Controlled by the L298N motor driver to perform rotational movements.
  7. 7.4v Battery

    • Pins: +, -
    • Description: Power source for the circuit.
    • Purpose in Circuit: Provides power to the Arduino and other components.

Wiring Details

HC-SR04 Ultrasonic Sensor

L298N DC Motor Driver

Arduino UNO

Servo

DC Motor (Motor 1)

DC Motor (Motor 2)

7.4v Battery

Code Documentation

/*
 * Arduino Sketch for Ultrasonic Sensor and LED Blinking
 * This sketch controls an ultrasonic sensor and an LED. The LED blinks
 * based on the distance measured by the ultrasonic sensor. If the distance
 * is between 3 cm and 5 cm, the LED blinks 5 times. If the distance is
 * between 10 cm and 20 cm, the LED blinks 10 times. The distance is also
 * printed to the Serial Monitor.
 */

// Pin definitions
const int trigPin = 9;     // TRIG pin for ultrasonic sensor
const int echoPin = 8;     // ECHO pin for ultrasonic sensor
const int ledPin = 13;     // LED pin

void setup() {
  // Initialize sensor and LED pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // Initialize Serial Monitor
  Serial.begin(9600);
}

void loop() {
  // Measure distance using ultrasonic sensor
  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;

  // Print distance to Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Control LED based on distance
  if (distance >= 3 && distance <= 5) {
    blinkLED(5);
  } else if (distance >= 10 && distance <= 20) {
    blinkLED(10);
  }

  delay(1000); // Wait for 1 second before next measurement
}

void blinkLED(int times) {
  for (int i = 0; i < times; i++) {
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
  }
}

This code initializes the ultrasonic sensor and an LED. It measures the distance using the ultrasonic sensor and blinks the LED based on the measured distance. The distance is also printed to the Serial Monitor.