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