How To Control DC Motors With Arduino.

DC motors are widely used in a variety of projects and applications, from robotics to home automation. The Arduino microcontroller is a popular platform for controlling DC motors due to its ease of use and versatility. In this blog, we will discuss how to control DC motors with Arduino.

Firstly, let’s discuss the components required for controlling DC motors with Arduino. The list of components is as follows:

  • Arduino board
  • Breadboard
  • DC motor(s)
  • L293D motor driver IC
  • Jumper wires
  • Power supply (battery or external adapter)

The L293D motor driver IC is a popular choice for controlling DC motors with Arduino. It is a dual H-bridge IC that can drive two DC motors or one stepper motor. The H-bridge configuration allows the direction of the motor to be controlled by the Arduino.

The L293D motor driver IC has the following pin configuration:

Pin 1: Enable 1 (EN1)

Pin 2: Input 1 (IN1)

Pin 3: Output 1 (OUT1)

Pin 4: Ground (GND)

Pin 5: Ground (GND)

Pin 6: Output 2 (OUT2)

Pin 7: Input 2 (IN2)

Pin 8: Enable 2 (EN2)

Pin 9: Motor A output (MOTOR A)

Pin 10: Motor A output (MOTOR A)

Pin 11: Motor B output (MOTOR B)

Pin 12: Motor B output (MOTOR B)

Pin 13: Ground (GND)

Pin 14: Supply voltage (Vs)

Pin 15: Input voltage (Vss)

To control a DC motor with Arduino using the L293D motor driver IC, follow these steps:

Step 1: Connect the motor driver IC to the Arduino

Connect pin 1 (EN1) of the L293D IC to pin 9 (PWM) of the Arduino, pin 2 (IN1) to pin 8 of the Arduino, pin 3 (OUT1) to the positive terminal of the DC motor, pin 4 (GND) to GND of the Arduino, pin 5 (GND) to GND of the power supply, pin 6 (OUT2) to the negative terminal of the DC motor, pin 7 (IN2) to pin 7 of the Arduino, pin 8 (EN2) to pin 10 (PWM) of the Arduino, pin 9 (MOTOR A) to the positive terminal of the DC motor, pin 10 (MOTOR A) to the negative terminal of the DC motor, pin 11 (MOTOR B) to the positive terminal of the DC motor, pin 12 (MOTOR B) to the negative terminal of the DC motor, pin 13 (GND) to GND of the power supply, pin 14 (Vs) to the positive terminal of the power supply, and pin 15 (Vss) to 5V of the Arduino.

Step 2 (continued): Write the Arduino code

The code above sets up the three required pins as outputs and then sets the direction of the motor to forward by setting one input pin high and the other input pin low. The analogWrite() function is used to vary the speed of the motor, with a value of 255 providing maximum speed.

Here is an example code to rotate the DC motor in the opposite direction:

int enA = 9;

int in1 = 8;

int in2 = 7;

void setup() {

  pinMode(enA, OUTPUT);

  pinMode(in1, OUTPUT);

  pinMode(in2, OUTPUT);

}

void loop() {

  digitalWrite(in1, LOW);

  digitalWrite(in2, HIGH);

  analogWrite(enA, 255);

}

This code sets the direction of the motor to reverse by setting the opposite input pin high and the other input pin low.

Step 3: Upload the code to the Arduino

After writing the code, connect the Arduino to your computer using a USB cable, and upload the code to the Arduino using the Arduino IDE.

Step 4: Connect the power supply

Connect the power supply (battery or external adapter) to the breadboard and connect the positive and negative terminals to the appropriate pins on the motor driver IC.

Step 5: Test the DC motor

After connecting everything, power on the Arduino and the motor should start rotating in the desired direction. You can vary the speed of the motor by changing the value passed to the analogWrite() function.

Additional Points To Keep In Mind:

Always use a motor driver: A motor driver like the L293D IC is necessary to protect the Arduino from the high current and voltage required to drive a DC motor. Without a motor driver, the Arduino may be damaged.

Choose the correct power supply: Make sure to choose a power supply that can provide the correct voltage and current for your DC motor. Using a power supply that is too weak can result in the motor not spinning or spinning slowly, while using a power supply that is too strong can damage the motor.

Use PWM for speed control: The analogWrite() function in Arduino uses pulse width modulation (PWM) to vary the speed of the motor. By adjusting the duty cycle of the PWM signal, the speed of the motor can be controlled.

Reverse the motor direction: To reverse the direction of the motor, simply change the input pins that are set high and low in the Arduino code.

Add additional features: Depending on your project, you may want to add additional features like limit switches, encoders, or sensors to provide feedback to the Arduino and enable more advanced control of the motor.

Conclusion: 

Controlling DC motors with Arduino is a simple and effective way to control the direction and speed of a motor in a variety of applications. By following the steps outlined above and writing the appropriate code, you can easily integrate DC motors into your Arduino projects.

If you’re looking to enhance your understanding of Arduino, LearnTube offers an array of online courses to suit your needs. LearnTube provides a comprehensive learning experience through its dedicated learning app and WhatsApp bot. Whether you’re a beginner or an experienced learner, our platform offers a wide range of courses to cater to your needs. Browse our extensive selection of courses on our website to gain valuable insights.

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Top AWS Interview Questions & Answers 2024

If you've ever wanted to work in the cloud industry, now is your chance. With cloud computing platforms like Amazon Web Services (AWS) sweeping...

How Much Will I Earn as a Flutter Developer? The Ultimate Salary Guide for 2024

Flutter is a popular cross-platform mobile app development framework that is gaining immense popularity among developers worldwide. As the demand for Flutter developers continues...

Top Companies Hiring Flutter Developers in 2024

As the popularity of Flutter continues to rise, there is a growing demand for skilled Flutter developers in various industries. In 2024, there will...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!