A little curiosity. A whole lot of making.Robotics, CNC & connected ideas
robodatech.
Arduino sample library

L298N Motor Driver 29

Control direction and speed for common two-wire DC motors.

Open in compiler →
/*
  Roboda verified Arduino sample 0717
  Feature: L298N Motor Driver
  Generated for online build, classroom practice, and hardware upload testing.
*/

const byte in1 = 7;
const byte in2 = 8;
const byte enablePin = 10;

void setup() {
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(enablePin, OUTPUT);
}

void loop() {
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  analogWrite(enablePin, 118);
  delay(1000);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(1000);
}

How it works

Control DC motor direction and speed through an L298N-style driver.

  1. Connect IN1/IN2 to direction pins.
  2. Connect ENA/ENB to the PWM enable pin.
  3. Use an external motor power supply.
  4. Share GND between Arduino and motor driver.

Connections

PinConnect toNote
D7, D8L298N IN1 and IN2Controls motor direction.
D10L298N ENA/ENBControls motor speed. Share GND between motor supply and Arduino.

Good ideas start here.

Robotics lessons, build notes and useful updates. Subscribe by entering your email.