Arduino DC Motor Control Tutorial - L298N | H-Bridge | PWM | Robot Car

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello danatoski here from howtomechatronics.com in this arduino tutorial we will learn how to control dc motors using arduino we will take a look at some basic techniques for controlling dc motors and make two examples through which we will learn how to control dc motors using the l298n driver and the arduino board at the end we will be able to build and control our own arduino robot car we can control the speed of a dc motor by simply controlling the input voltage to the motor and the most common method of doing that is by using pwm signal pwm or pulse width modulation is a technique which allows us to adjust the average value of the voltage that's going to the electric device by turning on and off the power at the fast rate the average voltage depends on the duty cycle or the amount of time the signal is on versus the amount of time the signal is off in a single period of time so depending on the size of the motor we can simply connect an arduino pwm output to the base of a transistor or a gate of a mosfet and control the speed of the motor by controlling the pwm output the low power arduino pwm signal switches on and off the gate at the mosfet through which the high power motor is driven on the other hand for controlling the rotation direction we just need to inverse the direction of the current flow through the motor and the most common method of doing that is by using an h-bridge an h-bridge circuit contains four switching elements transistors or mosfets with the motor at the center forming an h-lite configuration by activating two particular switches at the same time we can change the direction of the current flow thus change the rotation direction of the motor so if we combine these two methods the pwm and the h bridge we can have a complete control over the dc motor there are many dc motor drivers that have these features and the l298n is one of them the l298n is a dual h-bridge motor driver which allows speed and direction control of two dc motors at the same time the module can drive dc motors that have voltages between 5 and 35 volts with peak currents up to 2 amps let's take a closer look at the pin out of the l298n module and explain how it works the module has two screw terminal blocks for the motor a and b and another screw terminal broke for the ground pin the vcc for the motor and a 5 volts pin which can either be an input or output this depends on the voltage used at the motor vcc the module have an onboard 5 volts regulator which is either enabled or disabled using a jumper if the motor supply voltage is up to 12 volts we can enable the 5 volts regulator and the 5 volts pin can be used as output for example for powering our arduino board but if the motor voltage is greater than 12 volts we must disconnect the jumper because these voltages will cause damage to the onboard 5 volts regulator in this case the 5 volts pin will be used as input as we need to connect it to a 5 volts power supply in order the ic to work properly we can note here that the ic makes a voltage drop of about 2 volts so for example if we use a 12 volts power supply the voltage at the motor's terminal will be about 10 volts which means that we won't be able to get the maximum speed out of our 12 volts dc motor next are the logic control inputs the enable a and enable b pins are used for enabling and controlling the speed of the motor if a jumper is present on this pin the motor will be enabled and work at maximum speed and if we remove the jumper we can connect a pwm input to this pin and in that way control the speed of the motor if we connect this pin to a ground the motor will be disabled next the input 1 and input 2 pins are used for controlling the rotation direction of the motor a and inputs 3 and 4 for the motor b using these pins we actually control the switches of the h bridge inside the l298 and ic if input 1 is low and input 2 is high the motor will move forward and vice versa if the input 1 is high and input 2 is low the motor will move backward in case both inputs are the same either low or high the motor will stop the same applies for the inputs 3 and 4 and the motor b now let's make some practical applications in the first example we will control the speed of a motor using a potentiometer and change the rotation direction using a push button here's the circuit schematic so we need an l298n driver a dc motor a potentiometer a push button and an arduino board here's the arduino code so first we need to define the pins and some variables needed for the program in the setup section we need to set the pin modes and set the initial rotation direction of the motor in the loop section we start by reading the potentiometer value and then map the value that we get from it which is from 0 to 1023 to a value from 0 to 255 for the pwm signal or that's 0 to 100 duty cycle of the pwm signal then using the analog write function we send the pwm signal to the enable pin of the l298 board which actually drives the motor next we check whether we have pressed the button and if that's true we will change the rotation direction of the motor by setting the input 1 and input 2 states inversely the push button will work as toggle button and each time we press it it will change the rotation direction of the motor so once we have learned this now we can build our own arduino robot car here's the circuit schematic all we need is two dc motors the l298n driver an arduino board and a joystick for the control as for the power supply i chose to use three 3.7 volt lithium batteries providing total of 11 volts i made the chassis out of three millimeters thick plywood attached the motors to it using metal brackets attached wheels to the motors and in front and attach a swivel wheel now let's take a look at the arduino code and see how it works after defining the pins in the loop section we start with reading the joystick x and y axis values the joystick is actually made of two potentiometers which are connected to the analog inputs of the arduino and they have values from 0 to 1023 when the joystick stays in its center position the value of both potentiometers or the axis is around 512. we will add a little tolerance and consider the values from 470 to 550 as center so if we move the y-axis of the joystick backward and the value goes below 470 we will set the two motors rotation directions to backward using the four input pins then we will convert the declining values from 470 to 0 into increasing pwm values from 0 to 255 which is actually the speed of the motor similar if we move the y-axis of the joystick forward and the value goes above 550 we will set the motors to move forward and convert the readings from 550 to 1023 into pwm values from 0 to 255 if the joystick stays in its center position the motor speed will be zero next let's see how we use the x-axis for the left and right control of the car so again first we need to convert the axis readings into speed values from 0 to 255 for moving left we use this value to decrease the left motor speed and increase the right motor speed here because of the arithmetic functions we also use two additional if statements to confine the range of the motor speed from 0 to 255 the same method is used for moving the car to the right depending on the applied voltage and the motor itself at lower speeds the motor is not able to start moving and it produces a buzzing sound in my case the motors were not able to move if the value of the pwm signal was below 70. therefore using these two if statements i actually confined the speed range from 70 to 255 at the end we just send the final motor speeds or pwm signal to the enable pins of the l298n driver so that would be all for this tutorial and in my next video we will upgrade this arduino robot car by adding a bluetooth and radio devices for enabling smartphone and wireless control thanks for watching and for more tutorials and projects visit howtomechatronics.com
Info
Channel: How To Mechatronics
Views: 1,214,178
Rating: undefined out of 5
Keywords: Arduino Tutorial, DC Motor, Driver, L298N, Speed Control, H-Bridge, PWM, Robot, Car
Id: I7IFsQ4tQU8
Channel Id: undefined
Length: 9min 58sec (598 seconds)
Published: Tue Aug 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.