Skip to main content

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

ComponentQuantityDescription
Raspberry Pi 3 B+1Main Controller
L298N Motor Driver1To control the DC motor
DC Motor1The motor to be controlled
Power Supply (12V)1External power for motor
Jumper WiresAs neededTo connect components
Breadboard1For connections (optional)

ЁЯФМ Circuit Connections

L298N PinConnects To
12V12V Power Supply
GNDRaspberry Pi GND & Power Supply GND
5VNot Connected (Raspberry Pi provides 3.3V)
IN1Raspberry Pi GPIO 17 (BCM)
IN2Raspberry Pi GPIO 18 (BCM)
ENARaspberry Pi GPIO 22 (BCM)
OUT1 & OUT2DC 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

python
import 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

  1. 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).

  2. Motor Control:

    • Motor Forward: IN1 = HIGH, IN2 = LOW

    • Motor Backward: IN1 = LOW, IN2 = HIGH

    • Motor Stop: IN1 = LOW, IN2 = LOW

  3. 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 рдоोрдЯрд░ рдб्рд░ाрдпрд╡्рд╣рд░1DC рдоोрдЯрд░ рдХंрдЯ्рд░ोрд▓ рдХрд░рдг्рдпाрд╕ाрдаी
DC рдоोрдЯрд░1рдлिрд░рдгाрд░ी рдоोрдЯрд░
12V рдкॉрд╡рд░ рд╕рдк्рд▓ाрдп1рдоोрдЯрд░рд╕ाрдаी рд╡ीрдЬрдкुрд░рд╡рдаा
рдЬрдо्рдкрд░ рд╡ाрдпрд░्рд╕рдЧрд░рдЬेрдк्рд░рдоाрдгेрдХрдиेрдХ्рд╢рдирд╕ाрдаी
рдм्рд░ेрдбрдмोрд░्рдб1рд╕рд░्рдХिрдЯ рдХрдиेрдХ्рд╢рдирд╕ाрдаी (рдкрд░्рдпाрдпी)

ЁЯФМ рд╕рд░्рдХिрдЯ рдХрдиेрдХ्рд╢рди

L298N рдкिрдирдЬोрдбрдгी
12V12V рдкॉрд╡рд░ рд╕рдк्рд▓ाрдп
GNDрд░ाрд╕्рдкрдмेрд░ी рдкाрдИ GND рдЖрдгि рдкॉрд╡рд░ рд╕рдк्рд▓ाрдп GND
5VрдХाрд╣ीрд╣ी рдиाрд╣ी (рд░ाрд╕्рдкрдмेрд░ी рдкाрдИ 3.3V рд╡ाрдкрд░рддे)
IN1рд░ाрд╕्рдкрдмेрд░ी рдкाрдИ GPIO 17 (BCM)
IN2рд░ाрд╕्рдкрдмेрд░ी рдкाрдИ GPIO 18 (BCM)
ENAрд░ाрд╕्рдкрдмेрд░ी рдкाрдИ GPIO 22 (BCM)
OUT1 рдЖрдгि OUT2DC рдоोрдЯрд░ рдЬोрдбрдгी

ЁЯФ┤ рдЯीрдк: L298N рдоॉрдб्рдпूрд▓рд▓ा рдмाрд╣्рдп 12V рд╡ीрдЬрдкुрд░рд╡рдаा рдЖрд╡рд╢्рдпрдХ рдЖрд╣े. рдоोрдЯрд░рд▓ा рд░ाрд╕्рдкрдмेрд░ी рдкाрдИ рдеेрдЯ рдкॉрд╡рд░ рджेрдК рд╢рдХрдд рдиाрд╣ी.

ЁЯУ╖ рд╕рд░्рдХिрдЯ рдбाрдпрдЧ्рд░ाрдо

(рдЗрдеे рд░ाрд╕्рдкрдмेрд░ी рдкाрдИ, L298N, рдЖрдгि DC рдоोрдЯрд░рдЪे рдЬोрдбрдгी рдбाрдпрдЧ्рд░ाрдо рджाрдЦрд╡ा.)


