0

members

Easy Basics: Project 033a L298N Dual H-Bridge motor driver module, one or two DC motors 6V

of Lex C in UNO

Basics: Project 033a

Project name: L298N Dual H-bridge motor driver module, one or two DC motors 6V

Attachments: OneDCmotorWithoutSpeedcontrolsketch1,

 OneDCmotorWithSpeedcontrolsketch2,

 TwoDCmotorsWithoutSpeedControlsketch3,

 TwoDCmotorsWithSpeedControlsketch4

Tags: Arduino, DC motor, L298N Dual H-bridge motor driver module

In this project, you needed these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2.L298N H-Bridge motor driver module (as you see they can be a little bit different but have the same working principle) 1pc

3.Arduino IDE ( you can download it from here  )

4.Jumper cables

5. DC motor 6V 2 pcs

6. External motor power supply 1 pc

7. External L298N IC logics power supply (optional, depending on external motor power supply) 1 pc

General

We will learn how to connect L298N H-Bridge motor driver module to Arduino board and use it with 2 DC motors. This is the most complete guide about this module.

Understanding the DC motor

Controlling motors with an Arduino is more complicated than just controlling LEDs for a couple of reasons. First, motors require more current than the Arduino’s output pins can supply, and second, motors can generate their own current through a process called induction, which can damage your circuit if you don’t plan for it. However, motors make it possible to move physical things, making your projectsmuch more exciting. They’re worth the complications! Moving things takes a lot of energy. Motors typically require more current than the Arduino can provide. Some motors require a higher voltage as well. To start moving, and when it has a heavy load attached, a motor will draw as much current as it can. The Arduino can only provide 40 milliamps (mA) from its digital pins, much less than what most motors require to work.

HOW DOES DC MOTOR WORKS

In a simple DC motor there are two main components, the “stator” and the “armature”. The stator is a permanent magnet and provides a constant magnetic field. The armature, which is the rotating part, is a simple coil. The armature is connected to a DC power source using a 2-piece ring installed around the motor shaft, these ring sections are called “commutator rings”. The two pieces of the commutator rings are connected to each end of the armature coil. Direct Current of a suitable voltage is applied to the commutator rings via two “brushes” that rub against the rings. When DC is applied to the commutator rings it flows through the armature coil, producing a magnetic field. This field is attracted to the stator magnet (remember, opposite magnetic polarities attract, similar ones repel) and the motor shaft begins to spin. The motor shaft rotates until it arrives at the junction between the two halves of the commutator. At that point the brushes come into contact with the other half of the commutator rings, reversing the polarity of the armature coil (or coils, most modern DC motors have several). This is great because at this point the motor shaft has rotated 180 degrees and the magnetic field polarities need to be reversed for the motor to continue rotating. This process repeats itself indefinitely until current is removed from the armature coils. The motor described is referred to as a brushed DC motor because it has brushes. Brushes however create many problems – they can start to wear over time, they rub against the motor shaft and they can even cause sparking as the motor gets older. Better quality DC motors are the brushless variety. Brushless motors use a more complex arrangement of coils and do not require a commutator. The moving part of the motor is connected to the permanent magnet. Because they do not contain brushes these brushless motors will last longer and are also much quieter than brushed DC motors. DC motors are specified by the voltage level at which they operate. Common hobbyists motors run at 6 Volts or 12 volts DC. To reverse the direction in which the DC motor rotates you simply reverse the polarity of the DC current that you apply to it. Changing the speed however is a different story. One method of changing the speed of a DC motor is to simply reduce its supply voltage. While this will work to some degree it is actually not a very good method of controlling motor speed as lowering the voltage will also lower the torque that the motor is capable of producing. Also, once the voltage drops below a certain point the motor will not rotate at all. A far better method of controlling DC motors is to use pulse width modulation or PWM. 

A DC motor can generate electricity when the shaft is spun around. When you stop supplying energy to a motor, it will continue to spin, because ithas inertia. When it’s spinning, it will generate a voltage in the opposite direction than the current you gave it. A more versatile way of controlling a DC motor is to use a circuit called an “H-Bridge”. An “H-Bridge” is an arrangement of transistors that allow you to control both the direction and speed of the motor. Today we’ll examine a very common H-Bridge module based around the L298N IC.

