1
WiKi
Ghassan Yusuf edited this page 2025-03-23 01:23:42 +03:00
L298N_ESP32 Library Documentation
Table of Contents
Introduction
The L298N_ESP32 library is a dual H-bridge motor driver with ESP32 support. It provides a simple and easy-to-use API for controlling DC motors using the L298N IC.
Installation
- Install the Arduino IDE.
- Install the ESP32 board package.
- Clone or download this repository into your Arduino library folder (
~/.arduino/libraries/L298N_ESP32
). - Import the library in your sketch by adding
#include <L298N_ESP32.h>
.
Functions
Constructor
- Initializes the motor driver with the specified pins and channel.
- Parameters:
in1
: Pin for input 1 ( HIGH for direction).in2
: Pin for input 2 (HIGH for direction).en
: Pin for enable signal (PWM output).channel
: PWM channel number (optional, default: 0).
begin()
- Initializes the motor driver and sets up the pins.
- Sets the PWM frequency to 30kHz and resolution to 8-bit.
setMin() and setMax()
- Sets the minimum and maximum speed values for the motor.
- Parameters:
value
: Minimum/maximum speed value (0-255).
invert()
- Inverts the direction of the motor.
forward() and backward()
- Sets the motor to move forward or backward.
- Note: These functions override any previous speed setting.
goMin() and goMax()
- Sets the motor to run at minimum or maximum speed.
setSpeed() and setPercentage()
- Sets the motor speed using a percentage value (0-100) or an absolute speed value (0-255).
- Note: These functions are equivalent to
setSpeed()
and override any previous setting.
override()
- Manually sets the PWM output value.
- Parameters:
value
: PWM output value (0-255).
stop()
- Stops the motor by setting the speed to 0.
command()
- Provides a serial interface for interacting with the motor driver.
- Returns the last tried value as an unsigned integer.
- Note: This function is not recommended for use in production code.
Example usage:
Here's an example Arduino code that utilizes the L298N_ESP32
library:
#include <L298N_ESP32.h>
// Create an instance of the L298N motor driver
L298N_ESP32 myMotor(2, 3, 4);
void setup() {
Serial.begin(9600);
myMotor.begin();
}
void loop() {
// Set the motor to move forward at maximum speed
myMotor.forward();
delay(2000);
// Set the motor to move backward at maximum speed
myMotor.backward();
delay(2000);
// Set the motor to run at minimum speed
myMotor.goMin();
delay(2000);
// Set the motor to run at 50% of maximum speed
myMotor.setPercentage(50);
delay(2000);
// Manually set the PWM output value to 150
myMotor.override(150);
delay(2000);
// Stop the motor
myMotor.stop();
delay(2000);
}