DC Motor Control with Arduino Uno

Welcome back to SmartBloggingšŸŒ.
To start with Robotics, first thing you should learn is controlling different types of motors. So will start again with basics of motors and how to control motor by arduino as per your requirement. To start with basics of board, please go through my previous blog Led- blinking-with-arduino-boards.


Arduino with DC Motor

Components Required:


Circuit Diagram:

The maximum current of an Arduino I/O port is 20mA but the drive current of a motor is at least 70mA. Therefore, we cannot directly use the I/O port to drive the current; instead, we can use an L293D to drive the motor. L293D L293D is designed to provide bidirectional drive currents of up to 600mA at voltages from 4.5V to 36V. It's used to drive inductive loads such as relays, solenoids, DC and bipolar stepping motors, as well as other high-current/high-voltage loads in positive-supply applications.
See the figure of pins below. L293D has two pins (Vcc1 and Vcc2) for power supply. Vcc2 is used to supply power for the motor, while Vcc1, for the chip. Since a small-sized DC motor is used here, connect both pins to +5V. If you use a higher power motor, you need to connect Vcc2 to an external power supply.
Arduno Uno with DC motor using L293D circuit diagram
The Enable pin 1,2EN of the L293D are connected to 5V already, so L293D is always in the working state. Connect pin 1A and 2A to pin 9 and 10 of the control board respectively. The two pins of the motor are connected to pin 1Y and 2Y respectively. When pin 10 is set as High level and pin 9 as Low, the motor will start to rotate towards one direction. When the pin 10 is Low and pin 9 is High, it rotates in the opposite direction.

Coding:

  • Build the circuit as shown above.
  • Copy the below code into software IDE.
---------------------------------------CODE-------------------------------------
//DC Motor Control
//The DC motor will begin rotating left and right, and its speed will vary accordingly.
//Email:blog2vin@gmail.com
//Website:www.blog2vin.com
const int motorIn1 = 9;  //attach to one of the pin of the motor
const int motorIn2 = 10;  //attach to another pin of the motor

void setup()
{
  pinMode(motorIn1,OUTPUT);  //initialize the motorIn1 pin as output 
  pinMode(motorIn2,OUTPUT);  //initialize the motorIn2 pin as output 
}

void loop()
{
  clockwise(200);         //rotate clockwise 
  delay(1000);            //wait for a second
  counterclockwise(200);  //rotate counterclockwise
  delay(1000);            //wait for a second
}

//The function to drive motor rotate clockwise
void clockwise(int Speed)
{
  analogWrite(motorIn1,Speed);  //set the speed of motor
  analogWrite(motorIn2,0);      //stop the motorIn2 pin of motor
}

//The function to drive motor rotate counterclockwise
void counterclockwise(int Speed)
{
  analogWrite(motorIn1,0);      //stop the motorIn1 pin of motor
  analogWrite(motorIn2,Speed);  //set the speed of motor
}
----------------------------------------END-----------------------------

Arduino with DC motor code

  • If "Done uploading" appears at the bottom of the window, it means the sketch has been successfully uploaded.
  • Now, the blade of the DC motor will begin rotating left and right, in a speed that varies accordingly.
Contact us for any queries. Enjoy playing with arduino boards and wait for the next postšŸ‘.




Comments

Post a Comment