0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use L298N Motor Driver with Raspberry Pi: Simple Guide

To use the L298N motor driver with a Raspberry Pi, connect the motor driver inputs to the Pi's GPIO pins and power the motors separately. Then, control the motor direction and speed by setting the GPIO pins HIGH or LOW and using PWM signals in Python.
📐

Syntax

To control motors with the L298N and Raspberry Pi, you use GPIO pins to set motor direction and PWM to control speed.

  • IN1, IN2: Control motor 1 direction.
  • IN3, IN4: Control motor 2 direction.
  • ENA, ENB: Enable pins for speed control via PWM.
  • GPIO.output(pin, state): Set pin HIGH or LOW to control motor direction.
  • GPIO.PWM(pin, frequency): Create PWM to control motor speed.
python
import RPi.GPIO as GPIO
import time

# Setup GPIO mode
GPIO.setmode(GPIO.BCM)

# Define pins
IN1 = 24
IN2 = 23
ENA = 25

# Setup pins
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)

# Setup PWM on ENA pin at 1000Hz
pwm = GPIO.PWM(ENA, 1000)
pwm.start(0)  # Start PWM with 0% duty cycle

# Control motor direction
GPIO.output(IN1, GPIO.HIGH)  # Motor forward
GPIO.output(IN2, GPIO.LOW)

# Set motor speed
pwm.ChangeDutyCycle(75)  # 75% speed

# Cleanup
GPIO.cleanup()
💻

Example

This example shows how to run a DC motor forward at 75% speed for 5 seconds, then reverse it at 50% speed for 5 seconds using the L298N motor driver and Raspberry Pi.

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

IN1 = 24
IN2 = 23
ENA = 25

GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)

pwm = GPIO.PWM(ENA, 1000)
pwm.start(0)

try:
    # Motor forward
    GPIO.output(IN1, GPIO.HIGH)
    GPIO.output(IN2, GPIO.LOW)
    pwm.ChangeDutyCycle(75)  # 75% speed
    print("Motor running forward at 75% speed")
    time.sleep(5)

    # Motor backward
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.HIGH)
    pwm.ChangeDutyCycle(50)  # 50% speed
    print("Motor running backward at 50% speed")
    time.sleep(5)

finally:
    pwm.stop()
    GPIO.cleanup()
    print("GPIO cleaned up")
Output
Motor running forward at 75% speed Motor running backward at 50% speed GPIO cleaned up
⚠️

Common Pitfalls

  • Incorrect wiring: Not connecting the motor power supply separately can damage the Raspberry Pi.
  • Missing ground connection: The Raspberry Pi and L298N must share a common ground.
  • Wrong GPIO numbering: Use BCM mode consistently to avoid pin confusion.
  • Not enabling EN pins: Without PWM on ENA/ENB, motors won't run or speed control won't work.
  • Forgetting GPIO cleanup: Can cause warnings or unexpected behavior on next run.
python
import RPi.GPIO as GPIO

# Wrong: Using BOARD mode but pins defined as BCM
GPIO.setmode(GPIO.BOARD)
IN1 = 24  # This is BCM pin, mismatch causes errors

# Correct:
GPIO.setmode(GPIO.BCM)
IN1 = 24
📊

Quick Reference

PinPurpose
IN1, IN2Motor 1 direction control
IN3, IN4Motor 2 direction control
ENA, ENBEnable pins for PWM speed control
+12VMotor power supply (separate from Pi)
GNDCommon ground for Pi and motor driver

Key Takeaways

Connect L298N inputs to Raspberry Pi GPIO pins and power motors separately.
Use GPIO.output to set motor direction and PWM on EN pins to control speed.
Always share a common ground between Raspberry Pi and motor driver.
Use BCM pin numbering consistently in your code.
Clean up GPIO pins after running your program to avoid conflicts.