Arduino Nano Controlled DC Motor with Encoder for Precise Movement
View and Edit Project In Cirkit Designer
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
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
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
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
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
- D2 connected to Encoder A of DC Motor with Encoder
- D3 connected to Encoder B of DC Motor with Encoder
- D7 connected to IN1 of L298N DC Motor Driver
- D8 connected to IN2 of L298N DC Motor Driver
- D9 connected to ENA of L298N DC Motor Driver
- VIN connected to 5V of L298N DC Motor Driver
- GND connected to GND of DC Motor with Encoder
- 5V connected to VCC of DC Motor with Encoder
L298N DC Motor Driver
- IN1 connected to D7 of Arduino Nano
- IN2 connected to D8 of Arduino Nano
- ENA connected to D9 of Arduino Nano
- 5V connected to VIN of Arduino Nano
- OUT1 connected to Motor -ve of DC Motor with Encoder
- OUT2 connected to Motor +ve of DC Motor with Encoder
- 12V connected to 12V-24V Output (DC) of Power Supply 12V 5AMP
- GND connected to GND (DC) of Power Supply 12V 5AMP
DC Motor with Encoder
- Encoder A connected to D2 of Arduino Nano
- Encoder B connected to D3 of Arduino Nano
- GND connected to GND of Arduino Nano
- VCC connected to 5V of Arduino Nano
- Motor -ve connected to OUT1 of L298N DC Motor Driver
- Motor +ve connected to OUT2 of L298N DC Motor Driver
Power Supply 12V 5AMP
- 12V-24V Output (DC) connected to 12V of L298N DC Motor Driver
- GND (DC) connected to GND of L298N DC Motor Driver
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.