DC motors
DC motors (direct current motors) are some of the most commonly used components in electronics and Arduino projects, making them ideal for driving robots, toy cars, and other simple mechanisms. In this article, I will show you how to control a DC motor using the L293D motor driver.
We will make the motor rotate forward and backward 5 times, then perform forward and backward movements with slow and fast stops, and finally gradually vary the speed using PWM.
First, let's see why we need a motor driver. A motor driver allows us to control motors using Arduino or other boards, enabling direction changes and speed control without overloading the board's output pins. If you connect a motor directly to an Arduino, you risk overloading the pins, damaging them, and the motor will not work properly.
So, what is the L293D? The L293D is an integrated circuit used to control DC motors and stepper motors with the help of a microcontroller. It contains two H-bridges, can control two motors simultaneously, supports voltages up to 36 V and a current of 600 mA per motor, and includes built-in diode protection.
Required components:
Arduino Nano R3
Breadboard
L293D integrated circuit
DC motor (3–6 V)
Jumper wires
Connections:
To avoid confusion, I've included an image below showing all the connections.

Code:
#define ENABLE 5
#define DIRA 3
#define DIRB 4
int i;
void setup() {
// Setare pini ca ieșire
pinMode(ENABLE, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
Serial.begin(9600);
}
void loop() {
// --- Exemplu: înainte și înapoi
Serial.println("One way, then reverse");
digitalWrite(ENABLE, HIGH); // activare driver
for (i = 0; i < 5; i++) {
digitalWrite(DIRA, HIGH); // un sens
digitalWrite(DIRB, LOW);
delay(500);
digitalWrite(DIRA, LOW); // sens invers
digitalWrite(DIRB, HIGH);
delay(500);
}
digitalWrite(ENABLE, LOW); // dezactivare
delay(2000);
// --- Exemplu: rapid / lent
Serial.println("Fast / Slow example");
digitalWrite(ENABLE, HIGH);
digitalWrite(DIRA, HIGH);
digitalWrite(DIRB, LOW);
delay(3000);
digitalWrite(ENABLE, LOW); // oprire lentă
delay(1000);
digitalWrite(ENABLE, HIGH);
digitalWrite(DIRA, LOW);
digitalWrite(DIRB, HIGH);
delay(3000);
digitalWrite(DIRA, LOW); // oprire rapidă
delay(2000);
// --- Exemplu PWM: viteză mare → mică
Serial.println("PWM full then slow");
digitalWrite(DIRA, HIGH);
digitalWrite(DIRB, LOW);
analogWrite(ENABLE, 255);
delay(2000);
analogWrite(ENABLE, 180);
delay(2000);
analogWrite(ENABLE, 128);
delay(2000);
analogWrite(ENABLE, 50);
delay(2000);
analogWrite(ENABLE, 128);
delay(2000);
analogWrite(ENABLE, 180);
delay(2000);
analogWrite(ENABLE, 255);
delay(2000);
digitalWrite(ENABLE, LOW); // stop final
delay(10000);
}
