Interfacing Raspberry Pi 3 B+ with DC Motor using L298N ๐๐ง
Controlling a DC motor with a Raspberry Pi 3 B+ is an essential project for robotics and automation. In this tutorial, we will interface a DC motor with Raspberry Pi 3 B+ using the L298N motor driver. Let’s get started! ๐⚡
๐ Components Required
Component | Quantity | Description |
---|---|---|
Raspberry Pi 3 B+ | 1 | Main Controller |
L298N Motor Driver | 1 | To control the DC motor |
DC Motor | 1 | The motor to be controlled |
Power Supply (12V) | 1 | External power for motor |
Jumper Wires | As needed | To connect components |
Breadboard | 1 | For connections (optional) |
๐ Circuit Connections
L298N Pin | Connects To |
---|---|
12V | 12V Power Supply |
GND | Raspberry Pi GND & Power Supply GND |
5V | Not Connected (Raspberry Pi provides 3.3V) |
IN1 | Raspberry Pi GPIO 17 (BCM) |
IN2 | Raspberry Pi GPIO 18 (BCM) |
ENA | Raspberry Pi GPIO 22 (BCM) |
OUT1 & OUT2 | DC Motor Terminals |
๐ด Note: The L298N module needs an external power supply (12V) to drive the DC motor efficiently. The Raspberry Pi should not power the motor directly.
๐ท Circuit Diagram
(Attach a circuit diagram showing Raspberry Pi, L298N, and DC motor connections.)
๐ป Python Code for Motor Control
pythonimport RPi.GPIO as GPIO
import time
# Pin configuration
IN1 = 17 # GPIO 17
IN2 = 18 # GPIO 18
ENA = 22 # GPIO 22 (Enable Pin)
# Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
# PWM setup
pwm = GPIO.PWM(ENA, 1000) # 1kHz frequency
pwm.start(50) # 50% duty cycle
# Function to move motor forward
def motor_forward():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
print("Motor moving forward")
# Function to move motor backward
def motor_backward():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
print("Motor moving backward")
# Function to stop motor
def motor_stop():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
print("Motor stopped")
# Test motor
motor_forward()
time.sleep(2)
motor_backward()
time.sleep(2)
motor_stop()
# Cleanup
GPIO.cleanup()
๐ Working Explanation
-
GPIO Setup: The Raspberry Pi controls the motor via the L298N module using GPIO 17 (IN1), GPIO 18 (IN2), and GPIO 22 (ENA - PWM speed control).
-
Motor Control:
-
Motor Forward: IN1 = HIGH, IN2 = LOW
-
Motor Backward: IN1 = LOW, IN2 = HIGH
-
Motor Stop: IN1 = LOW, IN2 = LOW
-
-
PWM Speed Control: The ENA pin is controlled using PWM (Pulse Width Modulation) to adjust the motor speed.
๐ ️ Troubleshooting Tips
๐น Motor not running? Check power connections and GPIO setup.
๐น Motor moves in the wrong direction? Swap IN1 and IN2 connections.
๐น PWM not working? Ensure correct frequency and duty cycle settings.
๐ เคฐाเคธ्เคชเคฌेเคฐी เคชाเค 3 B+ เคธोเคฌเคค DC เคฎोเคเคฐ เคเคฃि L298N เคंเคเคฐเคซेเคธिंเค
เคฐाเคธ्เคชเคฌेเคฐी เคชाเค 3 B+ เคเคฃि L298N เคฎोเคเคฐ เคก्เคฐाเคฏเคต्เคนเคฐ เคตाเคชเคฐूเคจ DC เคฎोเคเคฐ เคंเค्เคฐोเคฒ เคเคฐเคฃे เคถिเคूเคฏा! ๐⚡
๐ ️ เคเคตเคถ्เคฏเค เคธाเคนिเคค्เคฏ
เคเคเค | เคช्เคฐเคฎाเคฃ | เคตเคฐ्เคฃเคจ |
---|---|---|
เคฐाเคธ्เคชเคฌेเคฐी เคชाเค 3 B+ | 1 | เคฎुเค्เคฏ เคंเค्เคฐोเคฒเคฐ |
L298N เคฎोเคเคฐ เคก्เคฐाเคฏเคต्เคนเคฐ | 1 | DC เคฎोเคเคฐ เคंเค्เคฐोเคฒ เคเคฐเคฃ्เคฏाเคธाเค ी |
DC เคฎोเคเคฐ | 1 | เคซिเคฐเคฃाเคฐी เคฎोเคเคฐ |
12V เคชॉเคตเคฐ เคธเคช्เคฒाเคฏ | 1 | เคฎोเคเคฐเคธाเค ी เคตीเคเคชुเคฐเคตเค ा |
เคเคฎ्เคชเคฐ เคตाเคฏเคฐ्เคธ | เคเคฐเคेเคช्เคฐเคฎाเคฃे | เคเคจेเค्เคถเคจเคธाเค ी |
เคฌ्เคฐेเคกเคฌोเคฐ्เคก | 1 | เคธเคฐ्เคिเค เคเคจेเค्เคถเคจเคธाเค ी (เคชเคฐ्เคฏाเคฏी) |
๐ เคธเคฐ्เคिเค เคเคจेเค्เคถเคจ
L298N เคชिเคจ | เคोเคกเคฃी |
---|---|
12V | 12V เคชॉเคตเคฐ เคธเคช्เคฒाเคฏ |
GND | เคฐाเคธ्เคชเคฌेเคฐी เคชाเค GND เคเคฃि เคชॉเคตเคฐ เคธเคช्เคฒाเคฏ GND |
5V | เคाเคนीเคนी เคจाเคนी (เคฐाเคธ्เคชเคฌेเคฐी เคชाเค 3.3V เคตाเคชเคฐเคคे) |
IN1 | เคฐाเคธ्เคชเคฌेเคฐी เคชाเค GPIO 17 (BCM) |
IN2 | เคฐाเคธ्เคชเคฌेเคฐी เคชाเค GPIO 18 (BCM) |
ENA | เคฐाเคธ्เคชเคฌेเคฐी เคชाเค GPIO 22 (BCM) |
OUT1 เคเคฃि OUT2 | DC เคฎोเคเคฐ เคोเคกเคฃी |
๐ด เคीเคช: L298N เคฎॉเคก्เคฏूเคฒเคฒा เคฌाเคน्เคฏ 12V เคตीเคเคชुเคฐเคตเค ा เคเคตเคถ्เคฏเค เคเคนे. เคฎोเคเคฐเคฒा เคฐाเคธ्เคชเคฌेเคฐी เคชाเค เคฅेเค เคชॉเคตเคฐ เคฆेเค เคถเคเคค เคจाเคนी.
๐ท เคธเคฐ्เคिเค เคกाเคฏเค्เคฐाเคฎ
(เคเคฅे เคฐाเคธ्เคชเคฌेเคฐी เคชाเค, L298N, เคเคฃि DC เคฎोเคเคฐเคे เคोเคกเคฃी เคกाเคฏเค्เคฐाเคฎ เคฆाเคเคตा.)
๐ป เคฎोเคเคฐ เคंเค्เคฐोเคฒเคธाเค ी Python เคोเคก
(เคตเคฐीเคฒ เคंเค्เคฐเคी เคोเคก เคฎเคฐाเค ीเคค เคตเคฐ्เคฃเคจाเคธเคน เคตाเคชเคฐा.)
๐ฏ เคจिเคท्เคเคฐ्เคท
✅ L298N เคฎोเคเคฐ เคก्เคฐाเคฏเคต्เคนเคฐ เคตाเคชเคฐूเคจ เคฐाเคธ्เคชเคฌेเคฐी เคชाเค 3 B+ เคธोเคฌเคค DC เคฎोเคเคฐ เคจिเคฏंเคค्เคฐिเคค เคเคฐเคคा เคฏेเคคे.
✅ PWM เคตाเคชเคฐूเคจ เคธ्เคชीเคก เคंเค्เคฐोเคฒ เคเคฐเคคा เคฏेเคคो.
✅ เคนे เคฐोเคฌोเคिเค्เคธ เคเคฃि เคเคोเคฎेเคถเคจ เคช्เคฐोเคेเค्เคเคธाเค ी เคเคชเคฏुเค्เคค เคเคนे.
๐ เคคुเคฎเค्เคฏा เคช्เคฐोเคेเค्เคเคฎเคง्เคฏे เคฏाเคा เคตाเคชเคฐ เคเคฐूเคจ เคฌเคा เคเคฃि เค เคจुเคญเคต เคถेเค เคฐ เคเคฐा! ๐ ️⚡
เคคुเคฎ्เคนाเคฒा เคนा เคฌ्เคฒॉเค เคเคธा เคตाเคเคฒा? เคเคฎेंเค เคเคฐा! ๐ฌ๐
- Get link
- X
- Other Apps
Labels
ELECTRONICS PROJECTS- Get link
- X
- Other Apps
Comments