💡 Interfacing an LED with Raspberry Pi 3 – A Beginner’s Guide
🚀 Introduction
Raspberry Pi 3 B+ is a versatile mini-computer that enables us to control various electronic components. One of the most exciting applications is controlling a DC motor using an L298N motor driver. This project will introduce you to motor control, PWM (Pulse Width Modulation), and GPIO programming using Python. Let’s get started! ⚡🔧
🔧 Components Required
To complete this project, you will need:
- 🖥️ Raspberry Pi 3 B+
- ⚙️ L298N Motor Driver Module
- 🔩 DC Motor
- 🔋 12V Power Supply
- 🔌 Jumper Wires
🛠️ Circuit Diagram & Connections
To interface the DC motor with the Raspberry Pi 3 B+, follow these connections:
L298N Pin | Connects To |
---|---|
12V | External Power Supply (12V) |
GND | Raspberry Pi GND & Power Supply GND |
IN1 | Raspberry Pi GPIO 17 (BCM) |
IN2 | Raspberry Pi GPIO 18 (BCM) |
ENA | Raspberry Pi GPIO 22 (PWM for speed control) |
OUT1 & OUT2 | DC Motor Terminals |
💡 Note: The L298N module requires an external 12V power supply to run the motor efficiently. The Raspberry Pi should NOT power the motor directly.
💻 Writing the Python Code
To control the DC motor, we use Python and the RPi.GPIO library.
import RPi.GPIO as GPIO
import time
# 🎯 Define GPIO pins
IN1 = 17 # L298N IN1
IN2 = 18 # L298N IN2
ENA = 22 # L298N Enable (PWM)
# 🔧 GPIO Setup
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
# 🎚️ PWM Setup (1kHz Frequency)
pwm = GPIO.PWM(ENA, 1000)
pwm.start(50) # Set speed to 50%
# 🚀 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")
# 🔬 Testing the Motor
print("🚀 Starting Motor Test...")
motor_forward() # Move Forward
time.sleep(2) # Wait 2 Seconds
motor_backward() # Move Backward
time.sleep(2) # Wait 2 Seconds
motor_stop() # Stop Motor
print("✅ Motor Test Completed!")
# 🛑 Cleanup GPIO
GPIO.cleanup()
💡 This script makes the DC motor move forward, backward, and stop.
🚀 Running the Code
To execute the Python script, follow these steps:
1️⃣ Save the code as dc_motor.py
.
2️⃣ Open a terminal and navigate to the script’s location.
3️⃣ Run the script using the following command:
python3 dc_motor.py
4️⃣ Your DC motor should start running! ⚙️✨
📖 Code Explanation
- 🛠️
GPIO.setmode(GPIO.BCM)
: Sets up the GPIO pins using BCM numbering. - ⚙️
GPIO.setup(IN1, GPIO.OUT) & GPIO.setup(IN2, GPIO.OUT)
: Configures the motor control pins as outputs. - 🔼
GPIO.output(IN1, GPIO.HIGH) & GPIO.output(IN2, GPIO.LOW)
: Moves the motor forward. - 🔽
GPIO.output(IN1, GPIO.LOW) & GPIO.output(IN2, GPIO.HIGH)
: Moves the motor backward. - 🎚️
pwm = GPIO.PWM(ENA, 1000)
: Sets up PWM (Pulse Width Modulation) to control speed. - ⏹️
motor_stop()
: Stops the motor by setting both IN1 and IN2 LOW.
🛑 Stopping the Program
To stop the motor control program, press CTRL+C in the terminal. Then, clean up the GPIO settings using:
GPIO.cleanup()
💡 This prevents issues when running future GPIO-based programs.
🎯 Conclusion
🎉 Congratulations! You have successfully interfaced a DC motor with Raspberry Pi 3 B+ using the L298N motor driver. 🚗 This project introduces you to motor control, GPIO programming, and PWM speed control.
🚀 Now try modifying the PWM speed and experimenting with different motor control functions!
🌍 मराठीत मार्गदर्शक: रास्पबेरी पाय 3 B+ सह DC मोटर जोडणी
🔥 परिचय
रास्पबेरी पाय 3 B+ हा छोटा पण प्रभावी संगणक आहे, जो विविध इलेक्ट्रॉनिक उपकरणे नियंत्रित करण्यासाठी वापरला जातो. DC मोटर नियंत्रित करण्यासाठी आपण L298N मोटर ड्रायव्हर वापरणार आहोत. 🚗⚙️
🛠️ आवश्यक घटक
- 🖥️ रास्पबेरी पाय 3 B+
- ⚙️ L298N मोटर ड्रायव्हर
- 🔩 DC मोटर
- 🔋 12V पॉवर सप्लाय
- 🔌 जम्पर वायर
📌 सर्किट जोडणी
L298N पिन | जोडणी |
---|---|
12V | 12V पॉवर सप्लाय |
GND | ग्राउंड (GND) |
IN1 | GPIO 17 (BCM) |
IN2 | GPIO 18 (BCM) |
ENA | GPIO 22 (PWM कंट्रोल) |
OUT1 & OUT2 | DC मोटर जोडणी |
💡 टीप: मोटरला योग्य वीजपुरवठा (12V) आवश्यक आहे.
💻 पायथॉन कोड
import RPi.GPIO as GPIO
import time
IN1 = 17
IN2 = 18
ENA = 22
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
pwm = GPIO.PWM(ENA, 1000)
pwm.start(50)
def motor_forward():
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
print("🚗 मोटर पुढे फिरत आहे")
def motor_backward():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
print("🔙 मोटर मागे फिरत आहे")
def motor_stop():
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
print("⏹️ मोटर बंद झाली")
motor_forward()
time.sleep(2)
motor_backward()
time.sleep(2)
motor_stop()
GPIO.cleanup()
🎯 निष्कर्ष
अभिनंदन! 🎉 तुम्ही यशस्वीरित्या रास्पबेरी पाय 3 B+ सह DC मोटर नियंत्रित केली आहे. 🚀 PWM वापरून वेग नियंत्रित करू शकतो आणि वेगवेगळ्या दिशा बदलू शकतो.
💡 आता नवीन फंक्शन्स आणि वेग सेटिंग्स ट्राय करून बघा! ⚡
Comments