Understanding the L298N H-Bridge motor driver module

H-Bridge's are typically used in controlling motors speed and direction, but can be used for other projects such as driving the brightness of certain lighting projects such as high powered LED arrays. An H-Bridge is a circuit that can drive a current in either polarity and be controlled by Pulse Width Modulation (PWM).

PWM is a technique which allows us to adjust the average value of the voltage that’s going to the electronic device by turning on and off the power at a 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.

If you’ve read up on controlling LED’s with your microcontroller you probably have already run into PWM as it’s also a good method of controlling the brightness of an LED. With PWM the DC motor is sent a series of pulses. Each pulse is of the full voltage that the motor can handle so a 6-volt motor will be sent 6 volt pulses while a 12-volt motor will be sent 12 volt pulses. The width of the pulses are varied to control the motor speed, pulses with a narrow width will cause the motor to spin quite slowly. Increasing the pulse width will increase the speed of the motor, as illustrated below.

In order to stop the motor completely you just stop pulsing it, essentially sending it zero volts. To run it at full speed you send it the full voltage, again without pulsing it.

The Arduino has a function called “analogWrite” which is used to drive any of its PWM-capable outputs (the Arduino Uno has 6 digital outputs (pins 3,5,6,9,10,11) that are also capable of PWM).DC motors will last much longer and be more reliable if controlled through PWM.

Let's check how to reverse DC motor direction by changing polarity and how to change their speed using pulse width modulation with using a very common circuit configuration called an “H-Bridge”.

An H-Bridge circuit contains four switching elements, transistors or MOSFETs, with the motor at the center forming an H-like configuration. Using transistors also allows you to control the motor speed with PWM, as described above.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.

 

In the first diagram we can see four switches which are all in the open or “off” position. In the center of the circuit is a DC motor. If you look at the circuit as it is drawn here you can distinctly see a letter “H”, with the motor attached in the center or “bridge” section – thus the term “H-Bridge”.

If we close (i.e. turn on) two of the switches you can see how the voltage is applied to the motor, causing it to turn clockwise.

Now we’ll open those switches and close the other two. As you can see this causes the polarity of the voltage applied to the motor to be reversed, resulting in our motor spinning counterclockwise.

This is pretty simple but effective. In fact if all you need to do is design a circuit to drive the motor full-speed in either direction you could actually build this as shown here, using a 4PDT (4 Pole Double-Throw) center-off switch. But of course we want to control the motor using an Arduino, so an electronic circuit where the switches are replaced by transistors is what we need.

L298N H-Bridge

While you can use discrete transistors to build an H-Bridge there are a number of advantages in using an integrated circuit. A number of H-Bridge motor driver IC’s are available and all of them work in pretty much the same fashion. One of the most popular is the L298N.

The L298N is a member of a family of IC’s that all have the designation “L298”. The difference between the family members is in the amount of current they can handle. 

The L298N actually contains two complete H-Bridge circuits, so it is capable of driving a pair of DC motors. This makes it ideal for robotic projects, as most robots have either two or four powered wheels. The L298N can also be used to drive a single stepper motor, however we won’t cover that configuration in this article.

L298N schematics:

L298N Dual H-bridge motor driver module can control up to 2 DC motors or one bipolar stepper motor.

L298N logic table:

L=Low, H=High, X=Don’t care, Z=High impedance, Vin=Input voltage, Vdrop=Voltage drop

Note: There is a voltage drop between the input voltage and the L298N' motor outputs, you can expect a 1V to 3V drop depend on the input voltage and current draw.

Specifications:

  • Double H bridge Drive Chip: L298N
  • Logical voltage: 5V DC
  • Drive voltage: 5V-35V DC
  • Logical current: 0-36mA 
  • Drive peak current: up to 2A MAX single bridge (one DC motor)
  • Max power: 25W
  • Dimensions: 43 x 43 x 26mm
  • Weight: 26g

See more info about L298N IC here and here.

Signals and connections of L298N H-Bridge motor driver module

L298N integrated circuit pinouts:

