Control direction and speed for common two-wire DC motors.
Open in compiler →/*
Roboda verified Arduino sample 0517
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 = 6;
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enablePin, 110);
delay(1000);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(1000);
}
How it works
Control DC motor direction and speed through an L298N-style driver.
- Connect IN1/IN2 to direction pins.
- Connect ENA/ENB to the PWM enable pin.
- Use an external motor power supply.
- Share GND between Arduino and motor driver.
Connections
| Pin | Connect to | Note |
|---|---|---|
| D7, D8 | L298N IN1 and IN2 | Controls motor direction. |
| D6 | L298N ENA/ENB | Controls motor speed. Share GND between motor supply and Arduino. |