L298n (L298N v0.1.0)
View SourceElixir library for controlling L298N dual H-bridge motor driver.
The L298N is a popular dual H-bridge motor driver IC that can control up to 2 DC motors or 1 stepper motor. This library provides a high-level Elixir interface for controlling motors through GPIO pins using the Pigpiox library.
Features
- Control up to 2 DC motors independently
- PWM speed control (0-100%)
- Direction control (forward/backward)
- Safe motor stopping
- GenServer-based process for state management
Hardware Setup
Connect your L298N module to your Raspberry Pi as follows:
| L298N Pin | Raspberry Pi GPIO | Function |
|---|---|---|
| IN1 | GPIO pin | Motor A direction control |
| IN2 | GPIO pin | Motor A direction control |
| ENA | PWM GPIO pin | Motor A speed control |
| IN3 | GPIO pin | Motor B direction control |
| IN4 | GPIO pin | Motor B direction control |
| ENB | PWM GPIO pin | Motor B speed control |
Usage
First, start the driver with your pin configuration:
{:ok, pid} = L298n.start_link([
motor_a: %{in1: 18, in2: 19, en: 12},
motor_b: %{in1: 20, in2: 21, en: 13}
])Then control your motors:
# Move both motors forward at 50% speed
L298n.set_motors(50, 50)
# Turn left (motor A forward, motor B backward)
L298n.set_motors(30, -30)
# Stop all motors
L298n.stop_all()Motor Speed Values
Motor speed is specified as a percentage from -100 to 100:
- Positive values: Forward direction
- Negative values: Backward direction
- 0: Stop
- ±100: Full speed
Summary
Functions
Gets the current motor speeds.
Sets the speed for motor A only.
Sets the speed for motor B only.
Sets the speed and direction for both motors.
Starts the L298N driver process.
Stops all motors immediately.
Functions
Gets the current motor speeds.
Returns a map with the current speed settings for both motors.
Examples
L298n.get_motor_speeds()
#=> %{motor_a: 50, motor_b: -30}
@spec set_motor_a(integer()) :: :ok
Sets the speed for motor A only.
Parameters
percent- Speed for motor A (-100 to 100)
Examples
L298n.set_motor_a(75) # Motor A forward at 75%
L298n.set_motor_a(-50) # Motor A backward at 50%
L298n.set_motor_a(0) # Stop motor A
@spec set_motor_b(integer()) :: :ok
Sets the speed for motor B only.
Parameters
percent- Speed for motor B (-100 to 100)
Examples
L298n.set_motor_b(75) # Motor B forward at 75%
L298n.set_motor_b(-50) # Motor B backward at 50%
L298n.set_motor_b(0) # Stop motor B
Sets the speed and direction for both motors.
Parameters
motor_a_percent- Speed for motor A (-100 to 100)motor_b_percent- Speed for motor B (-100 to 100)
Speed values:
- Positive: Forward direction
- Negative: Backward direction
- 0: Stop
Examples
# Both motors forward at 50% speed
L298n.set_motors(50, 50)
# Motor A forward, Motor B backward (turn)
L298n.set_motors(30, -30)
# Stop both motors
L298n.set_motors(0, 0)
@spec start_link(keyword()) :: GenServer.on_start()
Starts the L298N driver process.
Parameters
opts- Keyword list with motor pin configurations
Options
:motor_a- Map with keys:in1,:in2,:enfor motor A pins:motor_b- Map with keys:in1,:in2,:enfor motor B pins:name- Process name (optional, defaults toL298n.Driver)
Examples
# Start with both motors configured
{:ok, pid} = L298n.start_link([
motor_a: %{in1: 18, in2: 19, en: 12},
motor_b: %{in1: 20, in2: 21, en: 13}
])
# Start with only motor A
{:ok, pid} = L298n.start_link([
motor_a: %{in1: 18, in2: 19, en: 12}
])
@spec stop_all() :: :ok
Stops all motors immediately.
This is equivalent to calling set_motors(0, 0).
Examples
L298n.stop_all()