Advertisement
microrobotics

Pololu DRV8876 Motor Driver

Apr 21st, 2023
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's an example code to control a DC motor using the Pololu DRV8876 Motor Driver with an Arduino or a similar microcontroller. The code demonstrates how to control the motor speed using PWM and change the motor direction using a digital pin.
  3.  
  4. To use the code, connect the Pololu DRV8876 Motor Driver's PWM input to digital pin 9 and the DIR input to digital pin 8 on your Arduino board. Upload the code using the Arduino IDE and open the Serial Monitor to see the motor status.
  5.  
  6. Remember to connect the driver's VM pin to a suitable power supply for your motor (up to 45V), the motor's terminals to the driver's A and B outputs, and the driver's GND pin to the GND pin on the Arduino.
  7. */
  8.  
  9. // Pololu DRV8876 DC Motor Driver 1.3A (3.5A Max) example for Arduino
  10.  
  11. const int motorPWM = 9; // Connect the driver's PWM input to digital pin 9 (PWM capable) on the Arduino
  12. const int motorDIR = 8; // Connect the driver's DIR input to digital pin 8 on the Arduino
  13.  
  14. void setup() {
  15.   pinMode(motorPWM, OUTPUT); // Set motorPWM pin as output
  16.   pinMode(motorDIR, OUTPUT); // Set motorDIR pin as output
  17.   Serial.begin(9600); // Start the Serial Monitor with a baud rate of 9600
  18. }
  19.  
  20. void loop() {
  21.   // Rotate the motor clockwise at full speed
  22.   digitalWrite(motorDIR, HIGH); // Set the motor direction (HIGH for clockwise)
  23.   analogWrite(motorPWM, 255); // Set the motor speed (0 - 255 for PWM control)
  24.   Serial.println("Motor running clockwise at full speed.");
  25.   delay(2000); // Run the motor for 2 seconds
  26.  
  27.   // Stop the motor
  28.   analogWrite(motorPWM, 0); // Set the motor speed to 0 to stop the motor
  29.   Serial.println("Motor stopped.");
  30.   delay(2000); // Wait for 2 seconds
  31.  
  32.   // Rotate the motor counter-clockwise at half speed
  33.   digitalWrite(motorDIR, LOW); // Set the motor direction (LOW for counter-clockwise)
  34.   analogWrite(motorPWM, 127); // Set the motor speed (0 - 255 for PWM control)
  35.   Serial.println("Motor running counter-clockwise at half speed.");
  36.   delay(2000); // Run the motor for 2 seconds
  37.  
  38.   // Stop the motor
  39.   analogWrite(motorPWM, 0); // Set the motor speed to 0 to stop the motor
  40.   Serial.println("Motor stopped.");
  41.   delay(2000); // Wait for 2 seconds
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement