Skip to main content

motor_l298n

DC motor driver using L298N H-bridge for speed and direction control.

Overview

The motor_l298n node drives DC motors using the L298N dual H-bridge motor driver module. It provides independent speed control via PWM on the enable pin and direction control via the IN1/IN2 logic pins. Supports forward, reverse, coast (stop), and active braking modes for precise motor management in robotics, conveyor systems, and automated vehicles.

46V
Max Motor Voltage
2A
Per Channel
0-100%
Speed Range
2 Ch
Motor Channels

Properties

Property Type Required Default Description
enablePin number Yes 12 PWM enable pin (ENA or ENB on L298N)
in1Pin number Yes 23 Direction control pin IN1
in2Pin number Yes 24 Direction control pin IN2
pwmFrequency number No 1000 PWM frequency in Hz for speed control
invertDirection boolean No false Swap forward/reverse direction mapping
initialSpeed number No 0 Initial speed on deploy (0-100%)

Inputs

msg.payload

Object with speed and direction controls.

{
  "speed": 75,
  "direction": "forward"
}
speed 0-100 (percentage)
direction "forward" | "reverse" | "stop" | "brake"

Outputs

Motor State

Outputs current motor state with pin assignments.

{
  "speed": 75,
  "direction": "forward",
  "enablePin": 12,
  "in1": 1,
  "in2": 0,
  "timestamp": 1709251200000
}

Direction Logic Truth Table

Direction ENA (PWM) IN1 IN2 Result
forward Speed % HIGH LOW Motor spins clockwise
reverse Speed % LOW HIGH Motor spins counter-clockwise
stop 0 LOW LOW Motor coasts to stop (free spin)
brake 100% HIGH HIGH Active braking (rapid stop)

Wiring / Connection Diagram

L298N Motor Driver to Raspberry Pi

  Raspberry Pi                    L298N Module
  +-----------+                 +----------------+
  |           |                 |                |
  |  GPIO 12  |---[PWM]--------| ENA            |     DC Motor A
  |  GPIO 23  |---[Digital]----| IN1        OUT1|------(+)
  |  GPIO 24  |---[Digital]----| IN2        OUT2|------(-)
  |           |                 |                |
  |  GND      |---[Ground]-----| GND            |     DC Motor B
  |           |                 |            OUT3|------(+)
  +-----------+                 | ENB        OUT4|------(-)
                                | IN3            |
   External Power               | IN4            |
   +--------+                   |                |
   | 6-12V  |---[+]------------| +12V           |
   | Supply  |---[-]------------| GND            |
   +--------+                   +----------------+

  Note: Remove 5V jumper if motor supply > 12V
        Keep jumper for onboard 5V regulator

Important: Always use an external power supply for the motor. Never power DC motors directly from the Raspberry Pi. Connect all grounds together.

Example Use Cases

Dashboard Motor Control

Use a slider for speed and buttons for direction from a dashboard.

slider + buttons function motor_l298n
[
  {
    "id": "speed-slider",
    "type": "slider",
    "label": "Motor Speed",
    "min": 0,
    "max": 100
  },
  {
    "id": "merge-controls",
    "type": "function",
    "func": "var speed = flow.get('speed') || 0;
var dir = flow.get('direction') || 'stop';
msg.payload = { speed: speed, direction: dir };
return msg;"
  },
  {
    "id": "motor-a",
    "type": "motor_l298n",
    "enablePin": 12,
    "in1Pin": 23,
    "in2Pin": 24,
    "pwmFrequency": 1000
  }
]

Conveyor Belt with Sensor Stop

Run a conveyor belt and stop when an object is detected at the end.

inject motor_l298n | pir motor_l298n
[
  {
    "id": "start-conveyor",
    "type": "inject",
    "payload": { "speed": 60, "direction": "forward" }
  },
  {
    "id": "end-sensor",
    "type": "pir",
    "pin": 25
  },
  {
    "id": "stop-motor",
    "type": "change",
    "rules": [
      { "t": "set", "p": "payload", "to": { "speed": 0, "direction": "brake" } }
    ]
  },
  {
    "id": "belt-motor",
    "type": "motor_l298n",
    "enablePin": 12,
    "in1Pin": 23,
    "in2Pin": 24
  }
]

MQTT Remote Vehicle Control

Control a two-motor vehicle remotely via MQTT commands.

mqtt-in switch motor_l298n (L) / motor_l298n (R)
[
  {
    "id": "mqtt-control",
    "type": "mqtt-in",
    "topic": "vehicle/drive"
  },
  {
    "id": "left-motor",
    "type": "motor_l298n",
    "enablePin": 12,
    "in1Pin": 23,
    "in2Pin": 24,
    "pwmFrequency": 1000
  },
  {
    "id": "right-motor",
    "type": "motor_l298n",
    "enablePin": 13,
    "in1Pin": 5,
    "in2Pin": 6,
    "pwmFrequency": 1000,
    "invertDirection": true
  }
]

Common Use Cases

Robotics

Wheeled robots and differential drive vehicles

Conveyor Systems

Industrial belt and roller conveyor control

Pump Control

Variable-speed water and fluid pump systems

CNC Machines

Spindle motors and axis drive control

Related Nodes