Advertisement
microrobotics

Pololu Motor Controller, Serial 4.5-48V 2.2A

Apr 22nd, 2023
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Simple Motor Controller is a versatile and user-friendly motor controller capable of operating at 4.5-48V and delivering up to 2.2A continuous. The following example demonstrates how to use an Arduino to control the Pololu Simple Motor Controller via TTL Serial communication.
  3.  
  4. First, connect the Simple Motor Controller to your Arduino as follows:
  5.  
  6. Connect the motor controller's TX pin to Arduino pin 10 (RX).
  7. Connect the motor controller's RX pin to Arduino pin 11 (TX).
  8. Connect the motor controller's ground (GND) to the Arduino ground.
  9. Note: Make sure to connect the motor and power supply to the motor controller following the instructions in the motor controller's user guide.
  10.  
  11. Next, upload the following code to your Arduino:
  12.  
  13. This code uses the SoftwareSerial library to communicate with the Pololu Simple Motor Controller via TTL Serial. The setMotorSpeed() function sends the motor speed command, and the exitSafeStart() function exits the Safe Start mode, which is required for the motor controller to be enabled.
  14.  
  15. In the loop(), the motor is controlled to move forward, brake, move in reverse, and brake again in a repeating cycle. You can modify the code to control the motor speed based on other inputs or events according to your project requirements.
  16.  
  17. Make sure you have the motor connected correctly to the motor controller and have the appropriate power supply connected. After uploading the code to your Arduino, the motor should start moving as per the commands in the loop().
  18. */
  19.  
  20. #include <SoftwareSerial.h>
  21.  
  22. // Pololu Simple Motor Controller pins
  23. const int RX_PIN = 10;
  24. const int TX_PIN = 11;
  25.  
  26. // Create a SoftwareSerial object for communication with the motor controller
  27. SoftwareSerial motorControllerSerial(RX_PIN, TX_PIN);
  28.  
  29. // Command values for forward, reverse, and brake
  30. const uint16_t FORWARD = 3200;
  31. const uint16_t REVERSE = 6400;
  32. const uint16_t BRAKE = 0;
  33.  
  34. void setup() {
  35.   // Initialize the motor controller serial communication
  36.   motorControllerSerial.begin(9600);
  37.  
  38.   // Exit Safe Start mode (required for the motor controller to be enabled)
  39.   exitSafeStart();
  40. }
  41.  
  42. void loop() {
  43.   // Move the motor forward
  44.   setMotorSpeed(FORWARD);
  45.   delay(2000);
  46.  
  47.   // Brake
  48.   setMotorSpeed(BRAKE);
  49.   delay(1000);
  50.  
  51.   // Move the motor in reverse
  52.   setMotorSpeed(REVERSE);
  53.   delay(2000);
  54.  
  55.   // Brake
  56.   setMotorSpeed(BRAKE);
  57.   delay(1000);
  58. }
  59.  
  60. void setMotorSpeed(uint16_t speed) {
  61.   // Command format: 0xAA, device number (0x0C for default), command byte, data byte(s), CRC byte
  62.   uint8_t command[] = {0xAA, 0x0C, 0x40 | (speed & 0x1F), speed >> 5};
  63.  
  64.   motorControllerSerial.write(command, sizeof(command));
  65. }
  66.  
  67. void exitSafeStart() {
  68.   // Command format: 0xAA, device number (0x0C for default), command byte
  69.   uint8_t command[] = {0xAA, 0x0C, 0x83};
  70.  
  71.   motorControllerSerial.write(command, sizeof(command));
  72. }
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement