Arduino Obstacle Avoiding Robot Using an L298N Motor Driver

by Paul Vertor in Circuits > Arduino

7208 Views, 9 Favorites, 0 Comments

Arduino Obstacle Avoiding Robot Using an L298N Motor Driver

F22E2828-62FC-48A3-AFE6-AED338FAD47F.gif
45DE1D97-BA1B-4BDB-9C43-1095F5C3677F.jpeg

In this project, I'm going to walk you through how to make a simple Arduino Flinching Robot step by step for those who prefer the L298N H-bridge Motor Driver to the Motor Shield.

Before we get started, have you ever wondered whether to choose an Arduino Motor Shield or an L298N Motor Driver for your project? I personally prefer the L298N Motor Driver because we can then use more of our Arduino's pins to add some more functions to our robot!

That being said, let's have a quick look at the comparison below to choose the best fit.

1. When to use Arduino Motor Shield L293D?

Advantages

Firstly, we can save some space on the robot chassis, plus much fewer jumpers are used for wiring up when using the Motor Shield compared to the L298N Motor Driver.

Secondly, we can also set motors' speed and define maximum speed by software using the Adafruit Motor Shield library.

Lastly, a single DC power supply can be used for the Arduino and motors, after that the shield will handle the rest to ensure your servos and motors will operate properly.

Disadvantages

Some soldering skills are required to solder pin headers onto the shield. Also, the shield uses an Arduino timer for servos' operation, which may cause Timer Conflicts between the "AFMotor.h" (i.e. Adafruit Motor Shield) library and other libraries. Last but not least, once you head towards 1A you'll probably want to put a heatsink on the motor driver, otherwise you will get a thermal failure, possibly burning out the chip (I knew it since mine was burnt out).

Specifications

You can check more information regarding the shield's specifications here.

2. When to use L298N Motor Driver?

Advantages

It requires only some wiring - no soldering. Though there is no pre-built library for this module, we can set motors' speed by using Arduino's PWM (Pulse Width Modulation) pins and some simple lines of code.

The concern for a burnt-out chip is much less here since each channel on the module can deliver up to 2A to the DC motor plus a big chunky heat sink can protect the module from a possible thermal failure (The heat sink make the driver look cooler too by the way).

Since we can use all Arduino's pins (on the Motor Shield the number of pins available for use is reduced by more than a half), there are more pins available for more functionalities!

Disadvantages

The Arduino and the Motor Driver placed separately will occupy some more space on the robot chassis. An extra breadboard may also be necessary for power and ground connections. Furthermore, in most cases, two separate DC power supplies are required for Arduino and motors.

You may also encounter some weird servo's operation if the battery supply is not able to provide constant power, causing problems with Arduino resets and resulting in servo jitters or not working. But don't worry just yet - I will address an easy way to solve this later!

Specifications

The followings are the specification from lastminuteengineers.com:

"The L298N motor driver IC actually has two input power pins: ‘Vss’ and ‘Vs’.
From Vs pin, the H-Bridge gets its power for driving the motors which can be 5 to 35V. Vss is used for driving the logic circuitry which can be 5 to 7V. And they both sink to a common ground named ‘GND’."

See more information on how to use the Motor Driver here.

I will also show you the key features of this module that is necessary for the project along with the guide.

Watch a Short Video

Arduino Obstacle Avoiding Robot (L298N Version) | Quick Overview and Test

Prepare Your Components and Tools

A6D16B22-2360-466C-A31D-674F063957B9.jpeg
70A3EADC-201E-495E-9C16-6D5874B909B3.jpeg
D9C2295D-8F30-4C73-80DD-87B2F4B91FB4.jpeg
4772A325-EE5C-42E2-A700-E4A6DA20A0C8.jpeg

