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

Arduino UNO-Based Ultrasonic Sensor-Controlled DC Motor System with L298N Driver

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Ultrasonic Sensor-Controlled DC Motor System with L298N Driver

Circuit Documentation

Summary

This circuit is designed to control a DC motor using an L298N motor driver and an ultrasonic sensor, all managed by an Arduino UNO. The ultrasonic sensor detects clicks based on distance measurements, and the motor responds by moving forward on a single click and backward on a double click.

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. Ultrasonic Sensor

    • Description: A sensor used to measure distance by using ultrasonic waves.
    • Pins: +VCC, Trigger, Echo, GND
  3. L298N DC Motor Driver

    • Description: A dual H-Bridge motor driver that allows control of two DC motors.
    • 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
  4. 12V 7Ah Battery

    • Description: A rechargeable battery providing 12V power.
    • Pins: 12v +, 12v -
  5. DC Motor

    • Description: A motor that runs on direct current (DC).
    • Pins: pin 1, pin 2
  6. Photodiode

    • Description: A semiconductor device that converts light into an electrical current.
    • Pins: cathode, anode

Wiring Details

Arduino UNO

Ultrasonic Sensor

L298N DC Motor Driver

12V 7Ah Battery

DC Motor (First)

DC Motor (Second)

Code Documentation

/*
 * This Arduino sketch controls a DC motor using an L298N motor driver and an
 * ultrasonic sensor. A single click on the ultrasonic sensor triggers the motor
 * to move forward, while a double click triggers the motor to move backward.
 */

// Pin definitions
const int triggerPin = 10;
const int echoPin = 9;
const int motorENAPin = 5;
const int motorIN1Pin = 3;
const int motorIN2Pin = 2;

// Variables for ultrasonic sensor
long duration;
int distance;

// Variables for click detection
unsigned long lastClickTime = 0;
int clickCount = 0;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize motor control pins
  pinMode(motorENAPin, OUTPUT);
  pinMode(motorIN1Pin, OUTPUT);
  pinMode(motorIN2Pin, OUTPUT);

  // Initialize ultrasonic sensor pins
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // Measure distance using ultrasonic sensor
  distance = measureDistance();

  // Check for clicks
  if (distance < 10) { // Assuming a click is detected when distance < 10 cm
    unsigned long currentTime = millis();
    if (currentTime - lastClickTime < 500) { // Double click detected
      clickCount++;
    } else { // Single click detected
      clickCount = 1;
    }
    lastClickTime = currentTime;
  }

  // Perform actions based on click count
  if (clickCount == 1) {
    moveForward();
  } else if (clickCount == 2) {
    moveBackward();
    clickCount = 0; // Reset click count after double click
  }

  delay(100); // Small delay to debounce
}

int measureDistance() {
  // Clear the trigger pin
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);

  // Set the trigger pin HIGH for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  // Read the echo pin
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  int distance = duration * 0.034 / 2;
  return distance;
}

void moveForward() {
  digitalWrite(motorENAPin, HIGH);
  digitalWrite(motorIN1Pin, HIGH);
  digitalWrite(motorIN2Pin, LOW);
}

void moveBackward() {
  digitalWrite(motorENAPin, HIGH);
  digitalWrite(motorIN1Pin, LOW);
  digitalWrite(motorIN2Pin, HIGH);
}

This code initializes the pins for the motor driver and ultrasonic sensor, measures the distance using the ultrasonic sensor, and controls the motor based on the detected clicks.