Efficient utilization of microcontroller pins is crucial when working on Arduino projects, especially when pins are limited. A common scenario arises when controlling motor direction using an L293D motor driver. Typically, an H-bridge like the L293D requires two Arduino pins to control the motor's direction. However, with some ingenuity, we can reduce this to a single pin by integrating an NPN bipolar transistor.
This article explains how to interface the Arduino with an L293D motor driver and an NPN transistor to control motor direction while saving an output pin.
Understanding the L293D Motor Driver
The L293D is a dual H-bridge motor driver IC widely used for controlling DC motors and stepper motors. Each H-bridge requires two input pins from the Arduino to determine the direction of the motor, as shown below:
- IN1 & IN2: Used to control the direction of Motor A.
- IN3 & IN4: Used to control the direction of Motor B.
Standard Motor Direction Control:
- Setting IN1 = HIGH and IN2 = LOW rotates the motor forward.
- Setting IN1 = LOW and IN2 = HIGH rotates the motor backward.
The Problem
If you’re controlling multiple motors, the number of pins required can quickly add up, leaving fewer pins for other peripherals or sensors. For each motor, two Arduino pins are typically reserved for direction control.
The Solution: Using an NPN Transistor
We can save one Arduino pin per motor by using an NPN bipolar transistor as an electronic switch. The idea is to control one of the motor driver inputs (e.g., IN2) indirectly using the transistor while directly controlling the other (e.g., IN1) with the Arduino.
How It Works
- One Arduino pin (D4) directly connects to IN1 of the L293D.
- The second motor input, IN2, is connected to the collector of an NPN transistor.
- The emitter of the transistor is connected to the ground.
- A pull-down resistor ensures that the input to IN2 remains LOW when the transistor is off.
- The base of the transistor is connected to the same Arduino pin as IN1 through a current-limiting resistor. This creates an inverted signal for IN2, ensuring complementary logic to IN1.
Logic Table
Arduino Pin (IN1) | Transistor (IN2) | Motor Direction |
---|---|---|
HIGH | LOW | Forward |
LOW | HIGH | Reverse |
Circuit Diagram
Here’s the schematic for interfacing an Arduino with an L293D motor driver and an NPN transistor:
Components Required:- Arduino Board (e.g., Uno or Nano)
- L293D Motor Driver IC
- NPN Bipolar Transistor (e.g., 2N2222 or BC547)
- 1 kΩ Resistor (for the transistor base)
- 10 kΩ Resistor (pull-down for IN2)
- DC Motor
- External Power Supply (if needed)
Code Implementation
Below is the Arduino code to control the motor direction with a single output pin:
#define MOTOR_PIN 4 // Define the pin controlling the motor #define ENA_PIN 5; void setup() { Serial.begin(9600); pinMode(MOTOR_PIN, OUTPUT); pinMode(ENA_PIN, OUTPUT); } void loop() { // Rotate motor forward digitalWrite(MOTOR_PIN, HIGH); delay(2000); // Run for 2 seconds // Rotate motor backward digitalWrite(MOTOR_PIN, LOW); delay(2000); // Run for 2 seconds }
Working Explanation
- When
MOTOR_PIN
is set to HIGH:- IN1 receives a HIGH signal directly.
- IN2 is pulled LOW via the NPN transistor, creating a forward direction for the motor.
- When
MOTOR_PIN
is set to LOW:- IN1 receives a LOW signal directly.
- IN2 is set to HIGH through the pull-down resistor and the absence of base current in the transistor, creating a reverse direction for the motor.
This setup inverts the signal for IN2 using the transistor, eliminating the need for an additional Arduino pin.
Advantages of This Approach
1. Pin Efficiency: Each motor requires only one Arduino pin, freeing up more pins for other components.2. Cost-Effective: NPN transistors are inexpensive and readily available.
3. Simple Logic: The complementary logic simplifies control without additional software complexity.
4. Scalability: Ideal for projects involving multiple motors and limited microcontroller pins.