Last active
April 23, 2023 15:13
-
-
Save elktros/b0cef5aed52936277eafdaa71eb619fd to your computer and use it in GitHub Desktop.
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 = 24 | |
Motor1B = 23 | |
Motor1E = 25 | |
def setup(): | |
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) | |
sleep(5) | |
# Going backwards | |
GPIO.output(Motor1A,GPIO.LOW) | |
GPIO.output(Motor1B,GPIO.HIGH) | |
GPIO.output(Motor1E,GPIO.HIGH) | |
sleep(5) | |
# Stop | |
GPIO.output(Motor1E,GPIO.LOW) | |
def destroy(): | |
GPIO.cleanup() | |
if __name__ == '__main__': # Program start from here | |
setup() | |
try: | |
loop() | |
except KeyboardInterrupt: | |
destroy() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment