Driving Motors With The Arduino and an L239D

Being able to control a DC motor using an Arduino is an important skill to learn if you want the robot to go anywhere. Before going into the wiring diagram and example code. Let’s first take a look at the L239D motor driver chip.

The L239D:

The L239D is a dual h-bridge motor driver chip. Before we go into what a dual h bridge is, let’s find out what a single h-bridge is:

In simple terms, a h-bridge lets you change the direction which current flows through a load. In our case the load is a motor. Imagine that vcc is the positive connection of the battery pack and gnd is the negative connection.

If you connect the battery pack directly to the motor, the motor will spin one way. If you swap the positive and negative, the motor will spin the opposite direction. Feel free to try this out for yourself. The whole idea of a h-bridge is mimicking this changing of polarity without having to physically swap the wires.

With the configuration of power and 4 switches you can change the way the current flows through the motor by changing which two switches you close.

The l239D contains two of these H-bridges hence why it’s called a dual h-bridge driver chip.

 

Wiring Diagram:

 

Getting The Motors Turning:

In order to get the motors doing anything, we need to upload a program that will change the states of pins D2, D3, D4, and D5. These are the four motor control pins.

You can’t be sure which way around your motors are wired. So the first example is to figure out whether or not you need to swap around your purple motor wires:

Example 1 – Motor Polarity Test:

 

If either one (or both) of your motors is not turning forward, you just need to swap the purple wires for that particular motor and it should start rotating the correct way.

 

Adding In Speed Control Using PWM:

PWM or Pulse Width Modulation is a method of reducing the average power delivered by an electrical signal or source. For us, it’s an easy way to control the speed of the motor using the Arduino.

Add in the following wires shown below to enable speed control:

 

Example 2 : Motor Speed Controlled Via Ultrasonic Sensor Value

Now that we have PWM added. Let’s take a look at a full example that will change the motor speed depending on the ultrasonic sensor value

 

 

 

 

 

 

 

This entry was posted in Tutorial and tagged , , , . Bookmark the permalink.

1 Response to Driving Motors With The Arduino and an L239D

  1. Pingback: Blanchardstown Library 3 Day Workshop | RoboJAM!

Leave a Reply

Your email address will not be published. Required fields are marked *