You’ll find a few different versions of L298N boards but they all operate in the same fashion. The board contains an L298N mounted on a heatsink, a 5 volt voltage regulator to “optionally" provide power for logic circuits, supporting diodes and capacitors and connectors as follows:

  • Logic inputs for each H-Bridge circuit
  • Power supply inputs for the motor power supply
  • An optional 5 Volt power input for the logic circuits.
  • Outputs for each DC motor

L298N H-Bridge motor driver module jumpers:

CSA – This is the “current sensing” function for Motor A. If the jumper is in this function is ignored. Most of the time you’ll leave this jumper in place.

CSB – The “current sensing” function for Motor B. If the jumper is in this function is ignored. Most of the time you’ll leave this jumper in place.

UR1 – Input 1 pull-up resistor. You will usually leave this in place, which enables a 10k pull-up resistor for the input.

UR2 – Input 2 pull-up resistor.

UR3 – Input 3 pull-up resistor.

UR4 – Input 4 pull-up resistor.

5v-EN – This is the only jumper that you need to really pay attention to. When this jumper is in place it enables the boards internal 78M05 5V voltage regulator chip, supplying logic power from the motor power supply. When this jumper is enabled you will NOT need to supply 5 V to the 5 V input terminal. When the jumper is removed you will need to supply 5 V to the 5 V input terminal.

L298N H-Bridge motor driver module pins:

Motor A (or OUTA, OUTB) (or OUT1, OUT2) - DC motor A output (+ , -) or Stepper motor (+, -)

Motor B (or OUTC, OUTD) (or OUT3, OUT4) - DC motor B output(+ , -)

VMS (or VCC) (or VDD) - Motors external power supply from 5V to 35 V

GND - common ground

+5V - DC 5V output or input for the logic circuits

ENA (or EN1) - DC motor A enable jumper. Connect to PWM output for DC motor A speed control (Leave this in place when using a stepper motor).

ENB (or EN2) - DC motor B enable jumper. Connect to PWM output for DC motor B speed control (Leave this in place when using a stepper motor).

IN1 (or N1) (or INA) – Input 1 for DC motor A

IN2 (or N2) (or INB) – Input 2 for DC motor A

IN3 (or N3) (or INC) – Input 3 for DC motor B

IN4 (or N4) (or IND) – Input 4 for DC motor B

The L298N Dual H-bridge motor driver module has an onboard (internal) 5V regulator which is either enabled or disabled using a 5V regulator jumper. If the exernal motor supply voltage is up to 12V we can enable the onboard (internal) 5V regulator and the +5V pin can be used as output, for example for powering your Arduino board. But if the external motor power voltage is greater than 12V we must disconnect the 5V jumper because those voltages will cause damage to the onboard (internal) 5V regulator. In this case the +5V pin will be used as input as we need connect it to a 5V power supply in order the L298N IC to work properly.


Please note that L298N IC makes a voltage drop of about 2V. So for example, if we use a 12V power supply, the voltage at motors terminals will be about 10V, which means that we won’t be able to get the maximum speed out of our 12V DC motor.

Wiring

The following picture shows the needed connections with the Arduino Uno:

1. One DC motor without speed control (constant maximum speed only). Set the jumper on ENB and +5V on the motor driver board and the 5V_EN (5V) regulator jumper too.

2. One DC motor with speed control. Remove jumper on ENB and +5V from the motor driver board and set the 5V_EN (5V) regulator jumper on the motor driver board. You need to use PWM pin for speed control (can be 3,5,6,9,10,11)

 

3. Two DC motors without speed control (constant maximum speed only). Set the jumpers on ENA and +5V, ENB and +5V on the motor driver board and the 5V_EN (5V) regulator jumper too.

4. Two DC motors with speed control. Remove jumpers on ENA and +5V,  ENB and +5V from the motor driver board and set the 5V_EN (5V) regulator jumper on the motor driver board. You need to use PWM pin for speed control (can be 3,5,6,9,10,11)

Code

Speed control

The speed of the motors can be adjusted by connecting PWM outputs from your microcontroller to the ENA and ENB input pins on the motor driver board. The ENA pin controls Motor A and the ENB pin controls Motor B. When these pins are HIGH, power is output to the motor. By using PWM, you are turning power on and off very quickly to adjust the speed of the motor. The longer the PWM duty cycle is, the faster the motor will turn. We recommend always using a PWM duty cycle of 90% or less.

