Skip to content

Commit 3ec73fc

Browse files
authored
Merge pull request #52 from jedgarpark/pico-dc-motor-example
added h-bridge dc motor example
2 parents 2251bfc + 638fe6c commit 3ec73fc

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

examples/motor_h-bridge_dc_motor.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# This example uses an L9110 H-bridge driver to run a DC Motor using two PWM pins.
5+
# https://www.adafruit.com/product/4489
6+
7+
# Hardware setup:
8+
# DC motor via L9110 H-bridge driver on two PWM pins that are on their own channels
9+
# e.g., RP2040 Pico pins GP28, GP27
10+
11+
import time
12+
import board
13+
import pwmio
14+
from adafruit_motor import motor
15+
16+
PWM_PIN_A = board.GP28 # pick any pwm pins on their own channels
17+
PWM_PIN_B = board.GP27
18+
19+
# DC motor setup
20+
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
21+
# cases. A capacitor can be used to help prevent this.
22+
pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50)
23+
pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50)
24+
motor1 = motor.DCMotor(pwm_a, pwm_b)
25+
26+
print("***DC motor test***")
27+
28+
print("\nForwards slow")
29+
motor1.throttle = 0.5
30+
print(" throttle:", motor1.throttle)
31+
time.sleep(1)
32+
33+
print("\nStop")
34+
motor1.throttle = 0
35+
print(" throttle:", motor1.throttle)
36+
time.sleep(1)
37+
38+
print("\nForwards")
39+
motor1.throttle = 1.0
40+
print(" throttle:", motor1.throttle)
41+
time.sleep(1)
42+
43+
print("\nStop")
44+
motor1.throttle = 0
45+
print("throttle:", motor1.throttle)
46+
time.sleep(1)
47+
48+
print("\nBackwards")
49+
motor1.throttle = -1.0
50+
print(" throttle:", motor1.throttle)
51+
time.sleep(1)
52+
53+
print("\nStop")
54+
motor1.throttle = 0
55+
print("throttle:", motor1.throttle)
56+
time.sleep(1)
57+
58+
print("\nBackwards slow")
59+
motor1.throttle = -0.5
60+
print(" throttle:", motor1.throttle)
61+
time.sleep(1)
62+
63+
print("\nStop")
64+
motor1.throttle = 0
65+
print(" throttle:", motor1.throttle)
66+
time.sleep(1)
67+
68+
print("\nSpin freely")
69+
motor1.throttle = None
70+
print(" throttle:", motor1.throttle)
71+
72+
print("\n***Motor test is complete***")

0 commit comments

Comments
 (0)