Dc-motor

From aldeid
Jump to navigation Jump to search

L293D motor driver

Pin Function Connect to
VS Supply voltage Bat positive, power supply for motor
VSS Logic Supply Voltage Bat on the RaspberryPi (5V)
GND Ground Ground on RaspberryPi and Battery
Enable 1/2 Enable Outputs GPIO pins on RaspberryPi
Input 1/2/3/4 Logic Inputs GPIO pins on RaspberryPi
Output 1/2/3/4 Power Outputs Connect to motors

Diagram

Code

import RPi.GPIO as GPIO
from time import sleep
 
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

Motor1 = {'EN': 17, 'input1': 27, 'input2': 22}

for x in Motor1:
    GPIO.setup(Motor1[x], GPIO.OUT)

EN1 = GPIO.PWM(Motor1['EN'], 100)    
EN1.start(0)                    

while True:
    for x in range(40, 100):
        print ("FORWARD MOTION")
        EN1.ChangeDutyCycle(x)

        GPIO.output(Motor1['input1'], GPIO.HIGH)
        GPIO.output(Motor1['input2'], GPIO.LOW)
        
        sleep(0.1)
   
    print ("STOP")
    EN1.ChangeDutyCycle(0)

    sleep(5)
     
    for x in range(40, 100):
        print ("BACKWARD MOTION")
        EN1.ChangeDutyCycle(x)
        
        GPIO.output(Motor1['input1'], GPIO.LOW)
        GPIO.output(Motor1['input2'], GPIO.HIGH)

        sleep(0.1)
     
    print ("STOP")
    EN1.ChangeDutyCycle(0)

    sleep(5)

GPIO.cleanup()