Arduino Nano 33 IoT - DC Motor

This guide explains how to use the Arduino Nano 33 IoT to control a DC motor with the L298N Motor Driver. We will show you how to set the speed and direction of the DC motor. First, you will learn to control one DC motor, and then you will learn how to control two DC motors with one L298N Motor Driver.

Arduino Nano 33 IoT DC Motor

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×L298N Motor Driver Module
1×5V DC Motor
1×5V Power Adapter for 5V DC motor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of DC Motor

DC Motor Pinout

A DC Motor has two wires: one is negative (black) and the other is positive (red).

DC Motor Pinout

How DC Motor Works

The speed and direction of a DC motor depend on how we supply power to it. The pictures below show in detail how power affects both speed and direction.

How to control DC motor

When using PWM, a higher duty cycle means the motor spins faster.

How to control speed and direction of DC motor using Arduino Nano 33 IoT

First, the DC motor uses high voltage that can damage the Arduino Nano 33 IoT, so we cannot connect the DC motor directly to it. We need a special hardware driver between the DC motor and the Arduino Nano 33 IoT. This driver has three main jobs:

  • Shield the Arduino Nano 33 IoT from high voltage.
  • Use the Arduino Nano 33 IoT’s signal to reverse the power supply and change the motor’s direction.
  • Boost the PWM signal (both current and voltage) from the Arduino Nano 33 IoT to manage the motor’s speed.
Arduino Nano 33 IoT control DC motor

There are many drivers for DC motors. In this tutorial, we will use the L298N driver.

Overview of L298N Driver

One L298N driver can work with two DC motors or one stepper motor. In this guide, we use it to control a DC motor.

L298N Driver Pinout

The image below shows the pin layout of the L298N driver.

L298N Driver Pinout

You can find a simple explanation for each pin in this Arduino - DC motor tutorial: https://arduinogetstarted.com/tutorials/arduino-dc-motor#content_about_l298n_driver

One L298N driver can control two DC motors separately.

  • Motor A is controlled by the pins IN1, IN2, ENA, OUT1, and OUT2.
  • Motor B is controlled by the pins IN3, IN4, ENB, OUT3, and OUT4.

How To Control the Speed of DC Motor via L298N Driver

Controlling the speed of a DC motor is easy. Just send a PWM signal to the ENA/ENB pins on the L298N. You can do this by:

  • Connect the Arduino Nano 33 IoT's digital output pin to the ENA/ENB pin on the L298N.
  • Use the analogWrite() function to send a PWM signal to the ENA/ENB pin. The L298N driver then boosts the current and voltage before sending the power to the DC motor.
analogWrite(PIN_ENA, speed); // Set PWM output for motor A analogWrite(PIN_ENB, speed); // Set PWM output for motor B

The speed value can be any number from 0 to 255. A speed of 255 makes the motor run as fast as it can, and a speed of 0 stops the motor.

How To Control the Direction of DC Motor via L298N Driver

You can control which way motor A turns by using the IN1 and IN2 pins. The table below shows how the signals on these pins affect the motor's direction.

IN1 pin IN2 pin Direction
HIGH LOW DC Motor A rotates in clockwise direction
LOW HIGH DC Motor A rotates in anticlockwise direction
HIGH HIGH DC Motor A stops
LOW LOW DC Motor A stops

Also, the table below is for DC motor B.

IN3 pin IN4 pin Direction
HIGH LOW DC Motor B rotates in clockwise direction
LOW HIGH DC Motor B rotates in anticlockwise direction
HIGH HIGH DC Motor B stops
LOW LOW DC Motor B stops

Let's learn how to create a program to control it. We'll use motor A as an example. Motor B works the same way.

  • Set motor A to spin clockwise.
digitalWrite(PIN_IN1, HIGH); digitalWrite(PIN_IN2, LOW);
  • Turn motor A to spin counterclockwise.
digitalWrite(PIN_IN1, LOW); digitalWrite(PIN_IN2, HIGH);

※ NOTE THAT:

If you connect the wires from the DC motor to the L298N driver the wrong way, the motor will move in the opposite direction. To fix it, swap the OUT1 and OUT2 pins.

How To Stop DC Motor

You can stop a DC motor in two ways.

  • Set the speed to 0.
analogWrite(PIN_ENA, 0);
  • Set both IN1 and IN2 pins to either a low level or a high level.
digitalWrite(PIN_IN1, HIGH); digitalWrite(PIN_IN2, HIGH);
  • Or
digitalWrite(PIN_IN1, LOW); digitalWrite(PIN_IN2, LOW);

How to control a DC motor using L298N driver.

Wiring Diagram

The L298N board has three jumpers. Remove them all before you connect any wires.

The wiring diagram between Arduino Nano and 33 IoT DC Motor L298N Driver

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT Code

Let's look at the code below that does these tasks one by one.

  • The Arduino Nano 33 IoT makes the DC motor go faster.
  • The Arduino Nano 33 IoT reverses the DC motor's direction.
  • The Arduino Nano 33 IoT makes the DC motor go slower.
  • The Arduino Nano 33 IoT stops the DC motor.
/* * 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 }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Take off all three jumpers from the L298N board.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and send the code to the Arduino Nano 33 IoT board.
  • Watch the DC motor and you will notice:

- The motor speeds up and spins at full power for 2 seconds.

- The motor then changes direction.

- The motor spins backward at full power for 2 seconds.

- The motor slows down.

- The motor stops for 2 seconds.

  • This sequence repeats over and over again.

※ NOTE THAT:

This guide shows you how to change the speed of a DC motor. If you want to set an exact speed (rotations per second), you need to use a PID controller with feedback from an encoder.

How to Control two DC Motors using L298N Driver

Coming soon.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!