Advertisement
microrobotics

Pololu Dual VNH5019 Motor Driver Shield for Arduino

Apr 22nd, 2023
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Dual VNH5019 Motor Driver Shield for Arduino allows you to control two high-power DC motors using an Arduino Uno or compatible board. The following example demonstrates how to use an Arduino to control two motors with the Pololu Dual VNH5019 Motor Driver Shield.
  3.  
  4. First, stack the Pololu Dual VNH5019 Motor Driver Shield on top of your Arduino Uno or compatible board, making sure that the pins are correctly aligned.
  5.  
  6. Next, upload the following code to your Arduino:
  7.  
  8. This code sets the appropriate control pins for the two motors (Motor A and Motor B) and initializes them as outputs. In the loop(), both motors are controlled to move forward, brake, move in reverse, and brake again in a repeating cycle.
  9.  
  10. Make sure you have the motors connected correctly to the motor driver shield and have the appropriate power supply connected. After uploading the code to your Arduino, both motors should start moving as per the commands in the loop().
  11.  
  12. You can modify the code to control the motor speed and direction based on other inputs or events according to your project requirements. To control the motor speed, adjust the PWM values (0 to 255) passed to the analogWrite() function.
  13. */
  14.  
  15. // Motor A pins
  16. const int AIN1 = 2;
  17. const int AIN2 = 4;
  18. const int PWMA = 9;
  19.  
  20. // Motor B pins
  21. const int BIN1 = 7;
  22. const int BIN2 = 8;
  23. const int PWMB = 10;
  24.  
  25. void setup() {
  26.   // Set motor control pins as outputs
  27.   pinMode(AIN1, OUTPUT);
  28.   pinMode(AIN2, OUTPUT);
  29.   pinMode(PWMA, OUTPUT);
  30.   pinMode(BIN1, OUTPUT);
  31.   pinMode(BIN2, OUTPUT);
  32.   pinMode(PWMB, OUTPUT);
  33. }
  34.  
  35. void loop() {
  36.   // Move both motors forward at full speed
  37.   digitalWrite(AIN1, HIGH);
  38.   digitalWrite(AIN2, LOW);
  39.   analogWrite(PWMA, 255);
  40.  
  41.   digitalWrite(BIN1, HIGH);
  42.   digitalWrite(BIN2, LOW);
  43.   analogWrite(PWMB, 255);
  44.  
  45.   delay(2000);
  46.  
  47.   // Stop both motors (brake)
  48.   digitalWrite(AIN1, LOW);
  49.   digitalWrite(AIN2, LOW);
  50.   digitalWrite(BIN1, LOW);
  51.   digitalWrite(BIN2, LOW);
  52.  
  53.   delay(1000);
  54.  
  55.   // Move both motors in reverse at full speed
  56.   digitalWrite(AIN1, LOW);
  57.   digitalWrite(AIN2, HIGH);
  58.   analogWrite(PWMA, 255);
  59.  
  60.   digitalWrite(BIN1, LOW);
  61.   digitalWrite(BIN2, HIGH);
  62.   analogWrite(PWMB, 255);
  63.  
  64.   delay(2000);
  65.  
  66.   // Stop both motors (brake)
  67.   digitalWrite(AIN1, LOW);
  68.   digitalWrite(AIN2, LOW);
  69.   digitalWrite(BIN1, LOW);
  70.   digitalWrite(BIN2, LOW);
  71.  
  72.   delay(1000);
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement