DC Motor Angle Control
DC Motor Angle Control
Contents
Introduction
Aim & Objective
Materials
Software
Experiments
Conclusion
1.Introduction
In this experiment, we use Arduino and L298N motor driver to control a DC
motor. Arduino has to send commands to L298N motor controller and then
L298N decides the DC Motor Angle Control by manipulating the Arduino
commands. There are different ways to control a DC motor but the Arduino DC
motor control using L289N motor driver is becoming quite popular for many
reasons.
3.Materials
DC motor
L298N motor driver
Arduino UNO
9V battery
Jumper wires
Circuit board
4.Software
Arduino IDE
5.Experiment
Hardware setup
Connect the red and white wires of the DC motor to OUT1 and OUT2
pins of L298N respectively
Connect the blue and black wires of the DC motor to 5V power and
ground pins of Arduino respectively
Connect the yellow wire of the DC motor to the pin 2 of Arduino
Connect the power and GND pins of the 9V battery to the 12V input
pin and GND pin of the L298N respectively
Connect the pin 7 of Arduino to the enable pin of L298N
Software setup
#include <Encoder.h>
int stopAngle=270;
int encoderA=2;
int encoderValue=0;
float angle=0;
float ppr=1160;
float theda=0;
int forward=7;
int reverse=6;
int enable=5;
void calculateAngle();
void count();
void forwardr();
void reverser();
void enabler();
void stopr();
boolean motorStart=false;
void setup() {
pinMode(forward,OUTPUT);
pinMode(reverse,OUTPUT);
pinMode(enable,OUTPUT);
pinMode(encoderA,INPUT);
attachInterrupt(digitalPinToInterrupt(encoderA),count,FALLING);
Serial.begin(9600);
}
void loop() {
//stopAngle=map(analogRead(A0),0,1024,0,360);
while(!motorStart)
enabler();
forwardr();
calculateAngle();
if(angle>=stopAngle)
motorStart=true;
Serial.print("encoderValue");
Serial.println(encoderValue);
//calculateAngle();
stopr();
while(1){}
void count()
encoderValue++;
void calculateAngle()
angle=(encoderValue/ppr)*360;
Serial.print("angle ");
Serial.println(angle);
}
void forwardr()
digitalWrite(forward,HIGH);
digitalWrite(reverse,LOW);
void reverser()
digitalWrite(forward,LOW);
digitalWrite(reverse,HIGH);
void enabler()
digitalWrite(enable,HIGH);
void stopr()
digitalWrite(forward,LOW);
digitalWrite(enable,LOW);
6.Conclusion
In this experiment, we have controlled the direction and angle of the DC
motor. Using Arduino, we can send commands to the DC motor that in what
direction and with what angle it has to rotate.