L298N Motor Drive Module
The L298N is a high-voltage, high-current motor drive chip produced by ST. The chip uses a 15-pin package. Main features: high operating voltage, with a maximum operating voltage up to 46V; large output current, with instantaneous peak current up to 3A and continuous operating current of 2A; rated power of 25W. It contains a high-voltage, high-current full-bridge driver with two H bridges, which can be used to drive DC motors, stepper motors, relay coils, and other inductive loads; it uses standard logic-level signal control; it has two enable control terminals that allow or disable device operation without being affected by input signals; it has a logic power input terminal that enables the internal logic circuit to work at low voltage; an external detection resistor can be connected to feed the variation back to the control circuit. Using the L298N chip to drive a motor, the chip can drive a two-phase stepper motor or a four-phase stepper motor, as well as two DC motors.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a21n57.1.0.0.6003523cOoWQjP&id=691582716758&ns=1&abbucket=0#detail
Specifications
Drive voltage: 5V~24V Drive current: 2A Logic voltage: 5V Logic current: 36 mA Control method: PWM
View Materials
When the drive voltage is 7V~12V, i.e., when the VCC motor drive terminal is connected to the drive power source, the onboard 78M05 supplies the chip's logic power, the indicator light is on, and no external logic power supply is needed; if the onboard 5V power supply of the motor drive is used, the +5V supply terminal in the interface should not be input with voltage, but the 5V voltage can be routed out for external use (this is the conventional application!).
When the drive voltage is higher than 12V and less than or equal to 24V (the chip manual states that it can support up to 35V, but in our experience, supporting up to 24V for the L298 conservatively is already impressive!), for example, to drive a motor with a rated voltage of 18V. First, you must disconnect the onboard 5V enable, the indicator light turns off, do not use the onboard 78M05 to supply the chip's logic power, and then connect a 5V voltage externally at the 5V output port to power the L298N's internal logic circuit. (This is the unconventional high-voltage drive application!)
The 5V enable is a control signal with a level of 5V. When this signal is validly input and the motor drive module is normally powered, the motor drive module outputs current. Otherwise, even if the power supply is normal, there will be no current on the motor.
The L298N enable terminals (active high, normally connected to VCC via a jumper cap) can be used to implement PWM speed control through these two ports (remove the jumper cap when using PWM speed control). ENA and ENB are connected to PWM signals, and IN1, IN2, IN3, IN4 are normally connected to high/low levels to make the motor rotate forward, reverse, or stop.
Note: If the 5V power supply of the L298N is supplied by another power source (i.e., not shared with the MCU's power supply), you need to connect the MCU's GND and the module's GND together. Only then will the logic signal coming from the MCU have a reference 0 point. The input pin of the onboard 5V voltage regulator chip is conductive with the motor supply drive wiring terminal.
Porting Process
Pin Selection
The L298N controls the motor speed by connecting IN1 and IN2 to PWM and directly adjusting the duty cycle of the PWM for speed control. Therefore, IN1/IN2/IN3/IN4 all need to use the PWM function.
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting.
For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here.
Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_L298N.c and bsp_L298N.h, and the folder name to L298N.
Write Code
In the file bsp_L298N.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-15 LCKFB-lp first version
*/
#include "bsp_L298N.h"
/******************************************************************
* Function Name: L298N_Init
* Function Description: PWM configuration
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes:
******************************************************************/
void L298N_Init(void)
{
// Prepare and apply the PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = PWM_MODE, // Low speed mode
.timer_num = PWMA_TIMER, // Timer source of the channel Timer 0
.duty_resolution = LEDC_DUTY_RES, // Set duty cycle resolution to 13 bits
.freq_hz = LEDC_FREQUENCY, // Set output frequency to 5 kHz
.clk_cfg = LEDC_AUTO_CLK // Set the LED PWM clock source to automatic
//LEDC_AUTO_CLK = When starting the timer, the source clock will be automatically selected based on the given resolution and duty cycle parameters
};
ledc_timer_config(&ledc_timer);
// Prepare and apply the LEDC1 PWM channel configuration
ledc_channel_config_t ledc1_channel = {
.speed_mode = PWM_MODE, // Low speed mode
.channel = PWMA1_CHANNEL, // Channel 0
.timer_sel = PWMA_TIMER, // Timer source Timer 0
.intr_type = LEDC_INTR_DISABLE, // Disable interrupt
.gpio_num = GPIO_IN1, // Output pin GPIO1
.duty = 0, // Set duty cycle to 0
.hpoint = 0
};
ledc_channel_config(&ledc1_channel);
// Prepare and apply the LEDC2 PWM channel configuration
ledc_channel_config_t ledc2_channel = {
.speed_mode = PWM_MODE, // Low speed mode
.channel = PWMA2_CHANNEL, // Channel 1
.timer_sel = PWMA_TIMER, // Timer source Timer 0
.intr_type = LEDC_INTR_DISABLE, // Disable interrupt
.gpio_num = GPIO_IN2, // Output pin GPIO2
.duty = 0, // Set duty cycle to 0
.hpoint = 0
};
ledc_channel_config(&ledc2_channel);
}
/******************************************************************
* Function Name: AO_Control
* Function Description: Motor control of port A
* Function Parameters: dir rotation direction 1 forward 0 reverse speed rotation speed, range (0 ~ per-1)
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void AO_Control(uint8_t dir, uint32_t speed)
{
if( dir == 1 )
{
// Set AIN1 duty cycle
ledc_set_duty(PWM_MODE, PWMA1_CHANNEL, 0);
ledc_update_duty(PWM_MODE, PWMA1_CHANNEL);
// Set AIN2 duty cycle
ledc_set_duty(PWM_MODE, PWMA2_CHANNEL, speed);
ledc_update_duty(PWM_MODE, PWMA2_CHANNEL);
}
else
{
// Set AIN1 duty cycle
ledc_set_duty(PWM_MODE, PWMA1_CHANNEL, speed);
ledc_update_duty(PWM_MODE, PWMA1_CHANNEL);
// Set AIN2 duty cycle
ledc_set_duty(PWM_MODE, PWMA2_CHANNEL, 0);
ledc_update_duty(PWM_MODE, PWMA2_CHANNEL);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
In the file bsp_L298N.h, write the following code.
#ifndef _BSP_L298N_H
#define _BSP_L298N_H
#include <stdio.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "driver/spi_common.h"
#include "hal/gpio_types.h"
#include "driver/ledc.h"
#include "string.h"
#define GPIO_IN1 1
#define GPIO_IN2 2
#define PWMA_TIMER LEDC_TIMER_0 // Timer 0
#define PWMA1_CHANNEL LEDC_CHANNEL_0 // Use LEDC channel 0
#define PWMA2_CHANNEL LEDC_CHANNEL_1 // Use LEDC channel 0
#define PWM_MODE LEDC_LOW_SPEED_MODE // Low speed mode
#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // LEDC resolution set to 13 bits
#define LEDC_DUTY (4095) // Set duty cycle to 50%. ((2^13) - 1) * 50% = 4095
#define LEDC_FREQUENCY (5000) // Frequency unit is Hz. Set frequency to 5000 Hz
void L298N_Init(void);
void AO_Control(uint8_t dir, uint32_t speed);
#endif /* BSP_L298N_H */2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Porting Verification
In the main function of your project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-15 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_L298N.h"
#include "string.h"
#include "esp_private/esp_task_wdt.h"
#include "esp_private/esp_task_wdt_impl.h"
int app_main(void)
{
int i = 0;
esp_task_wdt_deinit();
L298N_Init();
printf("L298N Start......\r\n");
while(1)
{
i += 500;
if( i > 6000 ) i = 0;
AO_Control(0,i);
printf("PWM-AO: %d \r\n",i);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Power-on effect:
Driver code:
File Download
📌 Materials Download Center (Click to Jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.