For this project, you will need:

  1. an HC-SR04 ultrasonic sensor
  2. an ultrasonic sensor holder
  3. a servo motor (I used an SG90 Tower Pro hobby servo)
  4. a three-wheel robot chassis
  5. a small breadboard
  6. a 7805 voltage regulator
  7. a 9 Volts battery (power supply for your Arduino)
  8. a 9 V battery DC jack
  9. two 3.7 V 18650 Li-ion batteries (power supply for your motors)
  10. a two-slot 18650 battery holder with switch
  11. an Arduino Uno R3
  12. a type A/B USB cable (to upload code to your Arduino)
  13. an L298N Motor Driver
  14. two rubber wheels
  15. a swivel caster wheel/ a ball caster (it's all your choice, in my case I used a home-made caster for my robot)
  16. two 3-6 V DC plastic gearbox motors (a.k.a. 'TT' motors)
  17. Some Male-Male and Male-Female jumpers to hook up the components

There are many options to mount the components on the robot chassis, including the use of hot glue, double-sided tape, or screws. In my case, I used double-sided tape and screws to either assemble components or attach them to the robot chassis.

Here are some extra tools you MAY need:

  • a hot glue gun
  • a scissor
  • a screwdriver
  • a multimeter (if you want to precisely measure the voltages of your batteries - I will talk about this later)
  • solder, soldering iron, and conventional sponge
  • a 18650 3.7 V Li-ion battery charger (necessary for long term use)

Note that some quick and easy soldering is still necessary if you want a firm and reliable connection between jumpers and the 3-6 V DC TT motors, providing a steady and satisfying robot operation. To be honest, on the first try I didn't bother soldering those connections at all, because I thought that it wouldn't significantly affect the motors' performance. But hey - I was too naive then, it took some failures and sufferings for me to eventually realize that the soldering DID matter, otherwise you will not be happy with your robot.

The soldering tools I mentioned above are just the minimum requirements for soldering by the way. You can check the basic tools and materials for soldering as well as how to solder here.

Solder Jumpers to Terminal A&B of Each Motor

4A0F8F60-0841-47BE-9A0A-30F6033F3B93.jpeg
4F2FA904-4E15-4EC8-ABC6-9121853A7F9C.jpeg
0D586B7B-4F04-4589-BAAB-4E2DF98FA94E.jpeg
B2D704DB-7B68-4D6E-849C-DD663952E131.jpeg

If you don't want to solder, you can try searching for motors with wire previously soldered to it. Otherwise, do some quick and easy soldering similarly to wires soldering as shown in this guide (i.e. see the "how to solder wires" part in that guide). The only difference here is that instead of soldering two wires together, you will solder a wire to a motor. Note that the wire colours should be distinguishable for each terminal of a single motor as well as consistent for both of the motors (as shown in the last photo).

Here is a summary of the soldering steps:

  • Clean the tip of the soldering iron by wiping it on a damp sponge
  • Tin the tip of the iron with solder
  • Tin the tip of the wire with solder
  • Tin one of the motor leads with solder
  • Hold the tinned wire on top of the motor lead
  • Touch the soldering iron to both of them to melt the solder evenly
  • Once cooling down the harden solder will establish a firm connection between that motor lead and the wire
  • Repeat the same process for the rest terminals of the two motors

There are two terminals on a 3-6 V DC TT motor. From now on let's call them terminal A and B. The tricky thing is that each terminal can either be A or B depending on how you place the motor on the robot's chassis. But the rule is simple: after you have attached the motor to the chassis, whichever terminal on the top is A, the other one below is B. Identifying whether a motor's side is A or B is an important process that will make sure you will connect the motor's wires to the L298N Motor Driver correctly, ensuring that it will spin the robot's wheel in the desired direction (either in the FORWARD direction or in the BACKWARD direction).

For now, you only need to remember where the terminal A is and where the terminal B is for each motor of the two motors in the project.

Assemble Your Robot's Parts

B911CDCD-B853-4992-BAE2-C82BA341A3AE.jpeg
D025792D-8FF8-4CFD-AD79-005D545498E4.jpeg
034761EA-5DA8-4D3B-970F-42BB9E04B824.jpeg
DD96736D-8528-448C-980E-68C0F5E75F3B.jpeg
BA5CCCE2-F555-4392-A9B3-3A2EEDAEF1FC.jpeg
D3FFD860-9A5F-4D3B-AF05-7D1ECDC2040E.jpeg
ACD75D0B-D185-4FB9-83CA-25C846E43206.jpeg
B7829D84-1451-467F-9F93-9F3E8EC03E13.jpeg
75047531-041C-430D-A23A-EAFCDD8AE4E1.jpeg
41B14816-651A-4255-8B2E-39A9D98ADD7B.jpeg

Before hooking everything up, we will fix every component on the chassis to form the shape of your robot. Use any methods in your favour (e.g. hot glue, double-sided tape, screws) to attach the components to the robot's body. See the photo instructions with notes above if you would like to follow my assembly.

Since I used a single-layer chassis with limited space on the top surface, the two motors with wheels already put in, the caster, the battery holder, and the servo were fixed on the bottom surface. The ultrasonic sensor was attached to the sensor holder which was then screwed to the servo motor. Also, the Arduino Uno board and the Motor Driver module were placed on the tail of the robot, while the breadboard and the 9 V battery were put in the middle of the robot. Among the components, the battery holder was installed last to avoid overlapping on screws used to attach components on the top. Moreover, note that at this stage we haven't done anything to the 18650 cells yet. I suggest we just leave those there until you have some ideas on how to take good care of them.

Wire Everything Up

FlinchBot (1).png
C32CB431-F0E8-4E82-AE96-C4CE284B8381.jpeg
2F5AC293-0D78-4075-82EB-38CE7E5CD74E.jpeg
41CFD15C-5C24-4481-B8FD-F5452A81EE88.jpeg
5827EE43-88FE-4014-9A78-55F2A1C872AE.jpeg
0BFC7B90-1C5D-498E-BF48-E689C027D96E.jpeg
16FE1FF5-DB96-496B-97F8-2AD9A9C17EE5.jpeg
B4CFA2AD-F77C-42EB-89D7-F3A6B94033AB.jpeg
8652AD9D-CD3A-498A-8B2A-C63404CEA06C.jpeg

Now we have moved to an important step. These connections allow your Arduino to communicate with other components within your robot. The notes written in the photo instructions may help you.

Wire motors to the Motor Driver

If you take a look at the L298N Motor Driver Schematic it can be seen that there are four output terminals, including OUT1, OUT2, OUT3, and OUT4, respectively installed in a counter-clockwise direction on the module and used to provide electric currents to the motors.

Remember the terminal A and B of your motors mentioned at the beginning of this Instructable? Now is the time to connect the A wire of motor 1 (left motor) to OUT3, the B wire to OUT4, the A wire of motor 2 (right motor) to OUT1, and finally the B wire to OUT2.

In my case, purple is the colour for the A wire of the left motor, while blue is the colour of the B wire of the same motor, and vice versa for the right motor (blue for A and purple for B).

Connect Arduino 5 V and GND pins to the breadboard

These pins are used to create two common shared lines of 5V and GND on the breadboard, providing power to different components within the robot.

Power your Motor Driver

Take a look at the L298N Motor Driver Schematic again. You can see there are three terminals for powering your Motor Driver, including VCC, GND, and 5V. While the positive and negative wires of the battery holder are connected to VCC and GND respectively to supply power to the two motors, the 5V terminal is wired to the common 5V line on the breadboard to power the module's logic circuitry.

Wire the Motor Driver to the Arduino board

Also from the L298N Schematic above, we can see there are six input pins, which are ENA, IN1, IN2, IN3, IN4, and ENB from left to right. Since it is likely that there are jumpers in place of ENA and ENB, we only need to connect IN1-IN4 correspondingly to Digital pin number 4-7 (a.k.a. pin D4-D7). These pins are used to control the spinning direction of the motors going either forward or backwards. Specifically, IN4 drives the left motor forward, IN3 reverses that motor, IN2 set the forward direction for the right motor, and IN1 reverses it.

The pre-installed jumpers on ENA and ENB will basically set the motors to their maximum speed. If you want to control the motors' speed using the function 'analogWrite()', simply remove the pre-installed jumpers then connect ENA and ENB to PWM pins available on your Arduino and write any values to those pins ranging from 0 to 255.

Use the voltage regulator to provide the servo with a constant power

The voltage regulator has three pins including Input, Ground, and Output. In this case, it will take the 9 V input voltage from the Arduino's Vin pin and output a constant power of 5 V to the servo. Both the voltage regulator and the servo will sink to the common ground line on the breadboard.

The servo also has three pins which are VCC, GND, and the signal pin. VCC will be connected to the 'Output' pin of the voltage regulator as you have known, whereas the signal pin is connected to pin D9 on your Arduino board. Note that if you input 5 V to the 7805, the servo wouldn't work! So in case you are powering your Arduino with the USB cable, connect the VCC pin of the servo to the common 5 V line instead.

Wire the ultrasonic sensor to the Arduino Uno

The ultrasonic sensor has four pins which are VCC, Trigger, Echo and GND. It's a common practice to choose different wire colours for each pin. I've chosen orange for VCC, purple for Trigger, grey for Echo, and blue for GND. The module is powered by the common 5 V line and sinks to the same ground line with other components. The Trigger pin is connected to Arduino's pin A0 while the Echo pin is connected to pin A1.

Power your Arduino with the 9 V battery using the DC jack

Simply plug the jack to your Arduino's external DC port AND put the 18650 cells in the battery holder with the switch turned on to power your system. But hold on! Your robot's brain is currently empty and you need to put some code in it.

Upload a Code to the Robot's Brain

1B4B4652-D25B-4FBD-982E-DB34DA9A296E.jpeg

1. Download the Arduino IDE software

2. Download the code provided below

3. Download the "NewPing.h" library

  • In the Arduino IDE software, on the Menu tab, click Tools/ Manage Libraries...
  • Type "NewPing" in the Search bar then choose "Install"

4. Upload the code to your Arduino board

  • Connect the Arduino board to your PC via the USB cable
  • On the Menu tab, click Tools and choose "Arduino Uno" for the Board option
  • Click the 'Verify' symbol (looking just like a 'Tick') to compile the code
  • Click the 'Upload' symbol next to the 'Tick' symbol to upload some logic to your robot's brain

Take Good Care of the 18650 Cells

8BD13231-4ECA-4186-89D1-798AA1476CD3.jpeg

Before putting the 18650 cells in the battery holder to power the motors, it is important to check their remaining voltages beforehand as one of the measures NOT to accidentally reduce your batteries' life.

So what did I mean? If you want "healthy" batteries for long-term use, try not to either overuse or overcharge it. For instance, always maintain the voltage values ranging from 3.2 V to 3.9 V (though most cells can be charged up to a maximum value of 4.2 V). In order to do that, it is essential that you frequently check the remaining voltages of the cells used to power your robot and recharge them once the voltages dip below 3.2 V. For the cells I'm using, it normally takes about two hours to partially charge the cells up to roughly 3.9 V. The partial charging time may vary depending on different types of 18650 cells so please be aware of the time!

As shown in the photo, the remaining voltage of one of my 18650 cells is measured as 3.75 V so it is good to go! Now I can safely use it to power my robot.

You can read more on how to extend your batteries' life here.

Modify the Code However You Want!

FlinchingBot_Flowchart.png

We are almost there! Now I just would like to provide a little more information so that you can freely personalize your robot's logic.

So how does the robot think? If you look at the flowchart above, it can be seen that the robot will constantly check if there is any object in front of it within a distance of 50 centimetres. If there is nothing, it will keep going forward. Otherwise, it will back up to avoid colliding with the object in the front, then it will turn its head to look at both the right side and the left side to come up with a decision which direction it should turn to (either left or right). For example, if something were blocking on the right side at that time, it would turn left and vice versa. However, if both sides are just too narrow (i.e. distances of both sides are shorter than 30 centimetres), it will just simply turn around. After turning to whichever direction, it will continue going straight, closing an action loop.

Have you got it yet? Now enjoy modifying!

Done!

3E66DD5B-161A-4F2F-921E-FCFA913C5FCE.jpeg
989163AF-6F76-4337-B7FA-B5FC5BCA9BBE.jpeg

Congrats on making it here! Let's put the 18650 cells in the battery holder, turn on the switch, and plug that 9 V battery DC jack into your Arduino's external DC port and watch your robot start rolling its wheels!

If you have any questions that this Instructable didn't answer, feel free to ask in the comment section and I would be willing to answer all of them or even expand the Instructable to ensure it will fully cover all details in the making process. Also please do share your robot if you make one! I'd love to see it. Thank you for taking the time, enjoy making!