Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Here's an example code to control a DC motor using the Pololu DRV8876 Motor Driver with an Arduino or a similar microcontroller. The code demonstrates how to control the motor speed using PWM and change the motor direction using a digital pin.
- To use the code, connect the Pololu DRV8876 Motor Driver's PWM input to digital pin 9 and the DIR input to digital pin 8 on your Arduino board. Upload the code using the Arduino IDE and open the Serial Monitor to see the motor status.
- Remember to connect the driver's VM pin to a suitable power supply for your motor (up to 45V), the motor's terminals to the driver's A and B outputs, and the driver's GND pin to the GND pin on the Arduino.
- */
- // Pololu DRV8876 DC Motor Driver 1.3A (3.5A Max) example for Arduino
- const int motorPWM = 9; // Connect the driver's PWM input to digital pin 9 (PWM capable) on the Arduino
- const int motorDIR = 8; // Connect the driver's DIR input to digital pin 8 on the Arduino
- void setup() {
- pinMode(motorPWM, OUTPUT); // Set motorPWM pin as output
- pinMode(motorDIR, OUTPUT); // Set motorDIR pin as output
- Serial.begin(9600); // Start the Serial Monitor with a baud rate of 9600
- }
- void loop() {
- // Rotate the motor clockwise at full speed
- digitalWrite(motorDIR, HIGH); // Set the motor direction (HIGH for clockwise)
- analogWrite(motorPWM, 255); // Set the motor speed (0 - 255 for PWM control)
- Serial.println("Motor running clockwise at full speed.");
- delay(2000); // Run the motor for 2 seconds
- // Stop the motor
- analogWrite(motorPWM, 0); // Set the motor speed to 0 to stop the motor
- Serial.println("Motor stopped.");
- delay(2000); // Wait for 2 seconds
- // Rotate the motor counter-clockwise at half speed
- digitalWrite(motorDIR, LOW); // Set the motor direction (LOW for counter-clockwise)
- analogWrite(motorPWM, 127); // Set the motor speed (0 - 255 for PWM control)
- Serial.println("Motor running counter-clockwise at half speed.");
- delay(2000); // Run the motor for 2 seconds
- // Stop the motor
- analogWrite(motorPWM, 0); // Set the motor speed to 0 to stop the motor
- Serial.println("Motor stopped.");
- delay(2000); // Wait for 2 seconds
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement