# Interfacing L293d motor ic with Arduino uno

Hi csaipraveenkumar,

Try this. ;)

``````/*  L298N_Dual_H_Bridge

This program controls 2 separate DC motors using the L298N
H Bridge Chip.

*THE SUBROUTINES WORK AS FOLLOWS*

motorA(mode, speed)
% replace A with B to control motor B %

mode is a number 0 -> 3 that determines what the motor
will do.
0 = coast/disable the H bridge
1 = turn motor clockwise
2 = turn motor counter clockwise
3 = brake motor

speed is a number 0 -> 100 that represents percentage of
motor speed.
0 = off
50 = 50% of full motor speed
100 = 100% of full motor speed

EXAMPLE
Say you need to have motor A turn clockwise at 33% of its
full speed.  The subroutine call would be the following...

motorA(1, 33);

*/

#define ENA 5  //enable A on pin 5 (needs to be a pwm pin)
#define ENB 3  //enable B on pin 3 (needs to be a pwm pin)
#define IN1 2  //IN1 on pin 2 conrtols one side of bridge A
#define IN2 4  //IN2 on pin 4 controls other side of A
#define IN3 6  //IN3 on pin 6 conrtols one side of bridge B
#define IN4 7  //IN4 on pin 7 controls other side of B

void setup()
{
//set all of the outputs
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}

void loop()
{
motorA(1, 50);  //back

delay(5000);  //let motor A run for 5 seconds

motorA(3, 50);  //brake motor A with 100% braking power
delay(3000);

motorB(1, 50);  // back

delay(5000);  //let motor B run for 5 seconds

//have motor A turn counter-clockwise at 15% speed
motorB(3, 50);
delay(5000);
}

//******************   Motor A control   *******************
void motorA(int mode, int percent)
{

//change the percentage range of 0 -> 100 into the PWM
//range of 0 -> 255 using the map function
int duty = map(percent, 0, 100, 0, 255);

switch(mode)
{
case 0:  //disable/coast
digitalWrite(ENA, LOW);  //set enable low to disable A
break;

case 1:  //turn clockwise
//setting IN1 high connects motor lead 1 to +voltage
digitalWrite(IN1, HIGH);

//setting IN2 low connects motor lead 2 to ground
digitalWrite(IN2, LOW);

//use pwm to control motor speed through enable pin
analogWrite(ENA, duty);

break;

case 2:  //turn counter-clockwise
//setting IN1 low connects motor lead 1 to ground
digitalWrite(IN1, LOW);

//setting IN2 high connects motor lead 2 to +voltage
digitalWrite(IN2, HIGH);

//use pwm to control motor speed through enable pin
analogWrite(ENA, duty);

break;

case 3:  //brake motor
//setting IN1 low connects motor lead 1 to ground
digitalWrite(IN1, LOW);

//setting IN2 high connects motor lead 2 to ground
digitalWrite(IN2, LOW);

//use pwm to control motor braking power
//through enable pin
analogWrite(ENA, duty);

break;
}
}

//******************   Motor B control   *******************
void motorB(int mode, int percent)
{

//change the percentage range of 0 -> 100 into the PWM
//range of 0 -> 255 using the map function
int duty = map(percent, 0, 100, 0, 255);

switch(mode)
{
case 0:  //disable/coast
digitalWrite(ENB, LOW);  //set enable low to disable B
break;

case 1:  //turn clockwise
//setting IN3 high connects motor lead 1 to +voltage
digitalWrite(IN3, HIGH);

//setting IN4 low connects motor lead 2 to ground
digitalWrite(IN4, LOW);

//use pwm to control motor speed through enable pin
analogWrite(ENB, duty);

break;

case 2:  //turn counter-clockwise
//setting IN3 low connects motor lead 1 to ground
digitalWrite(IN3, LOW);

//setting IN4 high connects motor lead 2 to +voltage
digitalWrite(IN4, HIGH);

//use pwm to control motor speed through enable pin
analogWrite(ENB, duty);

break;

case 3:  //brake motor
//setting IN3 low connects motor lead 1 to ground
digitalWrite(IN3, LOW);

//setting IN4 high connects motor lead 2 to ground
digitalWrite(IN4, LOW);

//use pwm to control motor braking power
//through enable pin
analogWrite(ENB, duty);

break;
}
}
//**********************************************************
``````

It's waste of time chatting, try the code and tell me if it's working, it should of curse just connect right the electronics.

Hi, Can you post a picture of your project please? What is the part number of your motor, or specs?

Tom..... :)

