-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCMotor.py
49 lines (38 loc) · 1018 Bytes
/
DCMotor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import RPi.GPIO as GPIO
import time
# Set pin values
ena = 40
in1 = 38
in2 = 36
# Board and pin setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ena, GPIO.OUT)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
# Set all pins low to start to prevent rotation on run
GPIO.output(ena, GPIO.LOW)
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.LOW)
# Create PWM instance on enable pin A at 50 Hz
motor1 = GPIO.PWM(ena, 50)
try:
while True:
# Set in1 high for counter-clockwise rotation
GPIO.output(in1, GPIO.HIGH)
# Start motor with 25% duty cycle
motor1.start(25)
time.sleep(5)
# Set in1 low and in2 high for clockwise rotation
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.HIGH)
# Change to 100% duty cycle
motor1.ChangeDutyCycle(100)
time.sleep(10)
# Stop motor
motor1.stop()
time.sleep(5)
except KeyboardInterrupt:
# Stop motor
motor1.stop()
# Cleanup pins
GPIO.cleanup()