Advertisement
microrobotics

Pololu Motor Controller, I2C 4.5-48V 2.2A

Apr 22nd, 2023
577
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Pololu Simple Motor Controller can also be controlled using I2C communication. The following example demonstrates how to use an Arduino to control the Pololu Simple Motor Controller via I2C communication.
  3.  
  4. First, connect the Simple Motor Controller to your Arduino as follows:
  5.  
  6. Connect the motor controller's SCL pin to the Arduino SCL pin (A5 on the Uno or Nano, 21 on the Mega, or 19 on the Leonardo).
  7. Connect the motor controller's SDA pin to the Arduino SDA pin (A4 on the Uno or Nano, 20 on the Mega, or 18 on the Leonardo).
  8. Connect a 4.7kΩ resistor between the SCL pin and 5V (this is a pull-up resistor for the I2C bus).
  9. Connect a 4.7kΩ resistor between the SDA pin and 5V (this is a pull-up resistor for the I2C bus).
  10. Connect the motor controller's ground (GND) to the Arduino ground.
  11. Note: Make sure to connect the motor and power supply to the motor controller following the instructions in the motor controller's user guide.
  12.  
  13. Next, upload the following code to your Arduino:
  14.  
  15. This code uses the Wire library to communicate with the Pololu Simple Motor Controller via I2C. 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.
  16.  
  17. 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.
  18.  
  19. 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().
  20. */
  21.  
  22. #include <Wire.h>
  23.  
  24. // Pololu Simple Motor Controller I2C address
  25. const uint8_t SMC_I2C_ADDRESS = 0x13;
  26.  
  27. // Command values for forward, reverse, and brake
  28. const uint16_t FORWARD = 3200;
  29. const uint16_t REVERSE = 6400;
  30. const uint16_t BRAKE = 0;
  31.  
  32. void setup() {
  33.   // Initialize the I2C communication
  34.   Wire.begin();
  35.  
  36.   // Exit Safe Start mode (required for the motor controller to be enabled)
  37.   exitSafeStart();
  38. }
  39.  
  40. void loop() {
  41.   // Move the motor forward
  42.   setMotorSpeed(FORWARD);
  43.   delay(2000);
  44.  
  45.   // Brake
  46.   setMotorSpeed(BRAKE);
  47.   delay(1000);
  48.  
  49.   // Move the motor in reverse
  50.   setMotorSpeed(REVERSE);
  51.   delay(2000);
  52.  
  53.   // Brake
  54.   setMotorSpeed(BRAKE);
  55.   delay(1000);
  56. }
  57.  
  58. void setMotorSpeed(uint16_t speed) {
  59.   Wire.beginTransmission(SMC_I2C_ADDRESS);
  60.   Wire.write(0x40 | (speed & 0x1F));
  61.   Wire.write(speed >> 5);
  62.   Wire.endTransmission();
  63. }
  64.  
  65. void exitSafeStart() {
  66.   Wire.beginTransmission(SMC_I2C_ADDRESS);
  67.   Wire.write(0x83);
  68.   Wire.endTransmission();
  69. }
  70.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement