DC MOTORS CONTROLLING WITH ( DIGITAL, PWM, ENABLE ) PINS AND WITH JOYSTI...




ARDUINO TUTORIALS PART-5
DC MOTORS TUTORIALS 

3-TYPES OF CODE FOR CONTROLLING DC MOTORS 
TYPE-1:- ANY DIGITAL PINS.
TYPE-2:- PWM PINS.
TYP-3:-  ENABLE PIN

 
COMPONENTS REQUIRED:
1. ARDUINO UNO. 2. L298N MOTOR DRIVER. 3. DC MOTORS WITH WHEELS. 4.JOYSTICK MODULE. 5.JUMPER WIRES. 6. LI-ON BATTERIES.

L298N MOTOR DRIVER: 
A. DIGITAL PINS
1. DC MOTORS CONTROLLING WITH ANY DIGITAL PINS.
2. DC MOTORS CONTROLLING WITH ANY DIGITAL PINS AND JOYSTICK.

B. PWM PINS
1. DC MOTORS CONTROLLING WITH PWM PINS.
2. DC MOTORS CONTROLLING WITH PWM PINS AND JOYSTICK.

C. ENABLE PINS
1. DC MOTORS CONTROLLING WITH ENABLE PINS.
2. DC MOTORS CONTROLLING WITH ENABLE PINS AND JOYSTICK.



A. DIGITAL PINS
1. DC MOTORS CONTROLLING WITH ANY DIGITAL PINS.
CIRCUIT:



CODE:
int LMT1 = 4;//LEFT MOTOR TERMINAL 1
int LMT2 = 5;//LEFT MOTOR TERMINAL 2

int RMT1 = 6;//RIGHT MOTOR TERMINAL 1
int RMT2 = 7;//RIGHT MOTOR TERMINAL 1

void setup() {
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);
}

void loop() {
//MOTORS ROTATE CLOCKWISE FOR 1 SECOND  
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
delay(1000);//1 second

//MOTORS ROTATE ANT CLOCKWISE FOR 1 SECOND  
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
delay(1000);//1 second

//MOTORS STOP ROTATING FOR 1 SECOND  
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);
delay(1000);//1 second
}

2. DC MOTORS CONTROLLING WITH ANY DIGITAL PINS AND JOYSTICK.
CIRCUIT:


CODE:

int LMT1 = 4;//LEFT MOTOR TERMINAL 1
int LMT2 = 5;//LEFT MOTOR TERMINAL 2
int RMT1 = 6;//RIGHT MOTOR TERMINAL 1
int RMT2 = 7;//RIGHT MOTOR TERMINAL 1


void setup() {
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);

pinMode(A0, INPUT);//JOYSTICK
}

void loop() {
int JOYSTICK = analogRead(A0);
//JOYSTICK VALUE FROM 0 TO 1023
//MIDDLE VALUE IS 512.


if(JOYSTICK < 500){
//MOTORS ROTATE CLOCKWISE 
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
}

else if(JOYSTICK > 520){
//MOTORS ROTATE ANTI-CLOCKWISE  
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
}
else{
//MOTORS STOP ROTATING 
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);
}
}



B. PWM PINS
1. DC MOTORS CONTROLLING WITH PWM PINS.
CIRCUIT:



CODE:
int LMT1 = 5; //LEFT MOTOR TERMINAL 1
int LMT2 = 6; //LEFT MOTOR TERMINAL 2
int RMT1 = 9; //RIGHT MOTOR TERMINAL 1
int RMT2 = 10; //RIGHT MOTOR TERMINAL 2

//5,6,9,10 ARE PWM PINS

void setup() {
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);
}

void loop() {
//SPEED FROM 0 TO 255
// MOTORS ROTATE CLOCKWISE WITH MAXIMUM SPEED 255 FOR 1 SECOND  
analogWrite(LMT1, 255);
analogWrite(LMT2, 0);
analogWrite(RMT1, 255);
analogWrite(RMT2, 0);
delay(1000);

// MOTORS ROTATE ANTI-CLOCKWISE WITH MAXIMUM SPEED 255 FOR 1 SECOND  
analogWrite(LMT1, 0);
analogWrite(LMT2, 255);
analogWrite(RMT1, 0);
analogWrite(RMT2, 255);
delay(1000);

// MOTORS STOP ROTATE FOR 1 SECOND  
analogWrite(LMT1, 0);
analogWrite(LMT2, 0);
analogWrite(RMT1, 0);
analogWrite(RMT2, 0);
delay(1000);
}

2. DC MOTORS CONTROLLING WITH PWM PINS AND JOYSTICK.
CIRCUIT:
CODE:
int LMT1 = 5; //LEFT MOTOR TERMINAL 1
int LMT2 = 6; //LEFT MOTOR TERMINAL 2
int RMT1 = 9; //RIGHT MOTOR TERMINAL 1
int RMT2 = 10; //RIGHT MOTOR TERMINAL 2

//5,6,9,10 ARE PWM PINS

void setup() {
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);

pinMode(A0, INPUT);//JOYSTICK
}

