Advertisement
microrobotics

Pololu Simple High-Power Motor Controller 18v25

Apr 14th, 2023
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Simple High-Power Motor Controller 18v25 is a versatile motor controller that supports various communication protocols like TTL serial, I2C, and USB. In this example, I'll provide you with an Arduino code to control a motor using the TTL serial interface. Note that this code assumes you are using an Arduino Uno or a similar board.
  3.  
  4. First, connect the motor controller to the Arduino as follows:
  5.  
  6. TX (Arduino) to RX (Motor Controller)
  7. RX (Arduino) to TX (Motor Controller)
  8. GND (Arduino) to GND (Motor Controller)
  9. Connect the motor to the motor controller's output and supply power to the motor controller as per your motor's requirements.
  10.  
  11. Upload this code to your Arduino. The motor will run at a speed of 127 in the forward direction for 2 seconds, then brake for 2 seconds. This process will repeat indefinitely. You can change the speed and brake values as needed.
  12. */
  13.  
  14. // Pololu Simple High-Power Motor Controller 18v25
  15. // Example code for Arduino
  16.  
  17. #include <SoftwareSerial.h>
  18.  
  19. // Motor Controller Pins
  20. const int rxPin = 10; // Connect the RX pin of the motor controller to Digital Pin 10
  21. const int txPin = 11; // Connect the TX pin of the motor controller to Digital Pin 11
  22.  
  23. // Create a SoftwareSerial object for communication with the motor controller
  24. SoftwareSerial motorControllerSerial(rxPin, txPin);
  25.  
  26. // Motor controller command IDs
  27. const byte SET_SPEED = 0x85;
  28. const byte SET_BRAKE = 0x92;
  29.  
  30. void setup() {
  31.   Serial.begin(9600); // Initialize the serial communication for debugging
  32.   motorControllerSerial.begin(9600); // Initialize the software serial communication with the motor controller
  33. }
  34.  
  35. void loop() {
  36.   int speed = 127; // Speed range: -3200 to 3200 (use -127 to 127 for full range)
  37.   setMotorSpeed(speed);
  38.   delay(2000); // Run the motor for 2 seconds
  39.  
  40.   setBrake(32); // Brake strength: 0 to 32
  41.   delay(2000); // Brake for 2 seconds
  42. }
  43.  
  44. void setMotorSpeed(int speed) {
  45.   if (speed < 0) {
  46.     motorControllerSerial.write(0x86); // Reverse direction
  47.     speed = -speed;
  48.   } else {
  49.     motorControllerSerial.write(0x85); // Forward direction
  50.   }
  51.  
  52.   // Ensure speed is within allowed range
  53.   if (speed > 3200) {
  54.     speed = 3200;
  55.   }
  56.  
  57.   // Send speed value (split into two bytes)
  58.   motorControllerSerial.write(speed & 0x1F);
  59.   motorControllerSerial.write((speed >> 5) & 0x7F);
  60. }
  61.  
  62. void setBrake(byte brake) {
  63.   motorControllerSerial.write(0x92);
  64.   motorControllerSerial.write(brake & 0x1F);
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement