Controlling a DC Motor with Raspberry Pi4
Introduction
What is motor Driver : A Motor Driver is a special circuit or IC that provides the necessary power (or rather the current) to the motor for smooth and safe operation.
we should never connect a motor directly to Raspberry Pi
Circuit Diagram
![]() |
Controlling a DC Motor with Raspberry Pi4 |
From this Datasheet This dual bidirectional motor driver, is based on the very popular L298 Dual H-Bridge Motor Driver Integrated Circuit. The circuit will allow you to easily and independently control two motors of up to 2A each in both directions.It is ideal for robotic applications and well suited for connection to a microcontroller requiring just a couple of control lines per motor. It can also be interfaced with simple manual switches, TTL logic gates, relays, etc. This board equipped with power LED indicators, on-board +5V regulator and protection diodes.
Brief Data:
• Driver: L298N Dual H Bridge DC Motor Driver
• Power Supply: DC 5 V - 35 V.
• Peak current: 2 Amp
• Operating current range: 0 ~ 36mA
Signal Control signal input voltage range:
• Low: -0.3V ≤ Vin ≤ 1.5V.
• High: 2.3V ≤ Vin ≤ Vss.
Enable signal input voltage range:
o Low: -0.3 ≤ Vin ≤ 1.5V (control signal is invalid).
o High: 2.3V ≤ Vin ≤ Vss (control signal active).
• Maximum power consumption: 20W (when the temperature T = 75 ℃).
• Storage temperature: -25 ℃ ~ +130.
• On-board + 5V regulated output supply (supply to controller board i.e. Arduino).
Ize Size: 3.4cm x 4.3cm x 2.7cm
Components Required
- Raspberry Pi 4 Model B
- L298N Motor Driver Module Lipo Battery 3300mAh
- Connecting wires (Jumper Wires)
- 5V – 2A Power Supply for Raspberry Pi
- 5V DC Motor
Python Program for Controlling a DC Motor with Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
from time import sleep | |
# Pins for Motor Driver Inputs | |
Motor1A = 21 | |
Motor1B = 20 | |
Motor1E = 16 | |
def setup(): | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BCM) # GPIO Numbering | |
GPIO.setup(Motor1A,GPIO.OUT) # All pins as Outputs | |
GPIO.setup(Motor1B,GPIO.OUT) | |
GPIO.setup(Motor1E,GPIO.OUT) | |
def loop(): | |
# Going forwards | |
GPIO.output(Motor1A,GPIO.HIGH) | |
GPIO.output(Motor1B,GPIO.LOW) | |
GPIO.output(Motor1E,GPIO.HIGH) | |
print("Going forwards") | |
sleep(5) | |
# Going backwards | |
GPIO.output(Motor1A,GPIO.LOW) | |
GPIO.output(Motor1B,GPIO.HIGH) | |
GPIO.output(Motor1E,GPIO.HIGH) | |
print("Going backwards") | |
sleep(5) | |
# Stop | |
GPIO.output(Motor1E,GPIO.LOW) | |
GPIO.output(Motor1B,GPIO.LOW) | |
print("Stop") | |
def destroy(): | |
GPIO.cleanup() | |
if __name__ == '__main__': # Program start from here | |
setup() | |
try: | |
loop() | |
except KeyboardInterrupt: | |
destroy() |
Raspberry pi4 Workshop PIR Sensor -Email Sending Movement Detector using IFTTT
No comments