void loop() {
int JOYSTICK = analogRead(A0);
int POT_VALUE1 = map(JOYSTICK, 0, 1023, 0, 225);//ROTATE CLOCK WISE VALUE FROM 0 TO 1023 WITH SPEED 0 TO 255
int POT_VALUE2 = map(JOYSTICK, 1023, 0, 0, 225);//ROTATE ANTI CLOCK WISE VALUE FROM 1023 TO 0 WITH SPEED 0 TO 255


if(JOYSTICK > 520){  
//SPEED FROM 0 TO 255
// MOTORS ROTATE CLOCKWISE WITH MAXIMUM SPEED 255 
analogWrite(LMT1, POT_VALUE1);
analogWrite(LMT2, 0);
analogWrite(RMT1, POT_VALUE1);
analogWrite(RMT2, 0);
}
else if(JOYSTICK < 500){
// MOTORS ROTATE ANTI-CLOCKWISE WITH MAXIMUM SPEED 255 
analogWrite(LMT1, 0);
analogWrite(LMT2, POT_VALUE2);
analogWrite(RMT1, 0);
analogWrite(RMT2, POT_VALUE2);
}
else{
// MOTORS STOP ROTATE
analogWrite(LMT1, 0);
analogWrite(LMT2, 0);
analogWrite(RMT1, 0);
analogWrite(RMT2, 0);
}
}



C. ENABLE PINS
1. DC MOTORS CONTROLLING WITH ENABLE PINS.
CIRCUIT:



CODE:

int LMT1 = 4; //LEFT MOTOR TERMINAL 1
int LMT2 = 5; //LEFT MOTOR TERMINAL 2
int RMT1 = 6; //RIGHT MOTOR TERMINAL 1
int RMT2 = 7; //RIGHT MOTOR TERMINAL 2 

int ENABLEA = 3;//PWM PIN 3
int ENABLEB = 9;// PWM PIN 9

void setup() {
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);
pinMode(ENABLEA, OUTPUT);
pinMode(ENABLEB, OUTPUT);
}

void loop() {

 //MOTORS ROTATE CLOCKWISE WITH MAX SPEED 0 TO 255 
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
digitalWrite(ENABLEA, 255);//MAXIMUM SPEED 255
digitalWrite(ENABLEB, 255);
delay(1000);

 //MOTORS ROTATE ANTI CLOCKWISE WITH MAX SPEED 0 TO 255 
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
digitalWrite(ENABLEA, 255);//MAXIMUM SPEED 255
digitalWrite(ENABLEB, 255);
delay(1000);


 //MOTORS STOP ROTATE 
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);
digitalWrite(ENABLEA, 0);
digitalWrite(ENABLEB, 0);
delay(1000);

}

2. DC MOTORS CONTROLLING WITH ENABLE PINS AND JOYSTICK.
CIRCUIT:
                                        
CODE:

int LMT1 = 4; //LEFT MOTOR TERMINAL 1
int LMT2 = 5; //LEFT MOTOR TERMINAL 2
int RMT1 = 6; //RIGHT MOTOR TERMINAL 1
int RMT2 = 7; //RIGHT MOTOR TERMINAL 2 

int ENABLEA = 3;//PWM PIN 3
int ENABLEB = 9;// PWM PIN 9

void setup() {
pinMode(LMT1, OUTPUT);
pinMode(LMT2, OUTPUT);
pinMode(RMT1, OUTPUT);
pinMode(RMT2, OUTPUT);
pinMode(ENABLEA, OUTPUT);
pinMode(ENABLEB, OUTPUT);

pinMode(A0, INPUT);//JOYSTICK
}

void loop() {
int JOYSTICK = analogRead(A0);
int POT_VALUE1 = map(JOYSTICK, 0, 1023, 0, 255);//ROTATE CLOCKWISE
int POT_VALUE2 = map(JOYSTICK, 1023, 0, 0, 255);//ROTATE ANTI CLOCKWISE


if(JOYSTICK > 520){
 //MOTORS ROTATE CLOCKWISE WITH MAX SPEED 0 TO 255 
digitalWrite(LMT1, HIGH);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, HIGH);
digitalWrite(RMT2, LOW);
analogWrite(ENABLEA, POT_VALUE1);//MAXIMUM SPEED 255
analogWrite(ENABLEB, POT_VALUE1);
}



else if(JOYSTICK < 500){
 //MOTORS ROTATE ANTI CLOCKWISE WITH MAX SPEED 0 TO 255 
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, HIGH);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, HIGH);
analogWrite(ENABLEA, POT_VALUE2);//MAXIMUM SPEED 255
analogWrite(ENABLEB, POT_VALUE2);
}

else{
 //MOTORS STOP ROTATE 
digitalWrite(LMT1, LOW);
digitalWrite(LMT2, LOW);
digitalWrite(RMT1, LOW);
digitalWrite(RMT2, LOW);
analogWrite(ENABLEA, 0);
analogWrite(ENABLEB, 0);
}

}








Post a Comment

0 Comments