Skip to main content
Live robotics updates
Best Construction Scheduling Software for ContractorsRobotics & Automation NewsRenault Group expands Exotec warehouse automation across France and GermanyRobotics & Automation NewsWhat Are the Basics of Credit Approval?Robotics & Automation NewsExperts react to FCC limits on U.S. imports of new humanoid and mobile robotsThe Robot ReportCase study: Hai Robotics helps LPP Logistics scale automated e-commerce fulfilment in RomaniaRobotics & Automation Newsmimic robotics introduces ‘frontier video-action models’ to the factory floor at AudiRobotics & Automation NewsFrom cutting smartphone glass to ‘painting’ with light: Three applications of femtosecond lasersRobotics & Automation NewsNeura Robotics expands global physical AI training network in partnership with RWTH Aachen UniversityRobotics & Automation NewsNoTip launches public beta of robotic food delivery platform in OhioRobotics & Automation NewsSurviving the paper deluge: a one-year study in learning from demonstrationRobohubBest Construction Scheduling Software for ContractorsRobotics & Automation NewsRenault Group expands Exotec warehouse automation across France and GermanyRobotics & Automation NewsWhat Are the Basics of Credit Approval?Robotics & Automation NewsExperts react to FCC limits on U.S. imports of new humanoid and mobile robotsThe Robot ReportCase study: Hai Robotics helps LPP Logistics scale automated e-commerce fulfilment in RomaniaRobotics & Automation Newsmimic robotics introduces ‘frontier video-action models’ to the factory floor at AudiRobotics & Automation NewsFrom cutting smartphone glass to ‘painting’ with light: Three applications of femtosecond lasersRobotics & Automation NewsNeura Robotics expands global physical AI training network in partnership with RWTH Aachen UniversityRobotics & Automation NewsNoTip launches public beta of robotic food delivery platform in OhioRobotics & Automation NewsSurviving the paper deluge: a one-year study in learning from demonstrationRobohub
View all
Back
Arduino sample guide

roboda-sample-0067 - L298N Motor Driver 03

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

Step-by-step build

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

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.

Important code parts

These are the sections learners should understand before changing the sketch.

in1 / in2

Direction pair for the H-bridge.

analogWrite(enablePin, speed)

Sets motor speed with PWM.

direction reversal

Swapping IN1/IN2 changes motor direction.

Pin mapping for this sample

Use this first table for the exact sample wiring, then use the board-family table when moving the sketch to another Arduino board.

Code nameSample pinModeConnect toNote
in1 / in2D7, D8OUTPUTL298N IN1 and IN2Controls motor direction.
enablePinD6PWM OUTPUTL298N ENA/ENBControls motor speed. Share GND between motor supply and Arduino.

Arduino board pin reference

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 familyDigitalPWMAnalogI2CSPIInterrupt
Uno / Nano / Pro MiniDefault target for these samples.D0-D13D3, D5, D6, D9, D10, D11A0-A5A4 SDA, A5 SCLD10 SS, D11 MOSI, D12 MISO, D13 SCK / ICSPD2, D3
Mega 2560Best for many sensors or multiple serial ports.D0-D53D2-D13, D44-D46A0-A15D20 SDA, D21 SCLD50 MISO, D51 MOSI, D52 SCK, D53 SS / ICSPD2, D3, D18, D19, D20, D21
Leonardo / MicroUSB serial can behave differently because the main MCU handles USB.D0-D13D3, D5, D6, D9, D10, D11, D13A0-A5 plus extra analog-capable pins by boardD2 SDA, D3 SCLICSP headerD0, D1, D2, D3, D7
Nano Every / Uno WiFi Rev2Check the board package pinout for exact PWM labels.D0-D13PWM-marked digital pinsA0-A7 on Nano Every, A0-A5 on Uno WiFi Rev2SDA/SCL marked pinsICSP/SPI marked pinsUse pins supported by digitalPinToInterrupt()
Uno R4 Minima / Uno R4 WiFi5V logic board; still confirm shield/library compatibility.D0-D13PWM-marked digital pinsA0-A5SDA/SCL marked pinsICSP/SPI marked pinsUse digitalPinToInterrupt(pin)
Due3.3V logic only; protect pins from 5V sensors.D0-D53PWM-marked digital pinsA0-A11SDA/SCL and SDA1/SCL1SPI headerUse digitalPinToInterrupt(pin)
MKR / Nano 33 family3.3V logic. Some AVR-only libraries may need alternatives.Board-marked digital pinsPWM-marked pinsBoard-marked analog pinsSDA/SCL marked pinsSPI marked pinsUse digitalPinToInterrupt(pin)

Full sample code

/*
  Roboda verified Arduino sample 0067
  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, 92);
  delay(1000);
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  delay(1000);
}

Teacher notes

  • Compile the sample before changing wiring or constants.
  • When changing boards, keep the code names the same and update only the pin number constants.
  • For 3.3V boards, level-shift 5V sensors and modules before connecting signal pins.
  • Use the first compiler error and the Roboda diagnostic suggestion before fixing later warnings.
Roboda support Leave a message

New messages appear in the Finite LiveChat Ops Android app.