L298N stepper motor driver - johnosbb/Automation GitHub Wiki

L298N motor driver

Example Code


void setupMotorControl()
{
  pinMode(MOTOR_LEFT_DIRECTION_1, OUTPUT);
  pinMode(MOTOR_LEFT_DIRECTION_2, OUTPUT);
  pinMode(MOTOR_LEFT_SPEED, OUTPUT);
  pinMode(MOTOR_RIGHT_DIRECTION_1, OUTPUT);
  pinMode(MOTOR_RIGHT_DIRECTION_2, OUTPUT);
  pinMode(MOTOR_RIGHT_SPEED, OUTPUT);
    // configure LEDC PWM for the left motor
  ledcAttachChannel(MOTOR_LEFT_SPEED, FREQUENCY, RESOLUTION, PWM_CHANNEL_LEFT);
  ledcAttachChannel(MOTOR_RIGHT_SPEED, FREQUENCY, RESOLUTION, PWM_CHANNEL_RIGHT);
}


// Function to move forward
void moveForward(int directionPin1, int directionPin2, int speedPin, int duty_cycle) {
    DEBUG_PRINTLN(F("Motor Moving Forward"));
    motor_status = MOVING_FORWARD;
    updateMotorStatus();
    digitalWrite(directionPin1, LOW);
    digitalWrite(directionPin2, HIGH);
    ledcWrite(speedPin, duty_cycle); // Set the speed using duty cycle (0-255)
}

// Function to move backward
void moveBackward(int directionPin1, int directionPin2, int speedPin, int duty_cycle) {
    // Move DC motor backwards at maximum speed
    DEBUG_PRINTLN(F("Motor Moving Backwards"));
    motor_status = MOVING_REVERSE;
    updateMotorStatus();
    digitalWrite(directionPin1, HIGH);
    digitalWrite(directionPin2, LOW);
    ledcWrite(speedPin, duty_cycle); // Set the speed using duty cycle (0-255)
}

// Function to stop the motor
void stopMotor(int directionPin1, int directionPin2, int speedPin) {
    DEBUG_PRINTLN(F("Motor Stopped"));
    motor_status = STOPPING;
    updateMotorStatus();
    digitalWrite(directionPin1, LOW);
    digitalWrite(directionPin2, LOW);
    ledcWrite(speedPin, 0); // Set speed to 0
}

// Function to execute a gentle turn to the right
void gentleTurnRight(int leftDirectionPin1, int leftDirectionPin2, int leftSpeedPin,
                     int rightDirectionPin1, int rightDirectionPin2, int rightSpeedPin) {
    moveForward(leftDirectionPin1, leftDirectionPin2, leftSpeedPin, HIGHER_SPEED);  // Left motors move at higher speed
    moveForward(rightDirectionPin1, rightDirectionPin2, rightSpeedPin, REDUCED_SPEED); // Right motors move at reduced speed
}

// Function to execute a gentle turn to the left
void gentleTurnLeft(int leftDirectionPin1, int leftDirectionPin2, int leftSpeedPin,
                    int rightDirectionPin1, int rightDirectionPin2, int rightSpeedPin) {
    moveForward(leftDirectionPin1, leftDirectionPin2, leftSpeedPin, REDUCED_SPEED);  // Left motors move at reduced speed
    moveForward(rightDirectionPin1, rightDirectionPin2, rightSpeedPin, HIGHER_SPEED); // Right motors move at higher speed
}


// Function to rotate right (spin in place clockwise)
void rotateRight(int leftDirectionPin1, int leftDirectionPin2, int leftSpeedPin,
                 int rightDirectionPin1, int rightDirectionPin2, int rightSpeedPin, int duty_cycle) {
    moveForward(leftDirectionPin1, leftDirectionPin2, leftSpeedPin, duty_cycle);  // Left motors move forward
    moveBackward(rightDirectionPin1, rightDirectionPin2, rightSpeedPin, duty_cycle); // Right motors move backward
}

// Function to rotate left (spin in place counterclockwise)
void rotateLeft(int leftDirectionPin1, int leftDirectionPin2, int leftSpeedPin,
                int rightDirectionPin1, int rightDirectionPin2, int rightSpeedPin, int duty_cycle) {
    moveBackward(leftDirectionPin1, leftDirectionPin2, leftSpeedPin, duty_cycle); // Left motors move backward
    moveForward(rightDirectionPin1, rightDirectionPin2, rightSpeedPin, duty_cycle);  // Right motors move forward
}