Interfacing DC-Motor with Arduino Board
In this project, we will demonstrate how to interface a DC motor with an Arduino board. The Arduino will control the motor’s rotation direction.
Hardware Required:
- Arduino board (e.g., Arduino Uno)
- DC motor
- Motor driver module (e.g., L293D)
- External power supply for the motor (if required)
- Jumper wires
Circuit Diagram: Connect the components as follows:
Note: The motor driver module (e.g., L293D) is used to control the direction and speed of the DC motor. The specific connection details may vary depending on the motor driver module used. Please refer to the datasheet or documentation of your motor driver module for the appropriate connections.
Connection Table: Here’s a connection table for the motor driver module, DC motor, and Arduino:
Motor Driver Module Pin | Arduino Pin |
IN1 IN2 GND VCC | Digital Pin (e.g., 2) Digital Pin (e.g., 3) GND 5V |
Below is the Arduino code to control the DC motor:
//www.projectkitsandparts.com
// Define the motor driver pins connected to the Arduino
const int motorPin1 = 2;
const int motorPin2 = 3;
void setup() {
// Set the motor driver pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop() {
// Rotate the motor in one direction
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
delay(2000);
// Stop the motor
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(2000);
// Rotate the motor in the other direction
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
delay(2000);
// Stop the motor
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
delay(2000);
}
Code Explanation:
- The motorPin1, motorPin2 variables are declared and set to the Arduino pins connected to the motor driver module.
- In the setup() function, the motor driver pins and the enable pin are set as outputs using the pinMode() function.
- The loop() function is where the motor control happens.
- The motor is rotated in one direction by setting motorPin1 to HIGH and motorPin2 to LOW.
- The program waits for 2 seconds using delay(2000).
- The motor is stopped by setting both motorPin1 and motorPin2 to LOW, and the enable pin is set to 0.
- Another 2-second delay is added.
- The motor is rotated in the other direction by setting motorPin1 to LOW and motorPin2 to HIGH.
- The program waits for 2 seconds using delay(2000).
- The motor is stopped again.
- The loop repeats, rotating the motor in different directions and stopping it at intervals.
Code Manual:
- Connect the Arduino board to your computer via USB.
- Open the Arduino IDE (Integrated Development Environment) on your computer.
- Create a new sketch and copy-paste the code provided into the IDE.
- Make sure the correct Arduino board and port are selected under the “Tools” menu.
- Click the “Upload” button to compile and upload the code to the Arduino board.
- Connect the motor driver module’s input pins (IN1 and IN2) to the specified Arduino pins.
- Connect the motor driver module’s GND pin to the Arduino’s GND pin.
- Connect the motor driver module’s VCC pin to the Arduino’s 5V pin.
- Connect the DC motor to the motor driver module’s output terminals.
- When the code runs, the DC motor will rotate in one direction for 2 seconds, stop for 2 seconds, rotate in the other direction for 2 seconds, and then stop again.
Note: Ensure that the motor driver module is capable of handling the voltage and current requirements of your DC motor. Refer to the datasheet or documentation of your motor driver module for any specific usage instructions or precautions.
You have successfully interfaced a DC motor with an Arduino board.