Advertisement
microrobotics

Pololu G2 high-power motor driver

Apr 4th, 2023
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu G2 High-Power Motor Driver is available in several variants, but the basic control logic is similar for all. Here's an example code to control a Pololu G2 High-Power Motor Driver (18v25, 24v13, 18v17, or 24v21) using an Arduino:
  3.  
  4. This code sets the motor direction to forward, ramps up the motor speed from 0 to maximum, and then ramps down the speed to 0. It then reverses the motor direction and repeats the same process.
  5.  
  6. Connect the Pololu G2 High-Power Motor Driver to the Arduino as follows:
  7.  
  8. PWM pin to digital pin 9 on the Arduino
  9. DIR pin to digital pin 8 on the Arduino
  10. Additionally, make the following connections:
  11.  
  12. Connect the motor driver's GND pin to the GND pin on the Arduino
  13. Connect the motor driver's VIN and GND pins to an appropriate power supply for your motor
  14. Connect the motor to the motor driver's OUTA and OUTB pins
  15. Please note that you may need to adjust the pin numbers in the code depending on the specific pins you choose to use for connecting the motor driver to your Arduino.
  16. */
  17.  
  18. #include <Arduino.h>
  19.  
  20. const int pwmPin = 9;  // Connect this pin to the PWM pin of the motor driver
  21. const int dirPin = 8;  // Connect this pin to the DIR pin of the motor driver
  22.  
  23. void setup() {
  24.   pinMode(pwmPin, OUTPUT);
  25.   pinMode(dirPin, OUTPUT);
  26. }
  27.  
  28. void loop() {
  29.   // Set motor direction to forward
  30.   digitalWrite(dirPin, HIGH);
  31.  
  32.   // Ramp up the motor speed
  33.   for (int speed = 0; speed <= 255; speed++) {
  34.     analogWrite(pwmPin, speed);
  35.     delay(10);
  36.   }
  37.  
  38.   // Ramp down the motor speed
  39.   for (int speed = 255; speed >= 0; speed--) {
  40.     analogWrite(pwmPin, speed);
  41.     delay(10);
  42.   }
  43.  
  44.   // Set motor direction to reverse
  45.   digitalWrite(dirPin, LOW);
  46.  
  47.   // Ramp up the motor speed
  48.   for (int speed = 0; speed <= 255; speed++) {
  49.     analogWrite(pwmPin, speed);
  50.     delay(10);
  51.   }
  52.  
  53.   // Ramp down the motor speed
  54.   for (int speed = 255; speed >= 0; speed--) {
  55.     analogWrite(pwmPin, speed);
  56.     delay(10);
  57.   }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement