notes on interfacing L298N DC Motor Driver Module with ESP32
This blog post explores the practical interfacing of the L298N DC motor driver module with the ESP32 microcontroller, focusing on a principle of motor control, including direction and speed regulation it provides a a reference on integrating the L298N module with the ESP32 for DC motor control. The L298N, a dual H-bridge driver, is designed to also control the speed and direction of two DC motors independently, leveraging the H-bridge circuit to allow bidirectional current flow. The post includes an illustrative definition of pin connections, and an Arduino IDE code example to adjust motor speed and direction.
Example
Materials
- ESP32 DevKitc
- USB cable
- Motor Driver L298N
- DC motors
- breadboard
- wires
- power source
Setup
#define PIN_IN1 32 // ESP32 pin GPIO32 connected to the IN1 pin L298N
#define PIN_IN2 33 // ESP32 pin GPIO33 connected to the IN2 pin L298N
#define PIN_ENA 25 // ESP32 pin GPIO25 connected to the ENA pin L298N
#define PIN_IN3 27 // ESP32 pin GPIO27 connected to the IN3 pin L298N
#define PIN_IN4 14 // ESP32 pin GPIO14 connected to the IN4 pin L298N
#define PIN_ENB 26 // ESP32 pin GPIO26 connected to the ENB pin L298N
int ref_max = 100;
int ref_min = 0;
int ref_del = 1000;
int ref_tem = 5;
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
// initialize digital pins as outputs.
pinMode(PIN_IN1, OUTPUT);
delay(ref_tem); //
pinMode(PIN_IN2, OUTPUT);
delay(ref_tem); //
pinMode(PIN_ENA, OUTPUT);
delay(ref_tem); //
pinMode(PIN_IN3, OUTPUT);
delay(ref_tem); //
pinMode(PIN_IN4, OUTPUT);
delay(ref_tem); //
pinMode(PIN_ENB, OUTPUT);
delay(ref_tem); //
Serial.println("setup done");
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(PIN_IN1, HIGH); // control the motor's direction in clockwise
delay(ref_tem); //
digitalWrite(PIN_IN2, LOW); // control the motor's direction in clockwise
delay(ref_tem); //
digitalWrite(PIN_IN3, HIGH); // control the motor's direction in clockwise
delay(ref_tem); //
digitalWrite(PIN_IN4, LOW); // control the motor's direction in clockwise
delay(ref_tem); //
Serial.println("clockwise");
Serial.println("A");
for (int speed = ref_min; speed <= ref_max; speed++) {
analogWrite(PIN_ENA, speed); // speed up
delay(ref_tem);
}
delay(ref_del); // rotate at maximum speed
for (int speed = ref_max; speed >= ref_min; speed--) {
analogWrite(PIN_ENA, speed); // speed down
delay(ref_tem);
}
delay(ref_del); //
Serial.println("B");
for (int speed = ref_min; speed <= ref_max; speed++) {
analogWrite(PIN_ENB, speed); // speed up
delay(ref_tem);
}
delay(ref_del); // rotate at maximum speed
for (int speed = ref_max; speed >= ref_min; speed--) {
analogWrite(PIN_ENB, speed); // speed down
delay(ref_tem);
}
delay(ref_del); //
// change direction
digitalWrite(PIN_IN1, LOW); // control the motor's direction in anti-clockwise
delay(ref_tem); //
digitalWrite(PIN_IN2, HIGH); // control the motor's direction in anti-clockwise
delay(ref_tem); //
// change direction
digitalWrite(PIN_IN3, LOW); // control the motor's direction in anti-clockwise
delay(ref_tem); //
digitalWrite(PIN_IN4, HIGH); // control the motor's direction in anti-clockwise
delay(ref_tem); //
Serial.println("anti-clockwise");
Serial.println("A");
for (int speed = ref_min; speed <= ref_max; speed++) {
analogWrite(PIN_ENA, speed); // speed up
delay(ref_tem);
}
delay(ref_del); // rotate at maximum speed
for (int speed = ref_max; speed >= ref_min; speed--) {
analogWrite(PIN_ENA, speed); // speed down
delay(ref_tem);
}
delay(ref_del); // rotate at maximum speed
Serial.println("B");
for (int speed = ref_min; speed <= ref_max; speed++) {
analogWrite(PIN_ENB, speed); // speed up
delay(ref_tem);
}
delay(ref_del); // rotate at maximum speed
for (int speed = ref_max; speed >= ref_min; speed--) {
analogWrite(PIN_ENB, speed); // speed down
delay(ref_tem);
}
Serial.println("loop");
delay(ref_del); // stop motor
}
Example
Comments
Post a Comment