In this article, you will find answers to your searches about dc motor control with arduino, use of l298n motor driver board with arduino and arduino l298n motor speed control

You can easily drive brushed dc motors with L298N motor driver board. With this circuit board, you can control two different dc motors at the same time completely independently of each other. Can deliver up to 2 amps of output current to motor channels. You should take care not to draw more current than this, otherwise you may cause malfunction of your motor driver card. Before controlling our dc motors with Arduino, let's take a look at the components on the L298N motor driver board and perform the circuit setup for our needs.

On our board, there are two terminals required to connect our two-pin motors, and one three-pin power terminal. When we look at our power pins, we see that there is a 12V supply input, a GND pin and a 5V output so that we can give the necessary power to our card. This 5V output can also be used to power other components in our circuit, if the clamps that are in line with our power terminal are attached. You should be careful not to set the input voltage higher than 12 V. You may damage the L298n motor driver board.

When you take a look at the other pins on our board, you will notice that those pins are logic pins. ENA and ENB pins are related to whether or not we want to control the speed of our motors. If our ENA clamp is attached, our motor will rotate at full speed if we provide the required voltage. If we want to control the speed of our motor, we can do this by removing the clamp on the ENA and connecting this pin to one of the PWM pins on the arduino. The same goes for the other engine. The IN1 and IN2 pins receive the signals required for the A motor to run, while the IN3 and IN4 pins receive the signals required for the B motor to run from the Arduino. Let's build a simple circuit and code it.

STEP 1: ARDUINO L298N MOTOR CONTROL 


  • Arduino
  • L298N
  • DC motor x2
  • Güç Kaynağı

STEP 2: ARDUINO L298N CONNECTION


Make a connection similar to the connection given below. The most important point you should pay attention to is that the GND pin on the Arduino, the GND pin of the power supply and the GND pin of the L298N board must be connected to the same place.



STEP 3: ARDUINO L298N CODE


In1-In2 pins are the pins responsible for our A motor and In3-In4 pins are the pins responsible for our B motor. If there are those who have driven motor with "H Bridge" before, they can easily understand this place. If there are those who have not had such an experience before, it can be explained as follows. If we give any of the two pins responsible for the motors "HIGH" and the other "LOW", our motor will move in one direction. If we give the opposite, it will rotate in the other direction and when we give "LOW" to both, the motor will stop. In the code below, I coded according to possible scenarios and their explanations are available. You can easily understand it by working on the code.

#define in1 3
#define in2 5
#define in3 6
#define in4 9

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {
  
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(4000); 
  
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  delay(4000);
  
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  delay(4000); 
  
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  delay(4000);
  
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  delay(4000); 
  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(4000);
  
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  delay(4000); 
  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
  delay(4000);
} 
Now let's take a look at how we can achieve this if our dc motor is not rotating at the speed we want.

ARDUINO L298N MOTOR SPEED CONTROL

We have stated above that we can provide the speed control we want in our motors by removing the clamps connected to the ENA and ENB pins. We must connect these pins to the PWM pins on our Arduino. There is a (~) sign next to these pins. With this sign, you can easily understand which pins are PWM pins. From ENA and ENB PWM pins "analogWrite (enA, 255);" You can control the motor speed between 0 and 255 with the command. 

ARDUINO DC MOTOR SPEED CONTROL CONNECTION




ARDUINO DC MOTOR SPEED CONTROL CODE


#define in1 3
#define in2 5
#define in3 6
#define in4 9
#define enA 10
#define enB 11


void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
}

void loop() {
  // motor A
  analogWrite(enA,  255);   // A motor 255 full speed
  digitalWrite(in1, LOW);
  digitalWrite(in2,  HIGH);  
 
  // motor B
  analogWrite(enB,  40);   // B motor 40
  digitalWrite(in3, HIGH);
  digitalWrite(in4,  LOW);
} 
Using motor with Arduino L298N motor driver board is that simple.