Motor speed control is very important in robots and where the motor used. In this project, we will control the speed of DC motors with Arduino Nano. In this way, you will learn how to drive the motor and adjust the speed in projects where you use DC motors.

First of all, we can say that DC motors are elements that draw a lot of power. Therefore, we cannot drive a DC motor with the input and output pins of the microcontroller. We can suggest 3 ways to drive a DC motor;
- Making our own motor driver with MOSFETs,
- Make your own driver module with a motor driver like L298D,
- Use motor driver modules
We are going to make the 3rd of these three ways for now. There are various modules for this, we will use the L298N module. Let’s examine this module and talk about the working principle.
L298N Motor Drive Module

The module is usually shown on the right side. With this module, we can drive two DC motors. We think you will use two DC motor circuit designs and the software will do accordingly. The circuit connection should be as follows;
- We will connect our DC motors to the terminals specified with “MotorA” and “MotorB”.
- If we do not want to change the speed (we will), we will connect the “ENA” and “ENB” pins to the “+ 5V” pin above with a jumper.
- For speed control, we will connect the “ENA” and “ENB” pins to the controller’s PWM pins.
- We will connect the power supply (LiPo battery in this project) to the “+ 12V” and “GND” inputs to power the motors.
- The “+ 5V” part is the output of the regulator located in the module and provides 5V voltage to the motor driver. We can use this output to power our control circuit (Arduino Nano).
- The pins “IN1”, “IN2”, “IN3” and “IN4” will control the direction of the motors with the signals from the controller (Arduino Nano).
The “ENA” and “ENB” pins on the module enable us to turn the output power on and off. Thus, we can adjust the speed of the motors by sending a PWM signal to these pins. “ENA” controls “MotorA” while “ENB” controls “MotorB”. These situations are shown in the table below.
There are two ways to stop the motors as shown in the table. The first one is to cut off the power to the motors and make them stop freely. The second is to give the engine power to stop fast. We will use the first stop function in this project.
Schematic Design
As shown below, we used one 11.1V LiPo battery in our circuit. We connected this battery to the motor driver module and did not use an extra power supply by connecting the 5V output to the Arduino Nano. We’ve connected the motors to the output. Lastly, we connected the control pins.

Motor Speed Control Function
The function we use in the software performs the operations in the table shown below at the specified speeds. The SC variable used in the table specifies the variable Speed Constant.
Function Code
After installing the following code on Arduino Nano, if the motors are connected to a vehicle then we power the circuit, and they will do the following operations respectively;
- 1000ms forward at 150 speed,
- 500ms right at 200 speed,
- 1000ms forward at 150 speed,
- 1000ms backward at 150 speed,
- 500ms left at 200 speed,
- 1000ms backward at 150 speed,
- and it will go back to the beginning and repeat the same movements…
Speed is between 0 and 255.
/********* Connections **********/ // Motor connections #define IN1 9 #define IN2 8 #define IN3 7 #define IN4 6 #define ENA 10 #define ENB 5 /******** Variables ********/ // Direction variables #define F 1 #define FR 2 #define R 3 #define BR 4 #define B 5 #define BL 6 #define L 7 #define FL 8 #define STOP 0 #define SPEED_CONSTANT 0.2 void setup() { for (int i = 5; i <= 10; i++) { pinMode(i, OUTPUT); } } void motor(int dir, int speed); void loop() { // 1000ms forward @150 speed motor(F, 150); delay(1000); // 500ms right @200 speed motor(R, 200); delay(500); // 1000ms forward @150 speed motor(F, 150); delay(1000); // 1000ms backward @150 speed motor(B, 150); delay(1000); // 500ms left @200 speed motor(L, 200); delay(500); // 1000ms backward @150 speed motor(B, 150); delay(1000); } /* dir: F- Forward FR- Forward Right R- Right BR- Backward Right B- Backward BL- Backward Left L- Left FL- Forward Left STOP- Stop speed: min: 0, max: 255 */ void motor(int dir, int speed) { if (dir == F) { // Left motor forward with full speed digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, speed); // Right motor forward with full speed digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENB, speed); } else if (dir == FR) { // Left motor forward with full speed digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, speed); // Right motor forward with speed*SPEED_CONSTANT digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENB, speed * SPEED_CONSTANT); } else if (dir == R) { // Left motor forward with full speed digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, speed); // Right motor backward with full speed digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); analogWrite(ENB, speed); } else if (dir == BR) { // Left motor backward with full speed digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, speed); // Right motor backward with speed*SPEED_CONSTANT digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); analogWrite(ENB, speed * SPEED_CONSTANT); } else if (dir == B) { // Left motor backward with full speed digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, speed); // Right motor backward with full speed digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); analogWrite(ENB, speed); } else if (dir == BL) { // Left motor backward with speed*SPEED_CONSTANT digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, speed * SPEED_CONSTANT); // Right motor backward with full speed digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); analogWrite(ENB, speed); } else if (dir == L) { // Left motor forward with full speed digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, speed); // Right motor backward with full speed digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENB, speed); } else if (dir == FL) { // Left motor forward with speed*SPEED_CONSTANT digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, speed * SPEED_CONSTANT); // Right motor forward with full speed digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); analogWrite(ENB, speed); } else if (dir == STOP) { // Left motor stop analogWrite(ENA, 0); // Right motor stop analogWrite(ENB, 0); } }
In this project, we try to teach you how to do motor speed control. I hope it’s useful. Don’t forget to comment, and stay healthy.

Comments
Post a Comment