Advertisement
microrobotics

Pololu DRV8835 Dual Motor Driver for Raspberry Pi 2/3

Apr 21st, 2023
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. # 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
  2. # demonstrates how to control the motor speed using PWM and change the motor direction using GPIO pins.
  3.  
  4. # 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.
  5.  
  6. # Remember to connect the driver's power supply pins (VCC and GND) to a suitable power supply for your motors, the motor terminals to
  7. # 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.
  8.  
  9. # Before running the code, make sure to install the RPi.GPIO library on your Raspberry Pi if it's not already installed. You can
  10. # install it using the following command:
  11.  
  12. # sudo apt-get install python3-rpi.gpio
  13.  
  14. # To run the Python script, save the code in a file named, for example, "dual_motor_driver_example.py" and execute the following
  15. # command in the terminal:
  16.  
  17. # python3 dual_motor_driver_example.py
  18.  
  19. # 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
  20. # application.
  21.  
  22. import RPi.GPIO as GPIO
  23. import time
  24.  
  25. # Pololu DRV8835 Dual Motor Driver for Raspberry Pi 2/3 example
  26.  
  27. # Motor 1
  28. motor1PWM = 12 # Connect Motor 1 PWM input to GPIO 12 (PWM0) on the Raspberry Pi
  29. motor1DIR = 5  # Connect Motor 1 DIR input to GPIO 5 on the Raspberry Pi
  30.  
  31. # Motor 2
  32. motor2PWM = 13 # Connect Motor 2 PWM input to GPIO 13 (PWM1) on the Raspberry Pi
  33. motor2DIR = 6  # Connect Motor 2 DIR input to GPIO 6 on the Raspberry Pi
  34.  
  35. # Setup GPIO
  36. GPIO.setmode(GPIO.BCM)
  37. GPIO.setup(motor1PWM, GPIO.OUT)
  38. GPIO.setup(motor1DIR, GPIO.OUT)
  39. GPIO.setup(motor2PWM, GPIO.OUT)
  40. GPIO.setup(motor2DIR, GPIO.OUT)
  41.  
  42. # Initialize PWM
  43. pwm1 = GPIO.PWM(motor1PWM, 100) # Set PWM frequency to 100 Hz
  44. pwm2 = GPIO.PWM(motor2PWM, 100) # Set PWM frequency to 100 Hz
  45. pwm1.start(0)
  46. pwm2.start(0)
  47.  
  48. try:
  49.     while True:
  50.         # Rotate both motors clockwise at full speed
  51.         GPIO.output(motor1DIR, GPIO.HIGH)
  52.         pwm1.ChangeDutyCycle(100) # Set duty cycle (0-100 for speed control)
  53.  
  54.         GPIO.output(motor2DIR, GPIO.HIGH)
  55.         pwm2.ChangeDutyCycle(100) # Set duty cycle (0-100 for speed control)
  56.  
  57.         print("Both motors running clockwise at full speed.")
  58.         time.sleep(2) # Run the motors for 2 seconds
  59.  
  60.         # Stop both motors
  61.         pwm1.ChangeDutyCycle(0)
  62.         pwm2.ChangeDutyCycle(0)
  63.  
  64.         print("Both motors stopped.")
  65.         time.sleep(2) # Wait for 2 seconds
  66.  
  67.         # Rotate both motors counter-clockwise at half speed
  68.         GPIO.output(motor1DIR, GPIO.LOW)
  69.         pwm1.ChangeDutyCycle(50) # Set duty cycle (0-100 for speed control)
  70.  
  71.         GPIO.output(motor2DIR, GPIO.LOW)
  72.         pwm2.ChangeDutyCycle(50) # Set duty cycle (0-100 for speed control)
  73.  
  74.         print("Both motors running counter-clockwise at half speed.")
  75.         time.sleep(2) # Run the motors for 2 seconds
  76.  
  77.         # Stop both motors
  78.         pwm1.ChangeDutyCycle(0)
  79.         pwm2.ChangeDutyCycle(0)
  80.  
  81.         print("Both motors stopped.")
  82.         time.sleep(2) # Wait for 2 seconds
  83.  
  84. except KeyboardInterrupt:
  85.     print("Exiting...")
  86.  
  87. finally:
  88.     # Cleanup
  89.     pwm1.stop()
  90.     pwm2.stop()
  91.     GPIO.cleanup()
  92.  
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement