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

Arduino-Controlled Dual Motor System with IR and Water Level Sensing

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Dual Motor System with IR and Water Level Sensing

Circuit Documentation

Summary

This circuit is designed to control two DC gear motors and two mini diaphragm water pumps using an Arduino UNO microcontroller. The circuit includes a 12V power supply, bridge rectifier, voltage regulator, and electrolytic capacitors for power management. Two L298N DC motor drivers are used to control the motors and pumps. The circuit also features a water level sensor and an IR sensor for input, and rocker switches to control the power flow. The Arduino UNO is programmed to operate the motors and pumps based on the sensor inputs, with specific run times for each motor.

Component List

DC Gear Motor

Bridge Rectifier

12V Power Supply

Electrolytic Capacitor

Rocker Switch

Voltage Regulator

Arduino UNO

IR Sensor

L298N DC Motor Driver

Mini Diaphragm Water Pump

Water Level Sensor

Power Jack

Wiring Details

DC Gear Motor

Bridge Rectifier

12V Power Supply

Electrolytic Capacitor

Rocker Switch

Voltage Regulator

Arduino UNO

IR Sensor

L298N DC Motor Driver

Mini Diaphragm Water Pump

Water Level Sensor

Power Jack

Documented Code

// Pin definitions for motor control and sensors
const int motor1_IN1 = 2; // Motor 1 direction pin 1
const int motor1_IN2 = 3; // Motor 1 direction pin 2
const int motor1_ENA = 9; // Motor 1 speed control (ENA)
const int motor2_IN3 = 4; // Motor 2 direction pin 1 (IN3)
const int motor2_IN4 = 5; // Motor 2 direction pin 2 (IN4)
const int motor2_ENB = 10; // Motor 2 speed control (ENB)
const int irSensorPin = 7; // IR Sensor output pin
const int waterSensorPin = 8; // Water Sensor output pin

// Timing variables
unsigned long previousMillisMotor1 = 0; // Stores the last time motor 1 was started
const long motor1RunTime = 10000; // Motor 1 run time in milliseconds (10 seconds)
bool motor1Running = false; // Track if motor 1 is currently running

unsigned long previousMillisMotor2 = 0; // Stores the last time motor 2 was started
const long motor2RunTime = 30000; // Motor 2 run time in milliseconds (30 seconds)
bool motor2Running = false; // Track if motor 2 is currently running

void setup() {
  // Set motor control pins as outputs
  pinMode(motor1_IN1, OUTPUT);
  pinMode(motor1_IN2, OUTPUT);
  pinMode(motor1_ENA, OUTPUT);
  pinMode(motor2_IN3, OUTPUT);
  pinMode(motor2_IN4, OUTPUT);
  pinMode(motor2_ENB, OUTPUT);
  
  // Set the IR sensor pin and water sensor pin as inputs
  pinMode(irSensorPin, INPUT);
  pinMode(waterSensorPin, INPUT);

  // Start with motors stopped
  stopMotor1();
  stopMotor2();

  // Start Serial communication
  Serial.begin(9600); // Initialize Serial at 9600 baud rate
}

void loop() {
  // Read IR sensor value
  int irState = digitalRead(irSensorPin);
  // Read water sensor value
  int waterState = digitalRead(waterSensorPin);

  // Display the sensor readings on Serial Monitor
  Serial.print("IR Sensor State: ");
  Serial.print(irState); // Print the current state of the IR sensor
  Serial.print(" | Water Sensor State: ");
  Serial.println(waterState); // Print the current state of the water sensor

  // Run motor 1 if the IR sensor is triggered
  if (irState == LOW && !motor1Running) { // Object detected and motor 1 not already running
    startMotor1(); // Start motor 1 if object detected
    previousMillisMotor1 = millis(); // Record the start time for motor 1
    motor1Running = true; // Set motor1Running to true
  }

  // Stop motor 1 after 10 seconds
  if (motor1Running) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillisMotor1 >= motor1RunTime) {
      stopMotor1(); // Stop motor 1 after 10 seconds
      motor1Running = false; // Reset motor1Running flag
    }
  }

  // Run motor 2 if the water sensor detects water and motor 2 is not already running
  if (waterState == HIGH && !motor2Running) {
    startMotor2(); // Start motor 2
    previousMillisMotor2 = millis(); // Record the start time for motor 2
    motor2Running = true; // Set motor2Running to true
  }

  // Stop motor 2 after 30 seconds
  if (motor2Running) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillisMotor2 >= motor2RunTime) {
      stopMotor2(); // Stop motor 2 after 30 seconds
      motor2Running = false; // Reset motor2Running flag
    }
  }

  delay(100); // Add a small delay for readability
}

// Function to start Motor 1
void startMotor1() {
  digitalWrite(motor1_IN1, HIGH); // Set direction for Motor 1
  digitalWrite(motor1_IN