Direction control

The direction that the motors turn is controlled using the IN1, IN2, IN3 and IN4 input pins on the motor driver board. Connect these pins to digital outputs on your microcontroller. To make Motor A go forward, set IN1=HIGH and IN2=LOW. To make Motor A go backward set IN1=LOW and IN2=HIGH. The same method is used to control Motor B: set IN3=HIGH and IN4=LOW to o forward and set IN3=LOW and IN4=HIGH to go backwards. Note that “forward” and “backwards” refer to the direction of the motors themselves. If your motors does not rotate in the expected direction, reverse the motor polarity by swapping the green screw terminals for Motor A + and – and/or Motor B + and -.

Stopping

To remove power from the motors, simply set ENA=LOW for Motor A and ENB=LOW for Motor B. This will result in the motors stopping slowly and naturally from friction. To perform a quick braking operation, set ENA=LOW, IN1=LOW and IN2=LOW for Motor A and ENB=LOW, IN3=LOW and IN4=LOW for Motor B. The motors will come to an instant stop. Here are some handy tables to show the various modes of operation.

Step by Step instruction

  1. Do wiring.
  2. Connect DC motor 1 to the DC motor A output or DC motor 2 to DC motor B output on the motor driver module. 
  3. If you are using two DC motors ensure that the polarity of the motors is the same on both inputs (+ and -). Otherwise you may need to swap them over when you set both motors to forward and one goes backwards.
  4. DC motor 1 controlled by IN1 & IN2 , DC motor 2 controlled by IN3 & IN4.
  5. ENA and ENB (jumpers ENA+5V and ENB+5V set on the motor driver board) are HIGH by default that makes the motor running at a constant maximum speed. In order to control the speed of the motor, the jumpers ENA+5V and ENB+5V must removed from ENA and ENB, then connect them to PWM capable digital output of a Arduino board (In certain circumstances, ENA and ENB connected to a non-PWM digital output of the Arduino board in order to stop the motor slowly)
  6. If you do use the internal voltage regulator you’ll have to supply the motor power supply with at least 7.5 V. External motor power supply needs to be a bit higher voltage than the actual motor requirements. This is due to the internal voltage drop in the transistors that form the H-Bridge circuit. The combined voltage drop is 1.4 V, so if you are using 6 V motors you’ll need to give the board 7.4 V, if you have 12 V motors then your motor supply voltage will need to be 13.4 V.
  7. Connect your external motor power supply – the positive (red wire) to pin +12V on the motor driver module and negative (black wire) to pin GND close to pin +12V. If your external motor power supply is up to 12V you can leave in the 5V regulator jumper on the motor driver module and 5V will be available from pin +5V on this module. This can be feed to your Arduino board 5V pin to power it from the motors’ external power supply. Don’t forget to connect Arduino board GND to pin GND on the motor driver module as well to complete the circuit.
  8. Open Arduino IDE.
  9. Plug your Adruino Uno board into your PC and select the correct board and com port
  10. Open up serial monitor and set your baud to 9600 baud
  11. Verify and upload the the calibration sketch to your Adruino Uno

Summary

We have learnt how to connect L298N H-Bridge motor driver module to Arduino board and use it with 2 DC motors. When the DC motor starts it will use 2-4 times more current then nominal and if the reverse done fast -  even more. This jump of current can be minimised or eliminated if the DC motor will have a smooth speed increase during specific start up time.  

Libraries:

  • No libraries required for this project

Sketch:

  • See attachments on the begining of this project description.
  • Sketch 1. DC motor will rotate to one side first, then will rotate to another side and stop in the loop with constant maximum speed.
  • Sketch 2. We can control the speed of the DC motor through PWM. Function used analogWrite (pin,number) where number = 0...255. DC motor will have minimum , medium , high rotation and stop.
  • Sketch 3.Both DC motors rotate to one direction then stop, rotate to another direction then stop.
  • Sketch 4. Both DC motors rotate to both directions with smooth speed increase


Other projects of Lex C
Easy Basics: Project 057a ACR122U NFC Reader of Lex C, Not selected 17-01-2018

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 19-09-2017
Viewed: 1022 times