Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The DRV8871 is a motor driver IC designed to control DC motors. Below is a simple example Arduino code to use with the DRV8871 DC Motor Driver. Note that you'll need to connect the appropriate pins from your Arduino to the motor driver according to the datasheet.
- This code demonstrates how to control a DC motor using PWM for speed control and two digital pins for direction control. Adjust the pin assignments based on your actual wiring. The example includes forward and reverse motions with a brief stop in between. The motorControl function sets the motor speed and direction.
- Please refer to the DRV8871 datasheet for the correct pin connections and specifications based on your motor and power supply.
- */
- // Define the pins for motor control
- const int motorPWM = 9; // PWM input for motor speed control
- const int motorINA = 8; // Input A for motor direction control
- const int motorINB = 7; // Input B for motor direction control
- void setup() {
- // Set the motor control pins as outputs
- pinMode(motorPWM, OUTPUT);
- pinMode(motorINA, OUTPUT);
- pinMode(motorINB, OUTPUT);
- // Initialize the motor in the stopped state
- digitalWrite(motorINA, LOW);
- digitalWrite(motorINB, LOW);
- // Start the serial communication for debugging (optional)
- Serial.begin(9600);
- }
- void loop() {
- // Run the motor forward for 2 seconds
- motorControl(255, HIGH, LOW);
- delay(2000);
- // Stop the motor for 1 second
- motorControl(0, LOW, LOW);
- delay(1000);
- // Run the motor in reverse for 2 seconds
- motorControl(255, LOW, HIGH);
- delay(2000);
- // Stop the motor for 1 second
- motorControl(0, LOW, LOW);
- delay(1000);
- }
- // Function to control the motor
- void motorControl(int speed, int inA, int inB) {
- analogWrite(motorPWM, speed); // Set motor speed
- digitalWrite(motorINA, inA); // Set direction A
- digitalWrite(motorINB, inB); // Set direction B
- // Optional: print motor speed and direction for debugging
- Serial.print("Speed: ");
- Serial.println(speed);
- Serial.print("Direction: ");
- Serial.println((inA == HIGH) ? "Forward" : (inB == HIGH) ? "Reverse" : "Stop");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement