Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 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.
- First, connect the motor controller to the Arduino as follows:
- TX (Arduino) to RX (Motor Controller)
- RX (Arduino) to TX (Motor Controller)
- GND (Arduino) to GND (Motor Controller)
- Connect the motor to the motor controller's output and supply power to the motor controller as per your motor's requirements.
- 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.
- */
- // Pololu Simple High-Power Motor Controller 18v25
- // Example code for Arduino
- #include <SoftwareSerial.h>
- // Motor Controller Pins
- const int rxPin = 10; // Connect the RX pin of the motor controller to Digital Pin 10
- const int txPin = 11; // Connect the TX pin of the motor controller to Digital Pin 11
- // Create a SoftwareSerial object for communication with the motor controller
- SoftwareSerial motorControllerSerial(rxPin, txPin);
- // Motor controller command IDs
- const byte SET_SPEED = 0x85;
- const byte SET_BRAKE = 0x92;
- void setup() {
- Serial.begin(9600); // Initialize the serial communication for debugging
- motorControllerSerial.begin(9600); // Initialize the software serial communication with the motor controller
- }
- void loop() {
- int speed = 127; // Speed range: -3200 to 3200 (use -127 to 127 for full range)
- setMotorSpeed(speed);
- delay(2000); // Run the motor for 2 seconds
- setBrake(32); // Brake strength: 0 to 32
- delay(2000); // Brake for 2 seconds
- }
- void setMotorSpeed(int speed) {
- if (speed < 0) {
- motorControllerSerial.write(0x86); // Reverse direction
- speed = -speed;
- } else {
- motorControllerSerial.write(0x85); // Forward direction
- }
- // Ensure speed is within allowed range
- if (speed > 3200) {
- speed = 3200;
- }
- // Send speed value (split into two bytes)
- motorControllerSerial.write(speed & 0x1F);
- motorControllerSerial.write((speed >> 5) & 0x7F);
- }
- void setBrake(byte brake) {
- motorControllerSerial.write(0x92);
- motorControllerSerial.write(brake & 0x1F);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement