Arduino Nano 33 IoT - DC Motor

by DIYables in Circuits > Arduino

32 Views, 0 Favorites, 0 Comments

Arduino Nano 33 IoT - DC Motor

arduino-nano-33-iot-dc-motor-cover.jpg

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.

  1. Arduino Nano 33 IoT
  2. Micro USB Cable
  3. L298N Motor Driver Module
  4. 5V DC Motor
  5. 5V Power Adapter for 5V DC motor
  6. Breadboard
  7. Jumper Wires
  8. DC Power Jack
  9. Screw Terminal Expansion Board for Arduino Nano
  10. Breakout Expansion Board for Arduino Nano
  11. Power Splitter for Arduino Nano
  12. DIYables Sensor Kit (18 sensors/displays)

Wiring

arduino-nano-33-iot-dc-motor-l298n-wiring-diagram.jpg

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.

/*
* 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 and code explanation: https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-dc-motor