Keval Doshi
Published © GPL3+

Control a DC Motor with an L298 Controller and Raspberry Pi

Learn how to control a DC motor with an L298 motor driver and a Raspberry Pi.

IntermediateFull instructions provided2 hours7,297
Control a DC Motor with an L298 Controller and Raspberry Pi

Things used in this project

Story

Read more

Code

Code snippet #1

Plain text
import sys
import time
import RPi.GPIO as GPIO

mode=GPIO.getmode()

GPIO.cleanup()

Forward=26
Backward=20
sleeptime=1

GPIO.setmode(GPIO.BOARD)
GPIO.setup(Forward, GPIO.OUT)
GPIO.setup(Backward, GPIO.OUT)

def forward(x):
    GPIO.output(Forward, GPIO.HIGH)
    print("Moving Forward")
    time.sleep(x)
    GPIO.output(Forward, GPIO.LOW)

def reverse(x):
    GPIO.output(Backward, GPIO.HIGH)
    print("Moving Backward")
    time.sleep(x)
    GPIO.output(Backward, GPIO.LOW)

while (1):
    
    forward(5)

    reverse(5)
GPIO.cleanup()

Credits

Keval Doshi

Keval Doshi

1 project • 28 followers
Hardware Hacker. Love going to hackathons. Enough said!

Comments