Controlling a DC Motor with Raspberry Pi: GPIO + L298N H-Bridge
Learn how to control a DC motor using Raspberry Pi and L298N motor driver. Build robotics, automation, and mechanical projects by interfacing motors using Python.
1. Introduction to DC Motor Control
A DC motor converts electrical energy into mechanical motion. When interfaced with Raspberry Pi, it can be used to build moving robots, fans, doors, or any rotating parts.
Why Use Raspberry Pi to Control a DC Motor?
- Automate physical systems like doors and fans
- Essential for robotics and IoT projects
- Control motor speed and direction using Python
2. Components and Tools You’ll Need
Required:
- Raspberry Pi (any model with GPIO)
- L298N Motor Driver Module
- DC Motor (3V–12V)
- 12V Power Supply or Battery
- Breadboard and jumper wires
Optional but Helpful:
- PWM Fan or geared motor
- Heat sink for L298N (if used continuously)
- Switches for manual override
3. Understanding DC Motor and L298N Module
The L298N module is an H-Bridge driver that allows control of motor direction and speed via PWM. It can drive two motors simultaneously.
DC Motor Characteristics:
- Rotates when voltage is applied
- Direction depends on polarity
- Speed controlled using PWM
L298N Pins Explained:
- IN1 & IN2 – Control motor direction
- ENA – Controls speed via PWM
- 12V – Motor power
- 5V – Logic voltage (jumper used)
- OUT1 & OUT2 – Motor terminals
4. Circuit Diagram and Wiring
Connect the Raspberry Pi GPIO to L298N:
- IN1 → GPIO 17 (Pin 11)
- IN2 → GPIO 27 (Pin 13)
- ENA → GPIO 22 (Pin 15)
- OUT1 & OUT2 → Motor terminals
- 12V → External motor power
- GND of L298N → GND of Raspberry Pi
5. Installing Required Libraries
Set up GPIO control:
sudo apt-get update
sudo apt-get install python3-gpiozero
sudo pip3 install RPi.GPIO
6. Python Code to Run DC Motor
- Create a file: nano motor_control.py
- Paste the following code:
import RPi.GPIO as GPIO
import time
# Pin setup
IN1 = 17
IN2 = 27
ENA = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
# Set up PWM
pwm = GPIO.PWM(ENA, 100)
pwm.start(0)
def forward():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
pwm.ChangeDutyCycle(80)
def reverse():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
pwm.ChangeDutyCycle(80)
try:
print("Forward rotation")
forward()
time.sleep(3)
print("Reverse rotation")
reverse()
time.sleep(3)
except KeyboardInterrupt:
pass
finally:
pwm.stop()
GPIO.cleanup()
Run Command: python3 motor_control.py
Motor rotates forward and then in reverse direction.
7. Troubleshooting Tips
Common Issues and Fixes:
- Motor not moving? Check 12V supply.
- Wrong direction? Swap OUT1 and OUT2 or IN1/IN2 logic.
- Motor too slow? Increase PWM duty cycle or check voltage.
8. Cool Projects You Can Build
1. Line Following Robot
Combine motor control with IR sensors for intelligent robots.
2. Smart Blinds or Curtains
Use a DC motor to open or close curtains based on time or light.
3. Mini Conveyor Belt
Create a small assembly-line system for automation experiments.
9. Bonus: Speed Control with Potentiometer
Read analog input from ADC and convert to PWM:
Note: Use MCP3008 to read potentiometer value and adjust PWM in real time
10. Optimization Tips
- Use gear motors for torque-heavy tasks
- Add encoder for precise motion control
- Use PWM for smoother speed variations
- Use Python threading for multitasking
11. FAQs: Raspberry Pi with DC Motor
Q: Can I connect DC motor directly to Raspberry Pi?
A: No, Raspberry Pi cannot provide enough current. Use motor driver like L298N.
Q: What is ENA pin used for?
A: ENA controls motor speed via PWM signal from Raspberry Pi.
Q: How to power motor separately?
A: Connect external 12V to L298N's VCC, and connect GNDs together.
12. Conclusion
- You can control motor speed and direction with Pi
- Use L298N for safe driving of motors
- Python and GPIO let you automate movement