3$ BLDC and Stepper FOC driver - L298N

Connection example

Here is the connection diagram.

  1. Connect the motor to the motor to the 3 out 4 outputs of the L298N
    Example:
    • OUT1 - motor phase A
    • OUT2 - motor phase B
    • OUT3 - motor phase C
  2. Connect the IN1,IN2,IN3 and IN4 to the Arduino. Make sure to connect the the INx ports with the same number as OUTx ports to the PWM pins. Example:
    • IN1 - 11
    • IN2 - 10
    • IN3 - 9
    • IN4 - 8 (doesn’t have to be pwm) - can be connected to GND directly
  3. Make sure that both ENA and ENB are HIGH.
    • On my board I have on both sides two pin jumper which does exactly this.
    • If you don’t have it, connect ENA and ENB to digital pins and set them to HIGH (or connect them to 5V pin directly)
  4. Connect the common ground of the MCU and L298N
  5. Connect the power supply
  6. Connect your sensor and everything else you will need for your FOC application - steps after step5. are no longer L298N specific.

Code Example

Now you can run any BLDCDriver3PWM example out there with this setup. Just make sure to either connect the IN4 to the ground directly or in the setup() function add:

pinMode(8,OUTPUT);
digitalWrite(8,LOW);

Here is the open loop example code:

// Open loop motor control example for L298N board
#include <SimpleFOC.h>

#define IN1 11
#define IN2 10
#define IN3 9
#define IN4 8

// BLDC motor & driver instance
// BLDCMotor motor = BLDCMotor(pole pair number);
BLDCMotor motor = BLDCMotor(11);
// BLDCDriver3PWM driver = BLDCDriver3PWM(pwmA, pwmB, pwmC, Enable(optional));
BLDCDriver3PWM driver = BLDCDriver3PWM(IN1, IN2, IN3);

void setup() {
  // deactivate the OUT4 output
  pinMode(IN4,OUTPUT);
  digitalWrite(IN4,LOW);


  // driver config
  // power supply voltage [V]
  driver.voltage_power_supply = 12;
  driver.init();
  // link the motor and the driver
  motor.linkDriver(&driver);

  // limiting motor movements
  motor.voltage_limit = 3;   // [V]
  motor.velocity_limit = 20; // [rad/s]

  // open loop control config
  motor.controller = ControlType::velocity_openloop;

  // init motor hardware
  motor.init();

  Serial.begin(115200);
  Serial.println("Motor ready!");
  _delay(1000);
}

float target_velocity = 2; // [rad/s]

void loop() {
  // open loop velocity movement
  // using motor.voltage_limit and motor.velocity_limit
  motor.move(target_velocity);

}

3 Likes