Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Here's an example Python code to control two DC motors using the Pololu DRV8835 Dual Motor Driver for Raspberry Pi 2/3. The code
- # demonstrates how to control the motor speed using PWM and change the motor direction using GPIO pins.
- # To use the code, connect the Pololu DRV8835 Dual Motor Driver's inputs to the corresponding pins on your Raspberry Pi as specified in # the code, and run the Python script. The motors will run in both directions at different speeds with a pause in between.
- # Remember to connect the driver's power supply pins (VCC and GND) to a suitable power supply for your motors, the motor terminals to
- # the driver's outputs (M1A and M1B for Motor 1, M2A and M2B for Motor 2), and the driver's GND pin to the GND pin on the Raspberry Pi.
- # Before running the code, make sure to install the RPi.GPIO library on your Raspberry Pi if it's not already installed. You can
- # install it using the following command:
- # sudo apt-get install python3-rpi.gpio
- # To run the Python script, save the code in a file named, for example, "dual_motor_driver_example.py" and execute the following
- # command in the terminal:
- # python3 dual_motor_driver_example.py
- # The example code provided controls the direction and speed of two motors independently, running them clockwise and counter-clockwise # at different speeds with a pause in between. You can modify this code to suit the specific requirements of your project or
- # application.
- import RPi.GPIO as GPIO
- import time
- # Pololu DRV8835 Dual Motor Driver for Raspberry Pi 2/3 example
- # Motor 1
- motor1PWM = 12 # Connect Motor 1 PWM input to GPIO 12 (PWM0) on the Raspberry Pi
- motor1DIR = 5 # Connect Motor 1 DIR input to GPIO 5 on the Raspberry Pi
- # Motor 2
- motor2PWM = 13 # Connect Motor 2 PWM input to GPIO 13 (PWM1) on the Raspberry Pi
- motor2DIR = 6 # Connect Motor 2 DIR input to GPIO 6 on the Raspberry Pi
- # Setup GPIO
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(motor1PWM, GPIO.OUT)
- GPIO.setup(motor1DIR, GPIO.OUT)
- GPIO.setup(motor2PWM, GPIO.OUT)
- GPIO.setup(motor2DIR, GPIO.OUT)
- # Initialize PWM
- pwm1 = GPIO.PWM(motor1PWM, 100) # Set PWM frequency to 100 Hz
- pwm2 = GPIO.PWM(motor2PWM, 100) # Set PWM frequency to 100 Hz
- pwm1.start(0)
- pwm2.start(0)
- try:
- while True:
- # Rotate both motors clockwise at full speed
- GPIO.output(motor1DIR, GPIO.HIGH)
- pwm1.ChangeDutyCycle(100) # Set duty cycle (0-100 for speed control)
- GPIO.output(motor2DIR, GPIO.HIGH)
- pwm2.ChangeDutyCycle(100) # Set duty cycle (0-100 for speed control)
- print("Both motors running clockwise at full speed.")
- time.sleep(2) # Run the motors for 2 seconds
- # Stop both motors
- pwm1.ChangeDutyCycle(0)
- pwm2.ChangeDutyCycle(0)
- print("Both motors stopped.")
- time.sleep(2) # Wait for 2 seconds
- # Rotate both motors counter-clockwise at half speed
- GPIO.output(motor1DIR, GPIO.LOW)
- pwm1.ChangeDutyCycle(50) # Set duty cycle (0-100 for speed control)
- GPIO.output(motor2DIR, GPIO.LOW)
- pwm2.ChangeDutyCycle(50) # Set duty cycle (0-100 for speed control)
- print("Both motors running counter-clockwise at half speed.")
- time.sleep(2) # Run the motors for 2 seconds
- # Stop both motors
- pwm1.ChangeDutyCycle(0)
- pwm2.ChangeDutyCycle(0)
- print("Both motors stopped.")
- time.sleep(2) # Wait for 2 seconds
- except KeyboardInterrupt:
- print("Exiting...")
- finally:
- # Cleanup
- pwm1.stop()
- pwm2.stop()
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement