This build lets you control a DC motor's speed and direction from an Arduino Nano 33 IoT using the L298N motor driver. A DC motor just spins faster or slower depending on how much power it gets, and reverses when you swap its polarity.
If you've only ever coded against GPIO pins before, this is a good first real-world build: it shows why motors need a driver board sitting between them and your microcontroller, and how a PWM signal turns into actual physical motion.
The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Nano 33 IoT - DC Motor
What you need
- Arduino Nano 33 IoT
- Micro USB Cable
- L298N Motor Driver Module
- 5V DC Motor
- 5V Power Adapter for 5V DC motor
- Breadboard
- Jumper Wires
- DC Power Jack
- Screw Terminal Expansion Board for Arduino Nano
- Breakout Expansion Board for Arduino Nano
- Power Splitter for Arduino Nano
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A DC motor has just two wires, one negative (black) and one positive (red), and it draws more current and voltage than the Nano 33 IoT's pins can safely supply, so you never connect it directly to the board. Instead it goes through the L298N driver. Motor A connects through IN1, IN2, ENA, OUT1 and OUT2 on the L298N; OUT1 and OUT2 go to the motor's two wires, ENA takes a PWM signal from an Arduino digital pin for speed, and IN1/IN2 take digital signals for direction. The L298N board ships with three jumpers in place, remove all of them before wiring anything up. Motor B uses the matching IN3, IN4, ENB, OUT3 and OUT4 pins if you add a second motor.
How it works
The L298N sits between the Arduino and the motor and does three jobs: it shields the Arduino from the motor's higher voltage, it flips the power supply to the motor to reverse its direction, and it boosts the Arduino's PWM signal in both current and voltage so it's strong enough to actually drive the motor.
Code
The sketch uses pinMode() to set up the IN1, IN2 and ENA pins as outputs, then walks through the motor's behavior step by step: it ramps the motor up to full speed with analogWrite() on ENA, reverses direction by toggling IN1 and IN2 with digitalWrite(), ramps the speed back down, and finally stops the motor. The same pattern repeats for motor B through ENB, IN3 and IN4 if you're driving two motors from the same L298N.
arduino-nano-33-iot-dc-motor.ino
/*
* This Arduino Nano 33 IoT code was developed by newbiely.com
*
* This Arduino Nano 33 IoT code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-dc-motor
*/
#define PIN_ENA 5 // The Arduino Nano 33 IoT pin connected to the EN1 pin L298N
#define PIN_IN1 4 // The Arduino Nano 33 IoT pin connected to the IN1 pin L298N
#define PIN_IN2 3 // The Arduino Nano 33 IoT pin connected to the IN2 pin L298N
// The setup function runs once on reset or power-up
void setup() {
// initialize digital pins as outputs.
pinMode(PIN_IN1, OUTPUT);
pinMode(PIN_IN2, OUTPUT);
pinMode(PIN_ENA, OUTPUT);
}
// The loop function repeats indefinitely
void loop() {
digitalWrite(PIN_IN1, HIGH); // control the motor's direction in clockwise
digitalWrite(PIN_IN2, LOW); // control the motor's direction in clockwise
for (int speed = 0; speed <= 255; speed++) {
analogWrite(PIN_ENA, speed); // speed up
delay(10);
}
delay(2000); // rotate at maximum speed 2 seconds in clockwise direction
// change direction
digitalWrite(PIN_IN1, LOW); // control the motor's direction in anti-clockwise
digitalWrite(PIN_IN2, HIGH); // control the motor's direction in anti-clockwise
delay(2000); // rotate at maximum speed for 2 seconds in anti-clockwise direction
for (int speed = 255; speed >= 0; speed--) {
analogWrite(PIN_ENA, speed); // speed down
delay(10);
}
delay(2000); // stop motor 2 second
}
Full tutorial
The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-dc-motor

Top comments (0)