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

Arduino Nano Controlled DC Motor with Encoder for Precise Movement

View and Edit Project In Cirkit DesignerImage of Arduino Nano Controlled DC Motor with Encoder for Precise Movement

Circuit Documentation

Summary

This document provides a detailed overview of a circuit designed to control a DC motor with an encoder using an Arduino Nano and an L298N motor driver. The circuit includes a power supply to provide the necessary voltage and current for the motor driver and the motor. The Arduino Nano is programmed to control the motor's direction and speed, as well as to read the encoder signals to track the motor's position.

Component List

  1. Arduino Nano

    • Description: A small, complete, and breadboard-friendly board based on the ATmega328P.
    • Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
  2. L298N DC Motor Driver

    • Description: A dual H-Bridge motor driver that allows control of the speed and direction 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
  3. DC Motor with Encoder

    • Description: A DC motor equipped with an encoder to provide feedback on the motor's position.
    • Pins: Encoder B, Encoder A, GND, VCC, Motor +ve, Motor -ve
  4. Power Supply 12V 5AMP

    • Description: A power supply unit that provides a stable 12V DC output with a maximum current of 5A.
    • Pins: 220V Positive Pole (AC), 220V Negative Pole (AC), GND, GND (DC), 12V-24V Output (DC)

Wiring Details

Arduino Nano

L298N DC Motor Driver

DC Motor with Encoder

Power Supply 12V 5AMP

Documented Code

// Motor driver pins
const int IN1 = 7;
const int IN2 = 8;
const int ENA = 9;

// Encoder pins
const int encoderA = 2;
const int encoderB = 3;

volatile long encoderPosition = 0; // Encoder position tracker

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

  // Set encoder pins as input
  pinMode(encoderA, INPUT_PULLUP);
  pinMode(encoderB, INPUT_PULLUP);

  // Attach interrupts for encoder
  attachInterrupt(digitalPinToInterrupt(encoderA), encoderISR, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoderB), encoderISR, CHANGE);

  // Start serial for debugging
  Serial.begin(9600);
}

void loop() {
  // Move motor down
  Serial.println("Moving Down...");
  moveMotorDown();
  delay(3000); // Adjust based on how far you want to move

  // Stop and pause
  stopMotor();
  delay(5000); // Wait for 5 seconds

  // Move motor up
  Serial.println("Moving Up...");
  moveMotorUp();
  delay(3000); // Adjust based on how far you want to move

  // Stop and pause
  stopMotor();
  delay(5000); // Wait for 5 seconds
}

// Interrupt Service Routine (ISR) for encoder
void encoderISR() {
  int stateA = digitalRead(encoderA);
  int stateB = digitalRead(encoderB);

  if (stateA == stateB) {
    encoderPosition++; // Forward
  } else {
    encoderPosition--; // Backward
  }
}

// Motor control functions
void moveMotorDown() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 200); // Adjust speed (0-255)
}

void moveMotorUp() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  analogWrite(ENA, 200); // Adjust speed (0-255)
}

void stopMotor() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 0);
}

This code is designed to control the motor's direction and speed using the L298N motor driver and to read the encoder signals to track the motor's position. The motor moves up and down with pauses in between, and the encoder's position is tracked using interrupts.