当前位置:首页 > 科学研究 > 电子编程 > 正文内容

Arduino Project 030A - Dual Motor Driver L298N

RonWang1年前 (2024-11-22)电子编程612

L298N Dual Motor Driver Project Description 

The L298N Motor Driver is a controller that uses an H-Bridge to easily control motors direction and PWM to control the speed. This module allows you to independently manage two motors of up to 2A each in both directions. Supply range may vary between 5V and 35V, enough for most DC motor projects.

In first place, let's understand the meaning of each pin provided on L298N board.

L298N Pinout

l298n pinout arduino Motor driver

The spinning direction of the motor can be controlled by applying logic HIGH or logic LOW to IN1/IN2 (motor A) and IN3/IN4 (motor B) inputs as shown on table.

The speed control pins ENA and ENB are used to turn on/off the motors and control their speed with Pulse Width Modulation (PWM). That means that motor will spin at full speed with HIGH and stop with LOW. Usually the module comes with a jumper on these pins that connects them directly to HIGH value. If you want to keep the speed control of your motors remove the jumper and connect ENA and/or ENB pins to PWM. Otherwise the motor will always spin at full speed.

Voltage regulator

The module includes 5V regulator that can be enabled/disabled using a jumper:

  • With jumper: the regulator is enabled and +5V pin acts as output. It can be used to power an Arduino or other circuitry that needs 5V power. Note: with power supply greater than 12V the jumper must be removed to prevent damage to the onboard regulator.

  • Without jumper: the regulator is disabled and +5V pin acts as input, expecting 5V to be supplied.

Take into consideration that the L298N module has voltage drop of approximately 2V. With 12V power supply the motors will receive approximately 10V which means that we won't be able to get the maximum speed.

Circuit and Schematic 

Having a solid understanding of each pin and how the module works we can proceed with the wiring.

Arduino L298N Dual Motor Driver Circuit


Arduino L298N Dual Motor Driver Schematic

Coding

// Motor A   
#define ENA_PIN 5 //PWM   
#define IN1_PIN 10   
#define IN2_PIN 11   
// Motor B   
#define ENB_PIN 3 //PWM   
#define IN3_PIN 8   
#define IN4_PIN 9   
struct motor {   
 byte speed = 0;   
 struct {   
   byte input1 = LOW;   
   byte input2 = LOW;   
 } direction;   
};   
motor motorA, motorB;   
void setup()   
{   
 Serial.begin(115200);   
 // Set PWM & direction pins to output for both motor   
 pinMode(ENA_PIN, OUTPUT);   
 pinMode(IN1_PIN, OUTPUT);   
 pinMode(IN2_PIN, OUTPUT);   
 pinMode(ENB_PIN, OUTPUT);   
 pinMode(IN3_PIN, OUTPUT);   
 pinMode(IN4_PIN, OUTPUT);   
 // Init with default values   
 sendToMotorA();   
 sendToMotorB();   
}   
void loop()   
{   
 Serial.println("Motors are stopped now");   
 Serial.println("Set direction FORWARD");   
 delay(2000);   
 setMotorDirectionForward(motorA);   
 setMotorDirectionForward(motorB);   
 Serial.println("Gradually increase motors speed to max");   
 increaseMotorsSpeed();   
 Serial.println("Motors are on full speed now");   
 delay(2000);   
 Serial.println("Gradually decrease motors speed to min");   
 decreaseMotorsSpeed();   
 Serial.println("Motors are stopped now");   
 Serial.println("Set direction BACKWARD");   
 delay(2000);   
 setMotorDirectionBackward(motorA);   
 setMotorDirectionBackward(motorB);   
 Serial.println("Gradually increase motors speed to max");   
 increaseMotorsSpeed();   
 Serial.println("Motors are on full speed now");   
 delay(2000);   
 Serial.println("Gradually decrease motors speed to min");   
 decreaseMotorsSpeed();   
}   
void sendToMotorA()   
{   
 sendToMotor(motorA, ENA_PIN, IN1_PIN, IN2_PIN);   
}   
void sendToMotorB()   
{   
 sendToMotor(motorB, ENB_PIN, IN3_PIN, IN4_PIN);   
}   
void increaseMotorsSpeed()   
{   
 for (int speed = 0; speed <= 255; speed++) {   
   setMotorSpeed(motorA, speed);   
   setMotorSpeed(motorB, speed);   
   sendToMotorA();   
   sendToMotorB();   
   delay(20); // Add small delay between changes   
 }   
}   
void decreaseMotorsSpeed()   
{   
 for (int speed = 255; speed >= 0; speed--) {   
   setMotorSpeed(motorA, speed);   
   setMotorSpeed(motorB, speed);   
   sendToMotorA();   
   sendToMotorB();   
   delay(20); // Add small delay between changes   
 }   
}

版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://www.parentscn.com/?id=286

标签: Arduino

相关文章

C语言调试运行环境TurboC的安装

C语言调试运行环境TurboC的安装

上大学时学习《C语言程序设计》(第二版)作者谭浩强,大部分编程时间是在学校机房度过,每次点开桌面的TurboC图标,就开始在里面敲代码,然后保存程序Abc.C,下一步进行编译,如果编译成功的话,就可以...

Arduino Project 008 - RGB LED Mood Lamp

Arduino Project 008 - RGB LED Mood Lamp

In the last project, you learned how to adjust the brightness of an LED using the PWM capabilities o...

Arduino Project 029 - Control Stepper Motor ULN2004A

Arduino Project 029 - Control Stepper Motor ULN2004A

Stepper motors, due to their unique design, can be controlled to a high degree of accuracy without a...

Arduino Project 032 - BMP280 Pressure Sensor LCD Display

Arduino Project 032 - BMP280 Pressure Sensor LCD Display

For this project we will use Arduino Uno and BMP280 along with LCD 16x2 display module to display te...

C语言调试运行环境Pelles C的安装

C语言调试运行环境Pelles C的安装

C语言调试运行环境之TurboC一文介绍了在32位Windows系统下安装C语言运行环境之TubroC,但是由于TurobC只能在32位系统下运行,导致现在很多Windows10和Windows 11...

Arduino Project 039 - Ultrasonic Distance Display

Arduino Project 039 - Ultrasonic Distance Display

Ultrasonic sensors measure distance by sending and receiving the ultrasonic wave. The ultrasonic sen...