in1 / in2
Direction pair for the H-bridge.
Control DC motor direction and speed through an L298N-style driver.
Control direction and speed for common two-wire DC motors.
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.
These are the sections learners should understand before changing the sketch.
Direction pair for the H-bridge.
Sets motor speed with PWM.
Swapping IN1/IN2 changes motor direction.
Use this first table for the exact sample wiring, then use the board-family table when moving the sketch to another Arduino board.
| Code name | Sample pin | Mode | Connect to | Note |
|---|---|---|---|---|
| in1 / in2 | D7, D8 | OUTPUT | L298N IN1 and IN2 | Controls motor direction. |
| enablePin | D3 | PWM OUTPUT | L298N ENA/ENB | Controls motor speed. Share GND between motor supply and Arduino. |
The code names stay the same; only the physical board pins may change by family. Always confirm with the board silkscreen when using clones or special variants.
| Board family | Digital | PWM | Analog | I2C | SPI | Interrupt |
|---|---|---|---|---|---|---|
| Uno / Nano / Pro MiniDefault target for these samples. | D0-D13 | D3, D5, D6, D9, D10, D11 | A0-A5 | A4 SDA, A5 SCL | D10 SS, D11 MOSI, D12 MISO, D13 SCK / ICSP | D2, D3 |
| Mega 2560Best for many sensors or multiple serial ports. | D0-D53 | D2-D13, D44-D46 | A0-A15 | D20 SDA, D21 SCL | D50 MISO, D51 MOSI, D52 SCK, D53 SS / ICSP | D2, D3, D18, D19, D20, D21 |
| Leonardo / MicroUSB serial can behave differently because the main MCU handles USB. | D0-D13 | D3, D5, D6, D9, D10, D11, D13 | A0-A5 plus extra analog-capable pins by board | D2 SDA, D3 SCL | ICSP header | D0, D1, D2, D3, D7 |
| Nano Every / Uno WiFi Rev2Check the board package pinout for exact PWM labels. | D0-D13 | PWM-marked digital pins | A0-A7 on Nano Every, A0-A5 on Uno WiFi Rev2 | SDA/SCL marked pins | ICSP/SPI marked pins | Use pins supported by digitalPinToInterrupt() |
| Uno R4 Minima / Uno R4 WiFi5V logic board; still confirm shield/library compatibility. | D0-D13 | PWM-marked digital pins | A0-A5 | SDA/SCL marked pins | ICSP/SPI marked pins | Use digitalPinToInterrupt(pin) |
| Due3.3V logic only; protect pins from 5V sensors. | D0-D53 | PWM-marked digital pins | A0-A11 | SDA/SCL and SDA1/SCL1 | SPI header | Use digitalPinToInterrupt(pin) |
| MKR / Nano 33 family3.3V logic. Some AVR-only libraries may need alternatives. | Board-marked digital pins | PWM-marked pins | Board-marked analog pins | SDA/SCL marked pins | SPI marked pins | Use digitalPinToInterrupt(pin) |
/*
Roboda verified Arduino sample 0017
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 = 3;
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enablePin, 90);
delay(1000);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(1000);
}
Not a member yet? Register now
Are you a member? Login now
New messages appear in the Finite LiveChat Ops Android app.
Rate the lesson quality, diagrams, and support experience.