The Arduino Inventor's Guide (40 page)

FIGURE 8-8:
A geared hobby motor consists of a basic motor and a gearbox

A gearbox essentially converts mechanical rotations into torque. This gearbox has a 48:1 gear reduction, which means 48 rotations of the motor equal one rotation of the output shaft. This slows down the motor by a factor of roughly 1/48 and results in a multiplication of the torque by a factor of 48. Basically, the output speed is slower, but the torque is a lot higher.

BUILD THE DRAWBOT PROTOTYPE

Now, let’s wire this up to see how it all works. You’ll connect just one motor for now to test the H-bridge motor driver, so you’ll use only one half of the dual H-bridge board.
Figure 8-9
shows how the board and Arduino should be wired. The board is split horizontally, with the top half controlling Motor A and the bottom half controlling Motor B, though the power pins are used for both motors. Connect 5 V and GND from the Arduino to the power rails on the breadboard, and make sure to add a jumper wire to connect the two 5 V rails of the breadboard so you can use either rail to give power; this will save you from crossing too many wires and keep your board neat.

FIGURE 8-9:
H-bridge test circuit

Starting from the top left of the H-bridge, connect 5 V to the top two pins, VM and VCC. VM controls the power for the motors, and VCC controls the power for the chip. Next, use a jumper wire to connect one of the chip’s GND pins to the GND rail of the breadboard. There are three pins available for ground on the H-bridge, as you can see in
Figure 8-7
, and you can use any of these.

Next you’ll connect the motor. The motor has two wires: red and black. The orientation of the wires doesn’t actually matter, but for consistency connect the red wire to the pin labeled A01 and the black wire to pin A02.

The remaining pins on the left side are those for controlling the second motor and another GND pin, so leave them for now. The pins on the top right of the H-bridge breakout board are for the signal wire connections for Motor A. The topmost pin, labeled PWMA, controls the motor’s speed. Connect this to pin 11 on the Arduino. (Remember
that pins 3, 5, 6, 9, 10, and 11 all have PWM capability and can be used with the
analogWrite()
function.)

The next two pins, labeled AIN2 and AIN1, are used to control the direction and drive of Motor A, which you can do by setting these pins to different combinations of HIGH and LOW.
Table 8-1
shows the combinations. Connect AIN2 to Arduino pin 12 and AIN1 to Arduino pin 11.

TABLE 8-1:
H-bridge motor controller functions

AIN1

AIN2

FUNCTION

HIGH

LOW

Clockwise

LOW

HIGH

Counterclockwise

HIGH

HIGH

Electronic brake (see note)

LOW

LOW

Stop/coast

NOTE

Setting both pins HIGH will employ
electronic braking.
The two wires of the motor are essentially shorted together, causing the motor’s spinning to stop abruptly. By contrast, setting both to LOW would just stop actively spinning the motors, meaning the wheels would coast to a stop rather than stopping deliberately.

Lastly you’ll need to disable the STBY pin. As mentioned earlier, this H-bridge IC has a standby pin that allows you to put the chip into a low-power sleep mode, which is useful for applications where power consumption is a concern. For this project, you don’t need this feature, so you’ll disable it. This chip is designed with STBY as an
active low
input. This means that when this pin is LOW, it goes into standby mode. To disable standby, you’ll connect this pin directly to 5 V on the power rail.

PROGRAM THE DRAWBOT

Let’s start the sketch with a little test. This simple example will spin the motor clockwise slowly for 1 second, change directions and spin counterclockwise quickly for 1 second, and then stop for 1 second before starting again. Open the Arduino IDE, and copy the code in
Listing 8-1
into your window. When you’re done, click
Upload
and watch what happens!

LISTING 8-1:
H-bridge motor controller example of speed and direction control


const byte
AIN1 = 13;
  
const byte
AIN2 = 12;
  
const byte
PWMA = 11;
  
void
setup
()
  {
    
pinMode
(AIN1,
OUTPUT
);
    
pinMode
(AIN2,
OUTPUT
);
    
pinMode
(PWMA,
OUTPUT
);
  }
  
void
loop
()
  {
    
//set direction to clockwise

   
digitalWrite
(AIN1,
HIGH
);
    
digitalWrite
(AIN2,
LOW
);

   
analogWrite
(PWMA, 50);
    
delay
(1000);
    
//set direction to counterclockwise

   
digitalWrite
(AIN1,
LOW
);
    
digitalWrite
(AIN2,
HIGH
);
    
analogWrite
(PWMA, 255);
    
delay
(1000);
    
//brake

   
digitalWrite
(AIN1,
HIGH
);
    
digitalWrite
(AIN2,
HIGH
);
    
delay
(1000);
  }

