Advertisement
microrobotics

Pololu Dual VNH3SP30 Motor Driver

Apr 21st, 2023
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Here's an example code to control two DC motors using the Pololu Dual VNH3SP30 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 digital pins.
  3.  
  4. To use the code, connect the Pololu Dual VNH3SP30 Motor Driver's inputs to the corresponding pins on your Arduino board as specified in the code, and upload the code using the Arduino IDE. Open the Serial Monitor to see the motor status.
  5.  
  6. Remember to connect the driver's power supply pins (VIN and GND) to a suitable power supply for your motors, and the motor terminals to the driver's outputs (M1A and M1B for Motor 1, M2A and M2B for Motor 2).
  7.  
  8. For the Pololu Dual VNH3SP30 Motor Driver, ensure that the current-limiting jumpers are properly set according to the motor's specifications to avoid overloading the driver. Additionally, note that the VNH3SP30 motor driver can get hot during operation, so proper heat dissipation methods (e.g., a heatsink) should be employed if necessary.
  9.  
  10. 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.
  11.  
  12. If you would like to implement additional features like current sensing or the use of the driver's enable/disable pin, you can refer to the Pololu Dual VNH3SP30 Motor Driver datasheet for more details on how to connect and use these features.
  13. */
  14.  
  15. // Pololu Dual VNH3SP30 Motor Driver example for Arduino
  16.  
  17. // Motor 1
  18. const int motor1PWM = 9; // Connect Motor 1 PWM input to digital pin 9 (PWM capable) on the Arduino
  19. const int motor1DIR_A = 8; // Connect Motor 1 INA (direction) input to digital pin 8 on the Arduino
  20. const int motor1DIR_B = 7; // Connect Motor 1 INB (direction) input to digital pin 7 on the Arduino
  21.  
  22. // Motor 2
  23. const int motor2PWM = 10; // Connect Motor 2 PWM input to digital pin 10 (PWM capable) on the Arduino
  24. const int motor2DIR_A = 12; // Connect Motor 2 INA (direction) input to digital pin 12 on the Arduino
  25. const int motor2DIR_B = 11; // Connect Motor 2 INB (direction) input to digital pin 11 on the Arduino
  26.  
  27. void setup() {
  28.   pinMode(motor1PWM, OUTPUT); // Set motor1PWM pin as output
  29.   pinMode(motor1DIR_A, OUTPUT); // Set motor1DIR_A pin as output
  30.   pinMode(motor1DIR_B, OUTPUT); // Set motor1DIR_B pin as output
  31.  
  32.   pinMode(motor2PWM, OUTPUT); // Set motor2PWM pin as output
  33.   pinMode(motor2DIR_A, OUTPUT); // Set motor2DIR_A pin as output
  34.   pinMode(motor2DIR_B, OUTPUT); // Set motor2DIR_B pin as output
  35.  
  36.   Serial.begin(9600); // Start the Serial Monitor with a baud rate of 9600
  37. }
  38.  
  39. void loop() {
  40.   // Rotate both motors clockwise at full speed
  41.   digitalWrite(motor1DIR_A, HIGH);
  42.   digitalWrite(motor1DIR_B, LOW);
  43.   analogWrite(motor1PWM, 255);
  44.  
  45.   digitalWrite(motor2DIR_A, HIGH);
  46.   digitalWrite(motor2DIR_B, LOW);
  47.   analogWrite(motor2PWM, 255);
  48.  
  49.   Serial.println("Both motors running clockwise at full speed.");
  50.   delay(2000); // Run the motors for 2 seconds
  51.  
  52.   // Stop both motors
  53.   analogWrite(motor1PWM, 0);
  54.   analogWrite(motor2PWM, 0);
  55.  
  56.   Serial.println("Both motors stopped.");
  57.   delay(2000); // Wait for 2 seconds
  58.  
  59.   // Rotate both motors counter-clockwise at half speed
  60.   digitalWrite(motor1DIR_A, LOW);
  61.   digitalWrite(motor1DIR_B, HIGH);
  62.   analogWrite(motor1PWM, 127);
  63.  
  64.   digitalWrite(motor2DIR_A, LOW);
  65.   digitalWrite(motor2DIR_B, HIGH);
  66.   analogWrite(motor2PWM, 127);
  67.  
  68.   Serial.println("Both motors running counter-clockwise at half speed.");
  69.   delay(2000); // Run the motors for 2 seconds
  70.  
  71.   // Stop both motors
  72.   analogWrite(motor1PWM, 0);
  73.   analogWrite(motor2PWM, 0);
  74.  
  75.   Serial.println("Both motors stopped.");
  76.   delay(2000); // Wait for 2 seconds
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement