Arduino Tutorials:Arduino and DC motor using L293D

Published by sandesh on

L293D is an IC, which is used to control 2 wheel and 4 wheel robots. It has 16 pins. L293D divides power into two parts, which can be controlled according to the input pins. The following code makes the robot move forward, backward, left, right. Connect the robot as shown in the figure below.

Code:

const int motorPin1  = 1;
const int motorPin2  = 2;  
const int motorPin3  = 3;
const int motorPin4  = 4;
void setup(){
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}


void loop(){

digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
delay(2000);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(2000);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
delay(2000);
}

Code explaination:

Here, we take 4 inputs from pin 1,2,3,4; as motorpin1, motorpin2, motorpin3, motorpin4, and declare them as outputs. This all should be written in void setup. Then, we set outputs to HIGH and LOW according to the code above, to make the robot move forward, backward, right and -left. The L293D pins must be connected as shown below.

The Robot will look as follows:

Previous Post:Reading analog data from temperature sensor (LM35)

Also Read:Top 10 cool projects with Arduino/raspberry pi


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.