The sketch starts with a new data type:
const byte

. The keyword
const
is used to declare a
constant
, which is like a variable but with a value that can’t be changed again later in the code. Thus, constants are useful for declaring things that will stay the same throughout the code, like pin numbers or configurations. In this case, these constants define the pin numbers that control the H-bridge. Since the pin numbers are numbers between 0 and 13, you can define these constants as the data type
byte
.

NOTE

Most of the time it doesn’t matter too much whether you use a constant or a variable, but constants use less memory on the Arduino, so it’s good practice to use them when you can, and you may see them used in other people’s examples online. Also, while it’s not a hard-and-fast rule, constant names are typically in all capital letters.

Next, you set the pins as outputs, and then set the direction you want the motor to spin using two
digitalWrite()
functions

on pins
AIN1
and
AIN2
. The first loop block sets
AIN1
to
HIGH
and
AIN2
to
LOW
, which spins the motor clockwise. To set the speed, you use an
analogWrite()
function

on the
PWMA
pin. You may recall from
Project 5
that you can use
analogWrite()
to set an analog pin to a PWM value from
0
to
255
; the value given here,
50
, is relatively slow. The motor will spin for 1 second because of
delay(1000)
, and the next loop block changes directions with two more
digitalWrite()
functions

. Here the sketch simply swaps which pin is
HIGH
and which is
LOW
, sets the speed to
255
with
analogWrite()
, and adds another
delay(1000)
to set it to spin for 1 second.

The last part of the sketch sets both
AIN1
and
AIN2
to
HIGH

, with another
delay(1000)
. This applies an electronic brake and stops the motor for 1 second before the loop begins again and repeats the pattern.

Using this code as an example, you can now control both the speed and direction of a motor with just three lines of code! But we can make this even simpler. Let’s clean up the code by using custom functions.

Create a Custom Function

At the moment, every time you want to control the motor you’re using three lines of code: two to control the direction and one to set the speed. In this section you’ll make a custom function that will take just one number to determine both the direction and the speed of the spin. This number can be anything between
-255
and
255
and will spin the motor clockwise if the number is positive and counterclockwise if it’s negative. Add the code in
Listing 8-2
to the very end of your sketch.

LISTING 8-2:
Custom function to set the motor speed of Motor A

  
void

setMotorA(

int
motorSpeed)
  {

   
if
(motorSpeed > 0)
    {
      
digitalWrite
(AIN1,
HIGH
);
      
digitalWrite
(AIN2,
LOW
);
    }

   
else if
(motorSpeed < 0)
    {
      
digitalWrite
(AIN1,
LOW
);
      
digitalWrite
(AIN2,
HIGH
);
    }

   
else
    {
      
digitalWrite
(AIN1,
HIGH
);
      
digitalWrite
(AIN2,
HIGH
);
    }

   
analogWrite
(PWMA,
abs
(motorSpeed));
  }

Name the function
setMotorA()

. This function uses a number as a single argument named
motorSpeed

to set the motor’s speed. First, a simple
if()
statement determines whether the number is positive or negative by checking whether
motorSpeed
is greater or less than zero. If
motorSpeed
is positive

, the
if()
statement sets the direction pins so that the motor spins clockwise. If it’s negative

, an
else if()
statement sets the direction pins to spin the motor counterclockwise. If it’s neither positive nor negative (that is, if it’s
0
), a final
else
statement

sets both direction pins
HIGH
to apply the brake and stop the motor.

The line at

uses the
abs()
mathematical function to find the
absolute value
of
motorSpeed
. The
analogWrite()
function sets the speed of the motor, but it works only with values from
0
to
255
. The
abs()
function makes sure that the positive part, or
absolute magnitude
, of
motorSpeed
is used to set the speed.

Clean Up the Code

Now, let’s clean up the
loop()
with this new function. You can see in
Listing 8-3
that the
loop()
code is much shorter and easier to read. Make these changes to the
loop()
in your sketch and upload it to your board. It should behave the same way as before. Now, if you want to set the motor to a different speed or direction, you can do it with just a single line of code!

Other books

Unicorn Uproar by Carolyn Keene
Wildfire by Sarah Micklem
Nightmare City by Nick Oldham
Bermuda Heat by P.A. Brown
El general en su laberinto by Gabriel García Márquez
The Physique 57 Solution by Tanya Becker, Jennifer Maanavi
Through the Maelstrom by Rebekah Lewis