Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
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,354
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
1 project • 28 followers
Hardware Hacker. Love going to hackathons. Enough said!
Contact

Comments

Please log in or sign up to comment.