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

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

Image 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

  • 5V connected to +VCC of the Ultrasonic Sensor
  • GND connected to GND of the Ultrasonic Sensor
  • D10 connected to Trigger of the Ultrasonic Sensor
  • D9 connected to Echo of the Ultrasonic Sensor
  • D5 connected to ENA of the L298N DC Motor Driver
  • D3 connected to IN1 of the L298N DC Motor Driver
  • D2 connected to IN2 of the L298N DC Motor Driver

Ultrasonic Sensor

  • +VCC connected to 5V of the Arduino UNO
  • GND connected to GND of the Arduino UNO
  • Trigger connected to D10 of the Arduino UNO
  • Echo connected to D9 of the Arduino UNO

L298N DC Motor Driver

  • ENA connected to D5 of the Arduino UNO
  • IN1 connected to D3 of the Arduino UNO
  • IN2 connected to D2 of the Arduino UNO
  • OUT1 connected to pin 2 of the first DC Motor
  • OUT2 connected to pin 1 of the first DC Motor
  • OUT3 connected to pin 1 of the second DC Motor
  • OUT4 connected to pin 2 of the second DC Motor
  • 12V connected to 12v + of the 12V 7Ah Battery
  • GND connected to 12v - of the 12V 7Ah Battery

12V 7Ah Battery

  • 12v + connected to 12V of the L298N DC Motor Driver
  • 12v - connected to GND of the L298N DC Motor Driver

DC Motor (First)

  • pin 1 connected to OUT2 of the L298N DC Motor Driver
  • pin 2 connected to OUT1 of the L298N DC Motor Driver

DC Motor (Second)

  • pin 1 connected to OUT3 of the L298N DC Motor Driver
  • pin 2 connected to OUT4 of the L298N DC Motor Driver

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.