ЁЯТ╗ рдоोрдЯрд░ рдХंрдЯ्рд░ोрд▓рд╕ाрдаी Python рдХोрдб

(рд╡рд░ीрд▓ рдЗंрдЧ्рд░рдЬी рдХोрдб рдорд░ाрдаीрдд рд╡рд░्рдгрдиाрд╕рд╣ рд╡ाрдкрд░ा.)


ЁЯОп рдиिрд╖्рдХрд░्рд╖

L298N рдоोрдЯрд░ рдб्рд░ाрдпрд╡्рд╣рд░ рд╡ाрдкрд░ूрди рд░ाрд╕्рдкрдмेрд░ी рдкाрдИ 3 B+ рд╕ोрдмрдд DC рдоोрдЯрд░ рдиिрдпंрдд्рд░िрдд рдХрд░рддा рдпेрддे.
PWM рд╡ाрдкрд░ूрди рд╕्рдкीрдб рдХंрдЯ्рд░ोрд▓ рдХрд░рддा рдпेрддो.
✅ рд╣े рд░ोрдмोрдЯिрдХ्рд╕ рдЖрдгि рдСрдЯोрдоेрд╢рди рдк्рд░ोрдЬेрдХ्рдЯрд╕ाрдаी рдЙрдкрдпुрдХ्рдд рдЖрд╣े.

ЁЯЪА рддुрдордЪ्рдпा рдк्рд░ोрдЬेрдХ्рдЯрдордз्рдпे рдпाрдЪा рд╡ाрдкрд░ рдХрд░ूрди рдмрдШा рдЖрдгि рдЕрдиुрднрд╡ рд╢ेрдЕрд░ рдХрд░ा! ЁЯЫа️⚡


рддुрдо्рд╣ाрд▓ा рд╣ा рдм्рд▓ॉрдЧ рдХрд╕ा рд╡ाрдЯрд▓ा? рдХрдоेंрдЯ рдХрд░ा! ЁЯТмЁЯСЗ

Comments

Popular posts from this blog

TOUCH PLATE BASED DOOR BELL

Title:  Touch plate based door bell  Circuit:  Components: IC 555 Resistors: 1 M, 100k, 330 ohms Transistor: BC547  PN2222A Capacitor: 10n 1 Copper plate : as touch plate. A 6v battery An LED / Ic UM66 Description: This is the simple circuit for touch plate based security system. In this project what basically done is, circuit detects stray voltages produced by mains voltage and electrostatic built  up in the room.If sufficient static voltage is detected by the plate then chip will charge up. Transistor BC 547 or PN2222A is used basically to increase the sensitivity.In place of led just connect IC um 66(melody IC). Applications: In homes, of course. This can be specially used in places like hospitals, when patients need to call doctor by himself.

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️

Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ Interfacing Load Cell with Raspberry Pi 3 (via HX711) ⚖️ A load cell is a transducer that converts force (weight) into an electrical signal. The HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales. Today we’ll connect a load cell to Raspberry Pi 3 using the HX711 module. ЁЯзк ЁЯФз Components Required Component Quantity Raspberry Pi 3 1 Load Cell 1 HX711 Module 1 Jumper Wires 6 Breadboard (optional) 1 ЁЯФМ Pin Connections HX711 Pin Raspberry Pi Pin Pin Number VCC 5V Pin 2 GND Ground Pin 6 DT GPIO 5 Pin 29 SCK GPIO 6 Pin 31 Figure: Load Cell connected to Raspberry Pi 3 via HX711 ЁЯТ╗ Python Code from hx711 import HX711 import RPi.GPIO as GPIO import time hx = HX711(dout_pin=5, pd_sck_pin=6) hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(1) hx.reset()...

Interfacing Sound Sensor with Raspberry Pi 3

ЁЯФ╣ Overview The KY-037 is a high-sensitivity sound detection sensor that can detect noise levels in the environment. It provides both analog and digital outputs. In this tutorial, we’ll interface the digital output of KY-037 with Raspberry Pi 3 Model B+ (without using an ADC like MCP3008) and detect sound events.