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:
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);
}
}
BUY COMPONENTS:
1. ARDUINO UNO.
https://www.amazon.in/Robotbanao-Atmega328p-Cable-Length-Black/dp/B06XBMB9T1/ref=sr_1_5?dchild=1&keywords=ARDUINO+UNO&qid=1596336815&sr=8-5
2. L298N MOTOR DRIVER.
https://www.amazon.in/Driver-Module-Bridge-Stepper-Arduino/dp/B07D4MJVMF/ref=sr_1_1_sspa?crid=24ROQB8KZDSIY&dchild=1&keywords=l298n+motor+driver+module&qid=1596336840&sprefix=L298N%2Caps%2C868&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUFSVkVUVjdSVVFWOUgmZW5jcnlwdGVkSWQ9QTAxMzQxMDNMWjk1M1VYNTRKM1UmZW5jcnlwdGVkQWRJZD1BMDgzNTc3N1VaOFcxRDlDUUsxUyZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=
3. DC MOTORS WITH WHEELS.
https://www.amazon.in/INVENTO-2pcs-6V-Kg-cm-Geared/dp/B083SQ6QMW/ref=sr_1_15?dchild=1&keywords=dc+motor+and+wheels&qid=1596336875&sr=8-15
4. JOYSTICK MODULE.
https://www.amazon.in/Robocraze-JoyStick-Module-PS2-RC-A-040/dp/B073B647CW/ref=sr_1_1?dchild=1&keywords=JOYSTICK+MODULE&qid=1596336904&sr=8-1
5. JUMPER WIRES.
https://www.amazon.in/Jumper-Wires-Male-female-Pieces/dp/B00ZYFX6A2/ref=sr_1_6?dchild=1&keywords=JUMPER+WIRES&qid=1596336954&sr=8-6
6. LI-ON BATTERIES.
https://www.amazon.in/Volt-Rechargeable-Lithium-ion-Battery-Power/dp/B085XX498K/ref=sr_1_2?crid=2HYDEC6OMSAMW&dchild=1&keywords=lion+battery+3.7v+rechargeable&qid=1596337012&sprefix=LION+BATTER%2Caps%2C863&sr=8-2
0 Comments
Please do not Enter any Spam Link in the Comment Box
Emoji