Arduino Obstacle Avoiding Robot Using an L298N Motor Driver

Introduction: Arduino Obstacle Avoiding Robot Using an L298N Motor Driver

About: I make Arduino projects.

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.

Step 1: Watch a Short Video

Step 2: Prepare Your Components and Tools

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.

Step 3: Solder Jumpers to Terminal A&B of Each Motor

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.

Step 4: Assemble Your Robot's Parts

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.

Step 5: Wire Everything Up

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.

Step 6: Upload a Code to the Robot's Brain

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

Step 7: Take Good Care of the 18650 Cells

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.

Step 8: Modify the Code However You Want!

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!

Step 9: Done!

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!

Be the First to Share

    Recommendations

    • Halloween Contest

      Halloween Contest
    • Organization Contest

      Organization Contest
    • 3D Printed Student Design Challenge

      3D Printed Student Design Challenge

    Comments