Arduino Nano 33 IoT - DC Motor
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-dc-motor
This guide shows how to control a 5V DC motor with the Arduino Nano 33 IoT and an L298N motor driver. The driver sits between the Arduino and the motor, protecting the board from the motor's higher voltage while letting the Arduino set both speed and direction through PWM and digital signals.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- 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
The L298N board ships with three jumpers; remove all of them before wiring. Connect the Arduino's digital output pin to ENA on the L298N to control motor A's speed with PWM. Connect two more Arduino digital pins to IN1 and IN2 to set direction. Wire the DC motor's two leads (one black, one red) to OUT1 and OUT2. Power the L298N from the 5V adapter, and use the screw terminal expansion board or breadboard to keep connections solid.
How It Works
The L298N has three jobs: it shields the Arduino from the motor's higher voltage, reverses the power supply to the motor to flip direction, and boosts the PWM signal from the Arduino in both current and voltage so it can actually drive the motor. Sending analogWrite() to ENA sets speed, from 0 (stopped) to 255 (full speed). The states of IN1 and IN2 together determine whether the motor spins clockwise, counterclockwise, or stops. Setting both IN1 and IN2 to the same level (both low or both high) stops the motor, as does setting speed to 0.
Code
The sketch uses pinMode() to set up the Arduino pins connected to ENA, IN1, and IN2, then uses analogWrite() on ENA to control motor A's speed and digitalWrite() on IN1/IN2 to set its direction. In the example sequence, the Arduino first ramps the motor up to speed, reverses its direction, slows it back down, and finally stops it, demonstrating each function covered above.
arduino-nano-33-iot-dc-motor.ino - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-dc-motor