Hi,
Domino60

I would like thank you for ur valuable help by posting the code .I tried many ways to make the motor rotate anticlockwise but none of them worked.I could do it with ur code ,once again heartfelt thanks for the help you have done by posting the code.

hi,
I am doing the ir sensor interfacing with the arduino uno, in this process i have encountered a problem with the ir sensor ,the problem is it is not detecting the presence of the objects which are at a distance greater than 1cm, i am attaching a pic of the ir sensor circuit ,please do have look at it and help me to increase the range and tell me where the problem lies
The components in the ir sensor circuit are

1. ir transmitter
3. lm567 IC
4. 1k resistors (3)
5. 10kresistors (1)
6. 22k resistors(1)
7. 470 resistor(1)
8. BC 547 transistor(1)
9. 1uf,63v capacitor(1)
10. 0.1uf capacitor(2)
11. 0.2uf capacitor(1)

Are praveen1550 and csaipraveenkumar the same person?

JohnLincoln: Are praveen1550 and csaipraveenkumar the same person?

You beat me to it John: exact same post as this one

Csaipraveenkumar, I think you are going to add a lot of confusion by adding a new/unrelated problem to this topic, whilst the original problem is still unresolved.

JohnLincoln:
Csaipraveenkumar,
I think you are going to add a lot of confusion by adding a new/unrelated problem to this topic, whilst the original problem is still unresolved.

… and the new problem is already the subject of another thread, but maybe he didn’t like the answer. I asked the mods to investigate.

hi, No sir csaipraveenkumar and praveen1550 are not the same he is a old friend of mine he is doing the project with me ,so he has written to u abt the problem but he didnt inform it to me so unknowingly i have posted once again sorry,for it ,it is a matter of lack of communication b/w me and my friend so kindly apologize me.

Domino60, I'm sure that csaipraveenkumar won't be able to run your sketch successfully, as he doesn't have an Arduino PWM output connected to the L293D enable pin.

Hi to all, the previous problem about the interfacing the l293d ic to arduino uno and the problem to not able to rotate the motor anticlockwise had been solved by Domino60 , i got my motor rotated in anticlockwise direction,so taking that into consideration i have put forward my another problem regarding ir sensor sorry gentle man please forgive me its not that i didnt like ur answer .So kindly apologize me and look into my problem

But the new issue with the IR has nothing to do with the motor problem, and in any case already has another thread going, so best to answer it there.

hi, Yes sir i got it and i wouldn't let this happen next time which creates a problem to u and disturbs u once again sorry for the thing which happened ,as i am new to this forum (posting questions) so it has happened .I apologize for it .

moderator: Merged related projects from same IP.

JohnLincoln,

Yea, maybe but he still could use the code as an example, there is how to use step by step clockwise and anti.

You just need to read carefully the code and you/other will understand how it's working, I done my own modification as i need.

What I mean and how i mostly work and how people actually learn is from examples, give them an example and they give you back full project. People can't learn if you don't learn them 1st (or give them at least a single example).

;)

Keep the good work guys (csaipraveenkumar, praveen1550) don't let anyone to stop you, no mater what.

robtillaart

moderator: Merged related projects from same IP.

Same network, school, facility, home. You can have same IP while your network have more than 2 devices. 8)

Domino60: robtillaart

Same network, school, facility, home. You can have same IP while your network have more than 2 devices. 8)

The issue was actually that two different user names (csai*praveenkumar, **praveen*1550) posted the exact same question (the IR one), and the "thank you" response to you, D60, for the motor problem, wasn't from the name who asked the question. He's clearly using 2 names but forgets to switch back....

Domino60: Keep the good work guys (csaipraveenkumar, praveen1550)

You think there are two Praveens on the same IP asking the exact same question?

yes sir,there are two persons with the name praveen(my self) the first person is csaipraveenkumar(my self) and the other person is chpraveen(praveen1550 my friend ). He is doing the project along with me,actually when we got the problem regarding the ir sensor i had to leave the room in which he stays (due to some personal work),so after that he has posted in the forum regarding the problem ,but after returning from my work i opened the forum and posted our problem,the reason was he didnt inform me about his post in the forum ,so due to lack of communication this happened (i.e posting about the same problem twice). so please forgive me its a lack of communication b/w us and ill see that it wont happen next time

My apologies for thinking it was one person with two logins....

JimboZA

Even if there was 2 different persons with the same question but they could not know each other, you never know how people can have the same question in the same period of time in different parts of the earth/world.

Human race is something unexpected, so expect the unexpected :D