Raspberry Pi: DC Motor Control

So this is another simple project with the Raspberry Pi where I’ve controlled the direction of rotation of DC Motor.

Check out other Raspberry Pi Projects here: (LINK)

Demo:

You can’t really see the direction of rotation here!! Sorry!

Things Required:

  1. Raspberry Pi (I’ve used Raspberry Pi 3)
  2. Keyboard
  3. Mouse
  4. HDMI Screen
  5. DC Motor
  6. External Power Supply
  7. Breadboard
  8. Jumper wire

Reference Material:

  1. Link to article that I followed: (LINK)

Code:

Note that the following code is in python!

View on/Download from Github: (LINK)

import RPi.GPIO as GPIO
from time import sleep

Motor1A = 24
Motor1B = 23
Motor1E = 25

def setup():
    GPIO.cleanup()
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(Motor1A, GPIO.OUT)
    GPIO.setup(Motor1B, GPIO.OUT)
    GPIO.setup(Motor1E, GPIO.OUT)

def loop():
    #forwards
    GPIO.output(Motor1A, GPIO.HIGH)
    GPIO.output(Motor1B, GPIO.LOW)
    GPIO.output(Motor1E, GPIO.HIGH)
    sleep(5)
    #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():
    print("Destroyed")
    GPIO.cleanup()

if __name__ == '__main__':
    setup()
    try:
        while True:
            loop()
    except KeyboardInterrupt:
            destroy()

Stay Tuned for upcoming posts on Raspberry Pi Projects! Want to be the first to know when a new and amazing post comes up?? Then feel free to subscribe!

Happy Learning!!

This image has an empty alt attribute; its file name is stay-tuned.gif

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close