Skip to content

added h-bridge dc motor example #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions examples/motor_h-bridge_dc_motor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# This example uses an L9110 H-bridge driver to run a DC Motor using two PWM pins.
# https://www.adafruit.com/product/4489

# Hardware setup:
# DC motor via L9110 H-bridge driver on two PWM pins that are on their own channels
# e.g., RP2040 Pico pins GP28, GP27

import time
import board
import pwmio
from adafruit_motor import motor

PWM_PIN_A = board.GP28 # pick any pwm pins on their own channels
PWM_PIN_B = board.GP27

# DC motor setup
# DC Motors generate electrical noise when running that can reset the microcontroller in extreme
# cases. A capacitor can be used to help prevent this.
pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50)
pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50)
motor1 = motor.DCMotor(pwm_a, pwm_b)

print("***DC motor test***")

print("\nForwards slow")
motor1.throttle = 0.5
print(" throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print(" throttle:", motor1.throttle)
time.sleep(1)

print("\nForwards")
motor1.throttle = 1.0
print(" throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards")
motor1.throttle = -1.0
print(" throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print("throttle:", motor1.throttle)
time.sleep(1)

print("\nBackwards slow")
motor1.throttle = -0.5
print(" throttle:", motor1.throttle)
time.sleep(1)

print("\nStop")
motor1.throttle = 0
print(" throttle:", motor1.throttle)
time.sleep(1)

print("\nSpin freely")
motor1.throttle = None
print(" throttle:", motor1.throttle)

print("\n***Motor test is complete***")