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

Arduino UNO Controlled Motor with Limit Switches and Pushbutton Interface

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Motor with Limit Switches and Pushbutton Interface

Circuit Documentation

Summary

This circuit is designed to control a power window using an Arduino UNO microcontroller and an L298N DC motor driver. The system includes limit switches to detect the end positions of the window and pushbuttons to start and stop the window movement. The Arduino UNO controls the motor driver, which in turn drives the power window motor. The circuit is powered by a 12V power supply.

Component List

Arduino UNO

L298N DC Motor Driver

Limit Switches (x2)

Pushbuttons (x2)

Power Window

12V Power Supply

Wiring Details

Arduino UNO

L298N DC Motor Driver

Limit Switches

Pushbuttons

Power Window

12V Power Supply

Documented Code

// Pin Definitions
const int IN1 = 8;        // L298N IN1
const int IN2 = 9;        // L298N IN2
const int ENA = 10;       // L298N ENA (PWM)

const int limitSwitch1 = 2; // Left End Limit Switch (Home position)
const int limitSwitch2 = 3; // Right End Limit Switch

const int startButton = 4;  // Start Button
const int stopButton = 5;   // Stop Button

// State Variables
bool moving = false;
bool movingForward = true;

void setup() {
  // Initialize motor control pins
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);

  // Initialize limit switches as input with internal pull-up
  pinMode(limitSwitch1, INPUT_PULLUP);
  pinMode(limitSwitch2, INPUT_PULLUP);

  // Initialize buttons as input with internal pull-up
  pinMode(startButton, INPUT_PULLUP);
  pinMode(stopButton, INPUT_PULLUP);

  // Initialize Serial Monitor for debugging (optional)
  Serial.begin(9600);

  // Stop the motor initially
  stopMotor();
}

void loop() {
  // Read button states
  bool startPressed = (digitalRead(startButton) == LOW);
  bool stopPressed = (digitalRead(stopButton) == LOW);

  // Handle Stop Button
  if (stopPressed) {
    Serial.println("Stop button pressed.");
    stopMotor();
    moving = false;
  }

  // Handle Start Button
  if (startPressed && !moving) {
    Serial.println("Start button pressed.");
    moving = true;
    movingForward = true; // Start moving forward initially
    moveMotor(movingForward);
  }

  // If motor is moving, check limit switches
  if (moving) {
    if (movingForward && digitalRead(limitSwitch1) == LOW) {
      Serial.println("Reached Limit Switch 1. Reversing direction.");
      movingForward = false;
      moveMotor(movingForward);
    }
    else if (!movingForward && digitalRead(limitSwitch2) == LOW) {
      Serial.println("Reached Limit Switch 2. Reversing direction.");
      movingForward = true;
      moveMotor(movingForward);
    }
  }

  // Small delay for stability
  delay(50);
}

// Function to move motor forward or backward
void moveMotor(bool forward) {
  if (forward) {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    analogWrite(ENA, 100); // Full speed
    Serial.println("Motor moving forward.");
  } else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, HIGH);
    analogWrite(ENA, 100); // Full speed
    Serial.println("Motor moving backward.");
  }
}

// Function to stop the motor
void stopMotor() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 0); // Stop the motor
  Serial.println("Motor stopped.");
}

This code is designed to run on an Arduino UNO and controls a DC motor through the L298N motor driver. It uses digital inputs to read the state of the limit switches and pushbuttons, and it controls the motor's direction and speed using digital outputs and PWM. The code includes functions for stopping the motor and moving it in either direction, as well as serial output for debugging purposes.