DC MOTOR SETUP WITH L298N

In this sketch we show the DC motor setup with the L298N module. This module can run 2 DC motors. The easiest application is for a 2-wheeled car robot.
For four motors you can use a DC motor shield , you can see on https://arduinoproyek.wordpress.com/dc-motor-shield/

DC motor setup

The code can be downloaded at:

https://github.com/triantara/ARDUINO-TRIANTARA/blob/main/L298N.zip

// Declare L298N Dual H-Bridge Motor Controller directly since there is not a library to load.

// Motor 1

int dir1PinA = 2;

int dir2PinA = 3;

int speedPinA = 9; // Needs to be a PWM pin to be able to control motor speed

// Motor 2

int dir1PinB = 4;

int dir2PinB = 5;

int speedPinB = 10; // Needs to be a PWM pin to be able to control motor speed

void setup() { // Setup runs once per reset

// initialize serial communication @ 9600 baud:

Serial.begin(9600);

//Define L298N Dual H-Bridge Motor Controller Pins

pinMode(dir1PinA,OUTPUT);

pinMode(dir2PinA,OUTPUT);

pinMode(speedPinA,OUTPUT);

pinMode(dir1PinB,OUTPUT);

pinMode(dir2PinB,OUTPUT);

pinMode(speedPinB,OUTPUT);

}

void loop() {

henti();

delay(1000);

maju();

delay(5000);

henti();

delay(1000);

mundur();

delay(5000);

henti();

delay(1000);

kanan();

delay(5000);

henti();

delay(1000);

kiri();

delay(5000);

}

void maju(){

analogWrite(speedPinA, 80);//Sets speed variable via PWM

digitalWrite(dir1PinA, LOW);

digitalWrite(dir2PinA, HIGH);

analogWrite(speedPinB, 80);

digitalWrite(dir1PinB, LOW);

digitalWrite(dir2PinB, HIGH);

}

void mundur(){

analogWrite(speedPinA, 80);

digitalWrite(dir1PinA, HIGH);

digitalWrite(dir2PinA, LOW);

analogWrite(speedPinB, 80);

digitalWrite(dir1PinB, HIGH);

digitalWrite(dir2PinB, LOW);

}

void kanan(){

analogWrite(speedPinA, 80);//Sets speed variable via PWM

digitalWrite(dir1PinA, LOW);

digitalWrite(dir2PinA, HIGH);

analogWrite(speedPinB, 80);

digitalWrite(dir1PinB, HIGH);

digitalWrite(dir2PinB, LOW);

}

void kiri(){

analogWrite(speedPinA, 80);

digitalWrite(dir1PinA, HIGH);

digitalWrite(dir2PinA, LOW);

analogWrite(speedPinB, 80);

digitalWrite(dir1PinB, LOW);

digitalWrite(dir2PinB, HIGH);

}

void henti(){

analogWrite(speedPinA, 0);

analogWrite(speedPinB, 0);

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s