Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- First, connect the Simple Motor Controller to your Arduino as follows:
- 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).
- 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).
- Connect a 4.7kΩ resistor between the SCL pin and 5V (this is a pull-up resistor for the I2C bus).
- Connect a 4.7kΩ resistor between the SDA pin and 5V (this is a pull-up resistor for the I2C bus).
- Connect the motor controller's ground (GND) to the Arduino ground.
- Note: Make sure to connect the motor and power supply to the motor controller following the instructions in the motor controller's user guide.
- Next, upload the following code to your Arduino:
- 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.
- 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.
- 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().
- */
- #include <Wire.h>
- // Pololu Simple Motor Controller I2C address
- const uint8_t SMC_I2C_ADDRESS = 0x13;
- // Command values for forward, reverse, and brake
- const uint16_t FORWARD = 3200;
- const uint16_t REVERSE = 6400;
- const uint16_t BRAKE = 0;
- void setup() {
- // Initialize the I2C communication
- Wire.begin();
- // Exit Safe Start mode (required for the motor controller to be enabled)
- exitSafeStart();
- }
- void loop() {
- // Move the motor forward
- setMotorSpeed(FORWARD);
- delay(2000);
- // Brake
- setMotorSpeed(BRAKE);
- delay(1000);
- // Move the motor in reverse
- setMotorSpeed(REVERSE);
- delay(2000);
- // Brake
- setMotorSpeed(BRAKE);
- delay(1000);
- }
- void setMotorSpeed(uint16_t speed) {
- Wire.beginTransmission(SMC_I2C_ADDRESS);
- Wire.write(0x40 | (speed & 0x1F));
- Wire.write(speed >> 5);
- Wire.endTransmission();
- }
- void exitSafeStart() {
- Wire.beginTransmission(SMC_I2C_ADDRESS);
- Wire.write(0x83);
- Wire.endTransmission();
- }
Advertisement
Advertisement