hi welcome to an Arduino step-by-step course by Robojax
Lesson 95
L293D Motor Shield
in this video we are going to learn how to control four dc motors that you might need to use for
robot car or any other application using l293
motor shield with Arduino this is Arduino uno motor shield I’m going to demonstrate and
show you how to use it with Arduino uno and also Arduino mega in both cases this shield can be used
now let me explain the module a module has external power source you will connect your
battery or power that you have at this point and then for each motor we can use two terminals
these two left terminals are labeled as m1 so this will be your motor one and then this is motor two
and this two terminal here is motor four and these two are motor three do not worry about the middle
wire here so two motors on this side two motors one and two here and three and four there
so maybe these two could be connected to front wheels and this will be for the rear wheel
whichever way you prefer the most important element is this jumper this is now removed
just connect this here and program it we call this we call this shield because it's just
shielding or covering this Arduino and it's being inserted from the top so it eliminates a
lot of extra wiring this is one of the best way to do prototyping or working some on projects
so all these wiring are already done you just need to connect these few wires otherwise there will be
a huge mess with connecting so many wires shield can be also inserted are attached to Arduino mega
make sure that you attach it to these pins because these are exact configuration as Arduino uno
so pin one is here at pin one and here every other pin for aligned
here is the datasheet for l293 and l293d this is a quadruple half h bridge so what it means is one
chip or one ic can control two motors fully or four motors in one direction which will be half
branch and it can work with 4.5 up to 36 volts and 600 milliampere for the uh per channel for
l293d so the maximum current is 600 milliampere a separate video explained this fully so I’m not
going to spend any time on explaining the chip here you're programming your Arduino you will
plug this in and make sure that this is removed in this case the Arduino will be powered via usb
and here is the wiring diagram if you need it
one motor will be connected at these terminals the second motor will be in here the third and fourth
will be connected like that and your power supply will be connected in here which will
be from 5 to 16 volts on the module we have l293d two pieces because one can control two motors so
we have two of this two so this will control two motors that are connected and here and this chip
l293d will control two models these two chips will be controlled by this other chip and we are not
worried how this functions because it will be just waste of time we just connected and it will work
but you need separate external power for the power because the this power will power your
motor and from this it will go to Arduino either Arduino nano or mega that you have it will power
this as well so you do need external power just connect one power here and it will be up
to 16 volts so the 16 volts will come and enter from this pin it will enter when you connect it
and it will power your Arduino as well but in order for this to work you must connect this chip
this jumper once you connect it the power will go to Arduino as well and we will power it up so you
will leave this as is and here two motors will be connected on this two motor in here the polarity
doesn't matter because you will switch it and it will change the direction of rotation so this
does not have a polarity it's up to you positive negative however you want correct but this is for
one motor another motor and the same way in here each motor can be whatever voltage that you have
whatever voltage is your of your motor if it is five volts then connect five volts here if your
motor is 12 volts connect 12 volts and it will work but pay attention that your motor cannot
draw or cannot consume more than 600 milliampere so your motor must have lower power consumption
than 600 milliampere each now how you calculate the power if your motor one is
600 then four times six two thousand four hundred million per power is needed so you will connect
a power adapter or battery or something that can supply uh two thousand four hundred million power
or two point four amperes your motor will not run properly now external power is connected
but the motors are not running because I did not put this jumper let me just put this jumper
now the motors will have power Arduino is powered up and they will run so
if if I remove this Arduino will not receive any power or do you know uno that's at the bottom
will not receive power so this will not run
now Arduino is powered up and all these will run if you found this tutorial helpful please thumb
up and also click on the subscribe on the right side in the lower corner I really appreciate it
let me now explain the code the first thing you need to do is to install the library first
while you are connected to the internet click on sketch include library and then manage libraries
wait for this to complete and here type adafruit
Motor Shield
and wait for it and you can see it says adafruit motor shield library by adafruit
so you will see it here right now and you will see install button in here click
to install and wait for the bar graph to complete it will be installed in my case it is installed
and it says installed then close this the library has some examples when you click on file
examples and adafruit motor shield these are the examples but
this is not ready for you to use directly so I’m providing you this code this line it says that use
adafruit library so this is including the library and here the fun begins in here this is from the
class we create an object for a model we call it Motor1 and we put number one so this number one
refers to the pins and connectivity for motor one and this is for motor two three and four
but this word you can change it for example you can put front left move front left complete word
front right and rear or front or back whatever you want you can change it if your application needs
different method conversion but for simplicity I just called it motor one two three and four
and this line just initializes the serial monitor if you want to print something and this
is just printing the text we don't do anything because library is taking care of everything
so we created object called motor one and the same way we created motor two three and
four and very simple first we set the speed of motor it says set speed and here we will enter
some value between 0 to 255 so that is different speed from 0 means stop 255
means maximum speed because 255 is a Arduino-based value and it's not intuitive
for many applications I created a function called speed and you enter the speed in percent
100 mean hundred percent if you want fifty percent just put fifty or ten whatever you
want and but you use it like this so this is a function called speed it takes the 50 and
converts it to appropriate value that is needed for this spot that you enter
and so once you set the speed for motor one then you take action motor one dot run and
then forward so this will send the motor forward and this line it says keep it for two seconds
at that forward direction with that speed so whatever you set it will continue until as long
as this is true or after this if you change it and here after two seconds motor two will turn on
with hundred percent it goes forward if you want to change it to backwards just say
backward and it will go to the backwards and the same way motor 3 so let's say
I want to set it to 70 forward and then motor 4
90 speed so
at this stage just motor one is running and all that motor other motors are stopped so motor two
is now running and then after two seconds motor three is running and after two seconds motor4
is running if you want to run all of them just remove this delay and all motors will run together
so when you want to release it or stop it just put motor number motor 1 for example dot run
release so this will release the motor from controlling and it will stop we do not have
proper brake here they have brake it's supposed to be fast brake but it doesn't work much and
that's it so this speed when you put 100 it comes to this function that I’ve written and the value
that you have that number let's say 80 or whatever it will come here to this function called map
that percent let's say you have 80 80 will come here and we say this value which is from 0 to 100
whatever it is map it or do the calculation for make the zero zero 100 255 and whatever number
is in between will be appropriately mapped and the result of this calculation will be returned back
when we say return it fills up this area with a proper value so that's it
so that's taking care of the value so you don't worry about 255 or anything just enter in terms
of percent so you know the exact speed and once you set everything just click to upload your code
and go to tools board manager from here select Arduino mega 256.
now here's the demonstration of controlling four motors I’ve said the code that one motor
will turn on and the other one and the other one and the other one so the motor starts from here
motor one two and then motor three and four and after that it will stop
so I’m removing this flag here and putting it on this side and the motor will start running
now motor 3 and then this is now motor 4 and
all of them will stop again it goes to motor 1 and then motor 2
3 and then 4.
now I’ve uploaded the code to Arduino mega let me power this up
the same code is now running with Arduino mega
please make sure to subscribe by clicking at the subscribe button in here thank you
thank you for watching tutorial from Robojax if you learned something and found this useful
please thumb up if you have comment or question please post it at the comment section below
I try to answer and reply and don't forget subscribe so you get updates on my upcoming videos