Chapter 2. Arduino for Robotics

With some of the basics of electricity, Arduino, and general robot building out of the way, we jump right in to some of the specific interfacing tasks that are needed to complete the projects in this book. In Chapter 1, the code examples use low-power components that can be connected directly to the Arduino (LEDs, potentiometers, R/C receivers, button switches, and so on). This chapter focuses on how to interface your Arduino to mechanical, electronic, and optical switches, as well as some different input control methods, and finally some talk about sensors.

First we discuss the basics of interfacing relays, transistors, and motor-controllers to the Arduino. We then discuss the various methods of controlling your Arduino—focusing on the popular methods of wireless control. Lastly, I give you my two cents about the many different types of sensors available for robotic use.

There are no code examples in this chapter, but the information presented is useful to understand the interfacing methods, control types, and sensors used throughout this book. Let's start by introducing some switching components that can enable the Arduino to control high-powered devices.

Interfacing Arduino

Because the Arduino can supply only around 40ma of current through any one of its Output pins, it is severely limited to what it can power by itself. A typical 5mm red LED requires about 30ma of current, so the Arduino has no problem lighting it up to 100%—but anything more, and it will struggle. To use the Arduino to control a high-powered device requires the use of an "amplifier." Also called a "signal-buffer," an amplifier simply reproduces a low-power input signal, with a much higher output power to drive a load.

A basic amplifier has an input and an output—the input is a low-power signal (like the Arduino) and is used to drive the larger output signal that will power the load. A perfect amplifier is able to switch the high-power signal as quickly and efficiently as the Arduino switches the low-power signal. In reality, amplifiers are not perfectly efficient and some heat is dissipated in the switching process, which often requires the use of a heat sink on the switching device and possibly a fan to remove heat (like the CPU in your computer).

There are different types of amplifying circuits that can be interfaced with the Arduino depending on the type of signal output used. For slow-switching signals using the digitalWrite() command, you can interface the Arduino to a high-power relay. For fast-switching PWM signals using the analogWrite() command, you must use a solid-state switch, which allows for full 0-100% digital output control. You can also purchase a preassembled electronic speed controller and use the Arduino to provide the input control signals.

First let's talk about an electrically activated switch called a relay, which can conduct very large amounts of current and can be controlled using the Arduino.

Relays

A relay is an electrical switch that uses an electro-magnetic solenoid to control the position of a mechanical power contactor. A solenoid is similar to a motor because it uses a magnetic field to produce physical movement of the solenoid cylinder—but instead of spinning like a motor output shaft, the solenoid cylinder moves back and forth in a linear motion. Most relays are encased in a plastic or metal housing to keep the moving parts free from outside interference and dust (see Figure 2-1).

Here you can see a variety of relays in small to large sizes. The three smaller relays on the bottom row are called "signal" relays, meaning their contacts are rated for less than 2 amps of current. The three relays on the top row are called "power" relays, ranging from 5 amp to 25 amp contact ratings. Lastly, the mammoth relay on the far right is an automotive power relay, which is rated at 60 amps.

Figure 2.1. Here you can see a variety of relays in small to large sizes. The three smaller relays on the bottom row are called "signal" relays, meaning their contacts are rated for less than 2 amps of current. The three relays on the top row are called "power" relays, ranging from 5 amp to 25 amp contact ratings. Lastly, the mammoth relay on the far right is an automotive power relay, which is rated at 60 amps.

There are two parts to a relay: the solenoid and the contactor, and each is electrically isolated from the other. These two parts can essentially be treated as separate (but related) parts of a circuit, because each has its own ratings. The solenoid inside a relay has an electrical coil with a magnetic plunger that provides the movement needed to flip the contactor switch on and off. The relay coil should have the coil resistance listed as well as the operating voltage so that you can calculate how much current it will consume when in use. The contactor in a relay is where the high-power signal is switched. The contactor switch also has a voltage and current rating that tells you how much power you can expect the relay to conduct before the contacts fail.

Types of Relays

Relays are available with several different operation types depending on your application, so it is useful to understand how each type operates to make sure you get the right relay for the job.

  • Normally-Open (NO): This simply means that the two power contacts of the relay are connected when the relay coil is turned on and disconnected when the relay coil is turned off.

  • Normally-Closed (NC): This is the opposite of Normally-Open; the power contacts are connected when the relay is off and disconnected when the relay is on.

  • Latching: This means that the contactor switch in the relay is not spring-loaded, and it stays in whatever position it is placed into until the polarity is reversed to the coil, which returns the contactor switch to its original position. This is comparable to a standard home light switch—it stays on until you turn it off.

  • Non-latching: This is the "normal" type of relay that we use for failsafe switches. The relay contactor switch is spring-loaded and returns to the preset position unless power is applied to the coil. This is comparable to a momentary button switch—it stays on only while you press the button; otherwise, it springs back to the off position.

Relay Configurations

In addition to having different operating types, relays can have their contacts arranged in various configurations depending on the use. There are four common types of relays that we briefly discuss—each of these relays has only solenoid coil, but a varying number of power contacts. Any of these relay configurations can be Normally-Open or Normally-Closed as well as latching or Non-latching as described.

  • Single Pole, Single Throw (SPST): This type of relay uses one coil to control one switch with two contacts—there are four total contacts on this relay (see Figure 2-2).

    This SPST relay has one pole, with one contact (a simple switch).

    Figure 2.2. This SPST relay has one pole, with one contact (a simple switch).

  • Single Pole, Double Throw (SPDT): This type of relay uses one coil to operate one switch with three contacts (see Figure 2-3). The middle contact is for the load, the upper contact is for Voltage1, and the lower contact is for Voltage2 (or GND). This relay has five total contacts and is useful for switching one contact (Pole 1) between two different sources (Throw 1-1 and 1-2) —also called a three-way switch.

    This SPDT relay has one pole, with two contacts (a three-way switch).

    Figure 2.3. This SPDT relay has one pole, with two contacts (a three-way switch).

  • Double Pole, Single Throw (DPST): This type of relay uses one coil to operate two independent SPST switches at the same time (see Figure 2-4). This relay has six total contacts and is useful for switching two loads at the same time—the two loads being switched can be associated (like a set of motor wires) or separate (like a dual-voltage power switch).

    This DPST relay has two poles, and each pole has one contact (a double switch).

    Figure 2.4. This DPST relay has two poles, and each pole has one contact (a double switch).

  • Double Pole, Double Throw (DPDT): This type of relay uses one coil to operate two independent DPDT switches at the same time (see Figure 2-5). This relay has eight total contacts and can be configured as an H-bridge circuit, which is discussed in Chapter 3 (for controlling the direction of a load).

    This DPDT relay has two poles, and each pole has two contacts (a double three-way switch).

    Figure 2.5. This DPDT relay has two poles, and each pole has two contacts (a double three-way switch).

Uses

Relays have the advantage of using thick copper contacts, so they can easily be used to switch high currents with a relatively small amount of input current. Because the solenoid takes some time to move the contactor, PWM does not work with a relay. The PWM signal appears to the relay as an Analog voltage, which is either high enough to turn the relay coil on or it just stays off—but it is not generally a good idea to use a PWM signal on a relay.

You can, however, use a relay to switch high-power loads using the Arduino—including AC and DC lighting, motors, heaters, appliances, and almost anything else that uses electricity. The relay is extremely useful in robotics, because it can both switch a high-power load and be controlled electronically (and thus remotely), which opens many possibilities for its use. You can use a power relay as an emergency power disconnect on a large remote-controlled robot or a remote power switch for an electric motor or lights.

Using two SPDT (three-way) relay switches, we can control the direction of a DC motor. In Figure 2-6, you can see that if both relay coils (control 1 and control 2) are activated, the upper motor terminal will be connected to the positive voltage supply and the lower terminal will be connected to the negative voltage supply, causing the motor to spin in a clockwise direction. If power is removed from both relay coils, the upper motor terminal will be connected to the negative voltage supply and the lower terminal to the positive voltage supply, causing the motor to spin in a counter-clockwise direction.

These figures show how a DC motor can be controlled using two SPDT relay switches (or one DPDT relay switch).

Figure 2.6. These figures show how a DC motor can be controlled using two SPDT relay switches (or one DPDT relay switch).

Before we can use the relay, we need to calculate how much power is needed to drive the relay coil. If the relay coil draws more current than 40mA that the Arduino can supply, an interface switch will be needed to turn on the relay coil using the Arduino.

Calculating Current Draw

To determine the amount of current that a relay draws, you must first determine the coil resistance by checking the relay datasheet. If this information is not available, you can measure the resistance with a multi-meter. Using the coil resistance and voltage rating of the relay, use Ohm's law to calculate the current draw from the coil.

In Figure 2-7, you can see a sample of the datasheet from the Omron G5-CA series relays. As you can see, the relay is available with three different coil voltages (5v, 12v, or 24v). The coil resistance for each model is listed below along with the rated current. The 5v version of this relay coil has a rated current of 40mA, which is low enough to be powered by the Arduino without using an interface circuit.

This is a sample portion of a relay datasheet; you can see both the coil and contact ratings.

Figure 2.7. This is a sample portion of a relay datasheet; you can see both the coil and contact ratings.

Even though this particular datasheet displays the rated current of the relay coil, some relays have only the operating voltage listed. In this case, you must manually measure the resistance of the relay coil using your multi-meter and then use Ohm's law to calculate the current draw.

From the datasheet in Figure 2-7, we use Ohm's law to verify the current draw for a 5v relay with a coil resistance of 125 ohms.

  • V = I * R

  • I = V / R

  • I = 5v / 125 ohms

  • I = 0.040 amps (40mA) —The datasheet is correct!

Back-EMF Considerations

Remember from Chapter 1 that a relay coil (solenoid) is an inductive type load and produces a jolt of Backwards Electro-Motive Force, anytime the solenoid is turned off. This Back-EMF can severely damage electronic switching components that are not protected with a standard current rectifying diode, like the 1n4004 diode used in Figure 2-8. The diode is placed across the terminals of the load (in this case, the relay coil) to prevent the Back-EMF from damaging the Arduino output pin.

This schematic shows the use of a diode around the relay coil to protect the Arduino output pin or other switching device from Back-EMF produced by the relay coil.

Figure 2.8. This schematic shows the use of a diode around the relay coil to protect the Arduino output pin or other switching device from Back-EMF produced by the relay coil.

Although the relay in Figure 2-7 can be driven directly by the current available from the Arduino, most power-relays require a bit more than 40mA to turn on. In this case, we need a signal interface switch to provide power to the relay coil using the Arduino. To do this, we first need to discuss solid-state (electronic) switches.

Solid-State switches

A solid-state switch is one that switches an electrical load using doped silicon chips that have no moving parts. Transistors, Mosfets, photo-transistors, and solid-state relays are all examples of solid-state switches. Because solid-state electronics have no moving parts, they can be switched much faster than mechanical ones. You should check the manufacturer's datasheet for the part you are using, but PWM signals can typically be applied to these switches to provide a variable output to the load device.

There are two places that we can put a switch in the circuit to control power to the load. If the switch is between the load and the positive voltage supply, it is called a high-side switch. If the switch is between the load and the negative voltage supply, it is called a low-side switch, as shown in Figure 2-9.

Here you can see the difference between a high-side and low-side switch.

Figure 2.9. Here you can see the difference between a high-side and low-side switch.

Transistors

A transistor is an electronic switch that uses a small input signal to switch a large output signal, using a common voltage reference. Transistor switches differ from normal switches (like relays) because they cannot be placed just anywhere in the circuit. A low-side switch must use a negatively doped transistor, whereas a high-side switch must use a positively doped transistor.

There are three common types of transistors that we use: the Bipolar Junction Transistor (BJT), the Metal-oxide Semi-Conductor Field Effect Transistor (MOSFET), and the photo-transistor. All of these devices are transistor (electronic) switches and operate as such, but each is activated using a different means. The BJT is activated by supplying a specific amount of electrical current to its base pin. The MOSFET acts like a BJT, but instead of current, you must supply a specific voltage level to the MOSFET gate pin (usually 5v or 12v). The photo-transistor is the most different of the three, because this transistor is not activated by an electrical signal, but by light. We can interface all three of these types of transistors directly to the Arduino.

All types of transistors have a voltage and current (amperage) rating in their datasheet—the voltage rating should be strictly adhered to, because going over this limit will likely destroy the transistor. The current rating should be used as a guide to determine at what point the switch becomes unusably hot. As mentioned, you can install a heat sink and cooling fan to remove heat from the transistor, which increases its current rating.

Bipolar Junction Transistor (BJT)

The most common type of transistor, the BJT, is a current-driven amplifier/switch whose output current is related to its input current, called "gain." It is usually necessary to use a current-limiting resistor between the Arduino and BJT transistor to keep it from receiving too much current and overheating. Transistors also have no diode protection in case of Back-EMF from an inductive load, so if driving a motor or relay solenoid, you should use a protection diode as shown in Figure 2-10. If no Back-EMF protection diode is used, the Arduino output pin can potentially be damaged if the transistor switch is harmed.

A basic BJT has three pins: the Base (input), Collector (output), and Emitter (common). The emitter is always connected to either the positive or negative voltage supply (the polarity depends on the type of transistor) and the collector is always connected to the load. The base pin is used to activate the switch, which connects the emitter and collector pins together. There are two types of BJT transistors that are labeled by the arrangement of the three doped silicon layers on the semi-conductor chip.

  • Positive Negative Positive (PNP): Intended to be used as a high-side switch, the emitter of a PNP transistor connects to the positive voltage supply, the collector connects to the load, and the base is used to activate the switch. To turn this transistor off, its base pin must be equal to its emitter pin (positive voltage supply, or simply remove power to the base pin). Turning this transistor on is counter-intuitive because you have to apply a negative current, or a 0v (GND) signal to the base pin.

  • Negative Positive Negative (NPN): Intended to be used as a low-side switch, the emitter of an NPN transistor connects to the negative voltage supply (GND), the collector connects to the load, and the base is used to activate the switch. To turn this transistor off, its base pin must be equal to its emitter pin (negative voltage supply). This transistor is turned on by applying a positive current to the base pin (see datasheet for specific transistor rating).

This schematic shows a BJT used as a low-side switch to drive an inductive load (motor) with a Back-EMF protection diode around the switch. Notice that the transistor is driven through a current-limiting resistor (R1).

Figure 2.10. This schematic shows a BJT used as a low-side switch to drive an inductive load (motor) with a Back-EMF protection diode around the switch. Notice that the transistor is driven through a current-limiting resistor (R1).

Most BJTs require logic-level signals (+5v), to be applied to the base pin in order to activate the switch. Because a BJT is current-driven; when power is removed from its base pin the transistor quickly turns off. The current needed to switch on different transistors varies, but we will only use transistors that can be driven at levels provided by the Arduino. The common 2n2222a NPN transistor can be fully switched on with only a few milliamps of current and it can switch nearly 1 ampere, so it can be used as a simple low-side amplifier switch. The 2n2907a is the PNP counterpart to the 2n2222a that is commonly used as a simple high-side switch. Both of these parts are available at Radio Shack, Sparkfun.com, and Digikey.com and are inexpensive (less than $1 each).

Mosfets

A MOSFET is a type of transistor that is voltage-driven instead of current-driven like the BJT. This type of switch is also capable of extremely high PWM speeds and typically has very low internal resistance, making them ideal for use in motor-controllers. Mosfets usually include an internal protection diode (as shown in Figure 2-11) to isolate the output voltages from the input signal and protect from Back-EMF produced by the load, so it is generally acceptable to interface the Arduino directly to a MOSFET switch; this is one less part that must be added into the circuit.

This schematic shows a MOSFET switch (with built-in diode) used as a low-side switch to drive an inductive load (motor). Notice that there is no current-limiting resistor needed, but instead a pull-down resistor (R1) is used to keep the MOSFET switch turned off when not used.

Figure 2.11. This schematic shows a MOSFET switch (with built-in diode) used as a low-side switch to drive an inductive load (motor). Notice that there is no current-limiting resistor needed, but instead a pull-down resistor (R1) is used to keep the MOSFET switch turned off when not used.

A MOSFET transistor is similar to a BJT transistor because they have corresponding pins and types. The MOSFET pins are labeled Gate (input), Drain (output), and Source (common), which correspond to the BJT transistors Base, Collector, and Emitter, respectively (see Figure 2-12). Also, a MOSFET is not labeled as NPN or PNP, but rather N-channel or P-channel to denote its mode of operation. For practical purposes, these terms are interchangeable. Because MOSFET switches are voltage-driven and consume very little current, it is not necessary to use a current-limiting resistor in series with the gate pin of a MOSFET (as with a BJT), but it is good practice to use a resistor from the gate to source pin (see R1 in Figure 2-11) to fully turn the switch off when not in use.

Although they might physically look the same, the BJT (transistors) on the left are current driven, and the MOSFETs (transistors) on the right are voltage driven. Below each transistor, the pins are labeled—notice that the similar transistor packages have corresponding pins.

Figure 2.12. Although they might physically look the same, the BJT (transistors) on the left are current driven, and the MOSFETs (transistors) on the right are voltage driven. Below each transistor, the pins are labeled—notice that the similar transistor packages have corresponding pins.

Logic-Level vs. Standard

A normal MOSFET requires around 10v applied to the Base pin to fully turn on. Because driving anything above 5v with an Arduino requires using a level-shifter or amplifier, we use what is called a logic-level MOSFET for direct integration. A logic-level MOSFET can be turned on with a 5v "logic level" signal, which can be easily interfaced to the Arduino. Remember that a MOSFET requires a specific voltage level to be activated, but little current.

Mosfets are also sensitive to excessive gate-to-source voltages. If the limit is exceeded for even a second, it can destroy the MOSFET, so care should be taken to work within the voltage limits of the MOSFET. The maximum voltage that can be applied to the gate pin is listed in the datasheet as the "Gate to Source Voltage" or "Vgs" —this number is usually between 18vdc and 25vdc.

To drive a standard gate MOSFET, there are many different MOSFET-driver ICs that use logic-level input signals and a secondary power source (usually 12v) to send the amplified output signal to the MOSFET gate pin. Many MOSFET drivers are intended to provide the MOSFET gate pin with large amounts of current very quickly to allow for high frequency PWM switching speeds. Because of the higher PWM frequencies available with a high-current driver, we use MOSFET driver ICs in several of the projects in this book.

Mosfet Capacitance

Mosfets have tiny capacitors attached to their gate pins to maintain the voltage present at the gate. The capacitor charge enables the MOSFET to stay activated, even after the power is removed from the gate pin. Each time the MOSFET is switched, the gate capacitor must fully charge and discharge its current. For this reason, it is a good idea to ensure that the gate is forced to its off state by using a "pull-down" resistor to drain the capacitor when not actively powered by the Arduino (see R1 in Figure 2.11). Using a 10kOhm pull-down resistor from the gate pin to the source pin (gate to GND on n-channel, gate to VCC on p-channel) will be sufficient to keep the mosfet turned off when not in use.

As the PWM frequency that is applied to the MOSFET switch increases, the time allowed for the gate capacitor to charge and discharge decreases. As this happens, the gate-capacitor will require more current from the driver to fully charge and discharge in the shorter amount of time. If the current available from the driver is not sufficient to fully charge and discharge between switching cycles, the gate will be left in a partially conducting state, which can result in excess heating.

Saying that a MOSFET needs a lot of available current to switch quickly might seem confusing, because MOSFETs require a specific voltage to turn on and typically very little current. Although the 40mA that the Arduino PWM output pin can supply is plenty of current to fully switch a MOSFET on or off slowly, it is not enough to fully charge and discharge the MOSFETs gate-capacitor at high PWM frequencies where the MOSFET capacitor needs to be fully charged and drained 10,000 to 32,000 times per second!

Using a MOSFET driver IC (specialized signal-buffer) is the best way to drive a MOSFET switch because it can provide much more current during each switching cycle than the Arduino is capable of. A MOSFET driver can deliver enough current to the MOSFET to completely charge and drain the gate capacitor even at high PWM frequencies, which is important to reduce heat that is generated in the switch when it is not driven efficiently. You can also omit the pull-up or pull-down resistors from the gate pin when using a MOSFET driver to control a MOSFET—instead you should use a pull-down resistor at each input pin on the MOSFET driver IC, being driven from an Arduino PWM output pin.

On-State Resistance—Rds(On)

One of the most important properties of a MOSFET is the internal resistance between its Drain and Source pins (Rds) when the switch is on. This is important because the resistance of the switch determines the amount of heat that it will create with a given power level. We can determine the maximum Rds(On) value by checking the manufacturer's datasheet. The maximum power that is dissipated is determined using the Rds(On) resistance and the continuous current (in amps) that will pass through the switch.

Calculating heat using Rds(On) and amperage of DC motor:

How much total power will be passed through a MOSFET with an Rds(On) = 0.022 ohms (22 milliohms) and a continuous current draw of 10 Amperes? Use the Ohm's law pie chart from Figure 1-3 in Chapter 1—we want to know the heat produced in Watts, and we know the resistance of the MOSFET and the continuous current level passing through the circuit. So we need to use the formula: Watts = Current2 × Resistance.

  • W = I2 * R

  • W = 10 amps2 * 0.022 ohms

  • W = 100 amps * 0.022 ohms

  • W = 2.2 watts

This means that a single MOSFET with an Rds(On) = 0.022 ohms dissipates 2.2 watts if you try to pass 10 amperes through the switch. In my experience, dissipating more than 2 watts from a MOSFET in the TO-220 package results in excessive heating of the MOSFET. Any time more heat dissipation is needed, it is a good idea to add a heatsink or cooling fan to reduce the operating temperature and get rid of more heat. A good heat sink and fan can greatly increase the amount of power (or heat) allowed to safely pass through the MOSFET. If cooling methods do not suffice, you can arrange multiple identical MOSFETs in a parallel circuit to multiply the amount of current the switch device can handle. If you place multiple MOSFETs in parallel, it still operates only as one switch because they are opened and closed simultaneously, and their common pins are connected.

Parallel Mosfets

One of the most useful features of a MOSFET is the capability to arrange multiple switches in parallel for increased current capacity and decreased resistance. This is done by simply connecting the Drain terminals together and the Source terminals together (see Figure 2-13). The Gate terminals should be driven by the same control signal, but each MOSFET should have its own gate resistor to divide the total available current equally to each MOSFET used in parallel—these resistors can be a very low value from 10 ohms to 330 ohms.

Three MOSFETs (Q1, Q2, and Q3) are arranged in a parallel circuit (all like pins tied together) to allow three times the current flow and a third of the resistance as using only one MOSFET. The resistors (R1, R2, and R3) are in place only to evenly distribute the available current from the Arduino, but are not required.

Figure 2.13. Three MOSFETs (Q1, Q2, and Q3) are arranged in a parallel circuit (all like pins tied together) to allow three times the current flow and a third of the resistance as using only one MOSFET. The resistors (R1, R2, and R3) are in place only to evenly distribute the available current from the Arduino, but are not required.

Note

The voltage limits of the MOSFETs do not change even when using the parallel method. If the voltage limit is exceeded, you will likely blow up every MOSFET that is connected!

The total current that can be transferred through a parallel set of MOSFETs is equal to the amount of current that can be passed by a single MOSFET, times the number of MOSFETs used in parallel. In addition, the total resistance of the parallel set of MOSFETs is equal to the Rds(On) rating divided by the number of MOSFETs in the parallel circuit. This means that by using two MOSFETs in parallel, you decrease the resistance by half—and when the resistance is decreased, so is the heat dissipation.

Photo-Transistors

A photo-transistor operates like a standard NPN transistor, except that it is activated using infrared light from an LED instead of electrical current. These transistors are commonly used for line-following robots to detect reflective light differences on colored surfaces. If the infrared emitter and detector are enclosed in an IC package, the device is an optical-isolator, because the low power device (infrared emitter) is electrically isolated from the high power switch (photo-transistor), enabling the input and output circuits to be separated (they have different power sources).

This type of switch is like a transistor/relay hybrid; it has electrical isolation like a relay, but the switch is interfaced as a transistor; the unique feature that you get with a photo-transistor is an electrically isolated switch with PWM switching capabilities. In Figure 2-14, the base is driven using light from an infrared LED connected to the Arduino (using current-limiting resistor R1), the collector (pin 4) is connected to the negative load terminal (as a low-side switch), and the emitter (pin 3) is connected to the GND supply.

This optical-isolator (IRED and photo-transistor pair) is used as a low-side switch to drive a motor. Because the only thing connecting the Arduino to the Load is a beam of infrared light (no common GND signal), it is not required that you use a protection diode on the switch (though it is recommended).

Figure 2.14. This optical-isolator (IRED and photo-transistor pair) is used as a low-side switch to drive a motor. Because the only thing connecting the Arduino to the Load is a beam of infrared light (no common GND signal), it is not required that you use a protection diode on the switch (though it is recommended).

Interfacing a Motor-Controller

The term motor-controller refers to an amplifier that is designed to control the speed and direction of a motor using a specified set of signal commands. There are two types of motor-controllers that we discuss: motor-controller ICs and Electronic Speed Controllers (ESC).

A motor-controller IC is an integrated circuit chip that is designed to use a low-power input signal to provide a high-power output signal commanding both speed and direction to a DC motor. These usually require a few extra components (a few resistors, a capacitor, and a +5v power supply from Arduino) but take care of the motor-control.

An ESC is a complete motor speed controller circuit that accepts one or more types of input signals and outputs an appropriate speed and direction to the motor using PWM. These are usually prepackaged units that cost more to buy, but require less work to get going. If you are in a hurry, these are handy. The majority of ESCs are made for use with hobby airplane, car, and boat equipment and use a Servo pulse input signal. There are also an increasing number ESCs that are geared toward use in robotics, with a variety of different interfacing options.

Motor-controller ICs

There are several packaged IC chips that are inexpensive and easy to build into a circuit. The common L293D dual motor-controller is a 16-DIP IC chip that contains two protected driver circuits capable of delivering up to 600mA of continuous current to each motor at up to 36VDC (see Figure 2-15). The L298N is a similar chip that can deliver 2 amps to each motor. These chips (and others) accept standard 0-5v input signals and have internal logic gates to prevent accidental overloading and commanding the controller into a destructive state.

Notice in Figure 2-15 that there are four sets of inputs and outputs, labeled 1A (input) and 1Y (output) through 4A and 4y. The digital state of these input pins determines the digital state of their corresponding amplified output pin. In practice, when you apply a 5v signal (VCC1) to pin 1A, you get a 12v signal (VCC2) at pin 1Y. There is also an "enable" pin for each set of input/outputs. The 1-2 Enable pin controls the state of both 1Y and 2Y output pins simultaneously, and the 3-4 Enable pin controls the 3Y and 4Y outputs. You can use digital pins on the Arduino to control the four input pins and set the motor direction, while using a PWM signal on each Enable pin to set the speed of each motor.

A schematic of the L293D dual 1-amp motor-controller IC and how it can connect to the Arduino

Figure 2.15. A schematic of the L293D dual 1-amp motor-controller IC and how it can connect to the Arduino

If a motor-controller IC fits your needs but you don't want to build your own circuit, there are several commercial kits and preassembled circuits available that use small motor-controller ICs. The AdaFruit motor-shield in Chapter 4 features two L293D motor-controller ICs (see Figure 2-16) capable of powering up to four DC motors with 600mA of continuous current each. Simply plug the shield onto your Arduino and go—this shield is also wired for two Servo motor connectors (see Chapter 3 for more information) that can be used in addition to the four DC motors.

The Adafruit motor-shield is an easy-to-use motor-driver for the Arduino that can drive a variety of different motors, from left to right: DC motor, Servo motor, Stepper motor.

Figure 2.16. The Adafruit motor-shield is an easy-to-use motor-driver for the Arduino that can drive a variety of different motors, from left to right: DC motor, Servo motor, Stepper motor.

Electronic Speed Controllers (ESCs)

An ESC is a motor-controller with its own control circuitry. These are intended to be driven using a specified input signal and do not require a micro-controller. There are many different preassembled ESCs for use with both hobby type vehicles (cars, boats, and planes) and robotics that accept a specific input signal and command the motor appropriately. Though many of these units are designed to accept a Servo pulse signal from a standard hobby R/C system or an analog voltage from a potentiometer, you can use the Arduino to emulate a Servo pulse or analog potentiometer value. This can enable you to control a specialized ESC motor-controller with the Arduino, using any input method.

The Sabertooth 2×25 motor-controller from Dimension Engineering (see Figure 2-17) is a versatile replacement for any of the motor-controllers made in this book. You can use the Arduino to send control signals to the Sabertooth to command each motor, using a variety of different signals. Over-current protection is built in to the board, so it simply shuts itself down when it gets too hot.

This is the Sabertooth 2×25 dual 25-amp motor-controller. Though it doesn't look like much, it can provide two DC motors with enough power to move several hundred pounds with decent speed.

Figure 2.17. This is the Sabertooth 2×25 dual 25-amp motor-controller. Though it doesn't look like much, it can provide two DC motors with enough power to move several hundred pounds with decent speed.

There are many different speed-controllers available, each requiring a different control interface; some are PWM controlled, some use Serial commands, and others use analog voltages. Many hobby ESCs use a Servo pulse as the input signal to control each motor. There are four main motor-control interfacing methods that we will use, and I will briefly describe each:

  • Simple PWM control: Uses a PWM signal to determine the 0-100% output of one motor. The duty-cycle of the PWM signal determines the proportional output speed of the motor, in one direction. This is a common control method with homemade motor-controllers that interface directly to the transistor switches and with some types of MOSFET driver ICs. You either need two PWM signals or a high-speed signal inverter to drive the motor in both directions.

  • Bi-directional Analog control: Uses a 0-5v analog (or PWM) signal to determine the speed and direction of a motor. In this mode, the center position of 2.5v is considered Neutral. Below 2.5v spins the motor proportionally in Reverse with a 0v value yielding 100% Reverse. Above 2.5v spins the motor Forward with a 5v value yielding 100% Forward—this is called "Analog."

  • R/C control: Uses a special "Servo" pulse signal that encodes the position of an R/C transmitter control stick. The signal is a pulse of electricity that has a specific on time, which ranges from 1 millisecond to 2 milliseconds where the neutral position yields a 1.5 millisecond pulse. This type of interface is meant to directly connect to most hobby R/C radio systems.

  • Serial command: Uses a serial cable connected to a computer (USB) to receive a series of serial control pulses used to control the motors.

User Control

There are many different views of exactly what defines a "robot." Some people think that it isn't a robot unless it has some intelligence, such as the capability to make decisions based on its environment. Most industrial robotic equipment is designed to do a specific task with good accuracy, over and over again—but these robots are controlled by a humans and do not have decision-making capabilities. Some consider robotics to include the automation of a process, even if it is human controlled. In the interest of variety, we extend the scope to include all of these things as robots.

A robot's control method varies depending on its application. A stationary robot might need only commands when the user wants a specific action to be carried out, whereas a fast-moving mobile robot needs several control updates each second. An autonomous robot can also have R/C control to allow for multiple uses. The Arduino chip has enough room to code multiple control methods on the same robot. The control method can be as simple as a set of buttons mounted to the bot or as complex as using a GPS chip to guide the bot to a set of latitude and longitude coordinates on its own (see the section, "The Robo-Boat," in Chapter 9). There are many different methods of control but we discuss only a few common types.

Tethered (Wired) Control

Tethered control is easy to interface to an Arduino because you simply connect wires from each button, switch, or potentiometer directly to the Arduino input pins. You can even build your own controller box using a few potentiometers, some button switches, and a few feet of 8-conductor Thermostat wire from the hardware store. Using an 8-conductor wire and a common GND signal, you can easily have six or seven independent analog or digital channels on a tethered controller.

A medical power wheelchair typically has a joystick controller for the operator to control its movements, with a few other buttons to adjust anything from the top speed of the motors to the incline of the seat. The variable resistors and buttons in these controllers are wired directly into the micro-controller of the chair. The project in Chapter 11 is a ridable segway style bot that you can activate and steer from the handlebar—all of the connections are hard-wired into the Arduino.

Sometimes using a tethered controller is not possible on a robot, and you must look for a wireless solution. There are several to choose from with different applications and cost, so let's discuss a few.

Infrared Control (IR)

A set of infrared sensors can be used to control a robot, just like you use a remote control to change the channel on your TV. A TV remote control sends a specific infrared code for each button that is pressed, that way your TV set knows what to do depending on what button is pressed (i.e., Volume up/down, channel up/down, and so on). Using this same concept, you can read infrared codes on your Arduino using an IR receiver IC (see Figure 2-18) and use these codes to command a robot with a different robotic action for every different button that is pushed.

Using an infrared receiver IC pulled from an old VCR (pictured), you can send signals to the Arduino with your TV remote control.

Figure 2.18. Using an infrared receiver IC pulled from an old VCR (pictured), you can send signals to the Arduino with your TV remote control.

This type of control uses light emitted from an infrared emitting diode (IRED) that looks and operates just like an LED, except you can't see any visible light (with your naked eye) when the IRED is turned on. Because infrared devices use light to transfer the signal, this type of control method must have a clear line of sight between the emitter and receiver. With a clear transmission path, an IR sensor has an effective range of around 20 feet. There are even some R/C cars, indoor micro-helicopters, and robot toys that use an infrared wireless link, like the popular Robosapien from WowWee robotic toys (http://www.wowwee.com).

Radio Control Systems

Let me start by saying that I am by no means a radio expert, so I am not qualified to go into detail about the many different possibilities of radio signal transmission—but I have tried several different popular radio control methods and I can tell you which have worked best in my experience for robot use.

Radio control is probably the most common method for human control of a robot, because it is not strictly limited to line of sight operation and has an effective range of up to several miles. Most radio control equipment is intended for use in hobby airplanes, boats, and cars, but the Arduino makes an excellent interface to implement R/C control of a robot. A typical hobby grade radio control system consists of a "transmitter" that is used to capture your inputs and send them through the air, and a "receiver" that is used to capture these signals from the air and convert them into usable electrical signals (see Figure 2-19).

This hobby grade 2.4GHz radio system includes a six-channel transmitter and receiver pair for under $40 from HobbyPartz.com (part# 79P-CT6B-R6B).

Figure 2.19. This hobby grade 2.4GHz radio system includes a six-channel transmitter and receiver pair for under $40 from HobbyPartz.com (part# 79P-CT6B-R6B).

There are three types of radio systems that can be used for robot control: Amplitude Modulation (AM), Frequency Modulation (FM), and Spread Spectrum (2.4gHz). Although each of these types of signal modification is different, each one accomplishes the same goal: send the transmitter values to the receiver. For more advanced projects, there is also Xbee, a popular 2.4GHz wireless link.

Amplitude Modulation (AM)

AM radio systems are probably the most widely used, but mainly for remotely controlled toy cars and boats (27mHz and 49mHz are popular). These radio systems use long wire antennas to transmit their signals and are affected by interference from buildings, trees, and other radios nearby (on the same frequency). AM radio systems typically have no more than three usable control channels, making them less popular for use with robots. These systems can be used for small robots, but should be avoided where extreme reliability and range is required. These systems can be interfaced to the Arduino, but should be avoided unless you already own one.

Frequency Modulation (FM)

In the USA, all FM 75mHz radio systems are intended for use on ground vehicles (such as cars and boats) and all FM 72mHz radio systems are intended for use on air vehicles (such as airplanes and helicopters). These radio systems usually require a matching set of crystals placed into the transmitter and receiver to determine what "channel" the radio is operated on—the crystals simply tune the radio Tx and Rx to the same specific frequency so they can operate without interference. FM radio systems usually have a good range in open areas (up to 1 mile) and are not as prone to interference as AM systems, making them usable for robotics.

Spread Spectrum 2.4gHz

The 2.4GHz frequency range is common for wireless data transmission. Not only is this frequency band large enough to transmit large amounts of data (like audio, video, and Web), it can also provide a secure connection for R/C systems that is free from interference. There are many devices that use this frequency including wireless camera systems, wireless network routers, R/C systems, and even the Bluetooth wireless protocol. Spread Spectrum radios operate at 2.4gHz frequency, which is typically above the level of interference from metal fences and motor noise and they have no usage restrictions, making them suitable for use on any type of robot.

2.4gHz R/C systems use a process called "binding" to establish a semi-permanent connection between the transmitter and receiver. Binding is the replacement for matching frequency crystal pairs, used in legacy R/C systems. The spread spectrum standard uses a process called "frequency hopping" to constantly change frequency channels (both the transmitter and receiver simultaneously) to prevent any other nearby radios from crossing signals. An R/C system is the cheapest way to get a secure 2.4GHz connection that can be interfaced directly to the Arduino.

Xbee

Xbee operates as a wireless Serial data connection (with selectable data rates), capable of both sending and receiving data. Xbee radios are made by Digi International (www.digi.com) and use the "Zigbee" wireless communication protocol. With a variety of applications, these radios are easy to interface to the Arduino, requiring only a 3.3v power supply and a connection to the Arduino Serial tx and rx pins (D0 & D1).

Using a set of Xbee radios, you can create your own custom programmable R/C controller or telemetry link to obtain information about your bot during operation (i.e., battery voltage, amperage draw, speed, and so on). You can even use a pair of Xbee radios to wirelessly program an Arduino! Xbee radios are available with different power levels to accommodate longer distance transmissions, providing an effective range that is comparable to the common 2.4GHz R/C link. Because of the versatility of this robust wireless link, there are several chapters that discuss using a pair of Xbees for both robot control and monitoring (see Figure 2-20).

A pair of 2.4GHz Xbee radios (center) with breakout boards from Sparkfun.com

Figure 2.20. A pair of 2.4GHz Xbee radios (center) with breakout boards from Sparkfun.com

There are many retailers that sell Xbee radios and shield adapter boards to enable easy interfacing with the Arduino. A basic Xbee radio costs about $25 each and the adapter boards can range from $10–$25 each. An adapter board is needed because the pin spacing of each Xbee radio is 0.05 inch, which is not compatible with a breadboard or perforated prototyping boards, which use 0.1 inch spacing. In Figure 2-20, you can see two standard Xbee radios (Sparkfun.com part# WRL-08665), a Sparkfun Xbee Explorer Regulated breakout board (Sparkfun.com part# WRL-09132) to connect to the Arduino, and a Sparkfun Xbee Explorer USB breakout board (Sparkfun.com part# WRL-08687) for connecting to your computer. The makers of the Xbee radios have also created a software program called X-CTU, which is used to change settings on the Xbee radios while connected to your computer—the X-CTU software is free to use, but currently works only in Windows.

Sensor Navigation

Although creating a control link that converts user input into robotic output can be extremely useful, there are some tasks that require the robot to make decisions of its own without consulting a human. The first three robotic projects in this book (chapters 4, 5, and 7) use some type of external awareness to direct the robot to its destination without using any user guidance.

Believe it or not, you are actually an autonomous (self-controlled) being that uses several different "sensors" to help determine the environment around you. Your eyes, ears, nose, hands, and mouth each have their own sensation that your brain can interpret into some form of intelligence. From these sensors, your brain is able to make informed decisions about how your body should proceed and keep you safe from harm. Along the same lines, a robotic sensor is a device that is attached to a robot to gather information about its surrounding area. Without sensors, the robot would not have any way of knowing what is around it or how to proceed. This is the easiest way to add intelligence to your bot.

There are many types of sensors and each reads the environment differently, so it is common to add several types of sensors to one robot to effectively navigate around obstacles and gather important information. A sensor can measure light, distance, proximity, heat, gas, acceleration, angle, humidity and moisture, contact, and rotational position (among others). We focus on the sensors that are readily available and offer the most versatility for the price.

Contact Sensing

The most simple type of sensor that can be implemented is the contact switch, which simply tells the robot whether or not it is touching something. This type of sensor is commonly called a "bump-switch" and is used on the iRobot Roomba robotic vacuum cleaner to determine when it has bumped into a wall or other object. The main caveat of this sensor type is that it requires the robot to make physical contact with an object before it is detected.

Bump Switch

The bump switch is a simple form of sensor because it consists of as few as two electrical contacts (see Figure 2-21). If the contacts are touching, the switch is closed; otherwise, it is open. We use this form of switch as a method of telling the robot when it has run into something. If we place them at several spots on the bot, we not only know when the bot has bumped into something, we can also determine the best direction to travel to keep from hitting the same objects again.

This type of sensor is also useful as an over-travel switch. These are commonly installed on garage-door openers. When the door opens to a certain point, it touches the over-travel switch and the main board receives a command to stop the lifting motor. This is how it knows where to turn it off.

A typical tactile bump-switch with lever

Figure 2.21. A typical tactile bump-switch with lever

This type of sensor (or any switch) is read as a digital input using the digitalRead() command (see Listing 1-1 in Chapter 1).

Distance and Reflection Sensing

Range detection is useful when trying to determine whether an object is near without the robot having to touch it. A good range detector can calculate the distance from an object with accuracy down to the nearest inch. Range detection sensors use reflected sound or light waves to measure the distance between the sensor and any obstacle within range. Different range detection methods result in different effective ranges, accuracy, and prices. Range detection sensors can have an effective sensing range from 1 centimeter to 25 feet and cost anywhere from just a few dollars for an IR range finder to several thousand dollars for a Laser range finder. We use infrared detection for Linus in Chapter 4 and ultrasonic range finders on Wally in Chapter 7.

IR Sensor

IR detectors use an infrared emitter to send out IR light "packets" and a detector to check for reflections of that light that have bounced off any nearby objects. By measuring the amount of time that the light takes to return to the detector, the sensor can determine the distance from that object. IR finders can detect objects at distances up to 5 feet away—use an ultrasonic range finder for farther distances. The Sharp GP2 series of infrared proximity sensors are available at Sparkfun.com (part# SEN-08958) for under $15 and can be used for short-range object detection up to 5 feet (see Figure 2-22).

This is the Sharp GP2 IR finder (Sparkfun.com part# SEN-08958).

Figure 2.22. This is the Sharp GP2 IR finder (Sparkfun.com part# SEN-08958).

A simple infrared emitter and detector pair can be used at close ranges (under 3 inches) to determine the approximate reflectivity of a surface to infrared light. These simple IR emitter and detector pairs are the basis for the line-following robot in Chapter 4. The emitter and detector are mounted side by side and facing the same direction. The emitter is constantly sending a stream of infrared light toward the ground, whereas the detector is constantly reading the reflections of the light that is bouncing off the ground.

We use a piece of reflective tape as a guidance track for the bot to follow. As the bot moves away from the reflective tape, the IR sensors on each side begin to receive less IR light and therefore adjust the motor outputs to keep the bot centered on the tape. Using this simple guidance scheme, we can easily modify the path that the bot will follow by altering the path of the reflective tape. There are several different types of infrared emitter and photo-transistor packages that work for a line-following robot (see Figure 2-23).

These are three different types of infrared emitter and detector pairs. The IR pair on the far right is the type used in Linus, the line-following robot from Chapter 4. These sensors range in price from $1 to $3 each from Digikey.com.

Figure 2.23. These are three different types of infrared emitter and detector pairs. The IR pair on the far right is the type used in Linus, the line-following robot from Chapter 4. These sensors range in price from $1 to $3 each from Digikey.com.

Note

Many household appliances that use a remote control contain a useful IR receiver like the one you can find at Radio Shack or Digikey. If you happen to have a broken VCR, DVD player, TV, or stereo that you don't mind dismantling, you can de-solder the IR sensor from the PCB and save a few bucks. They typically only have three pins: +5v, GND, and Signal.

Ultrasonic Range Finder

The ultrasonic range finder uses high frequency sound waves that are reflected off nearby objects to calculate their distance. Some ultrasonic sensors require a microprocessor to both send and receive a signal from the sensor, whereas other sensors calculate the distance within the sensor and have a proportional output signal that is easily read by the Arduino.

Ultrasonic range finders are available in a variety of beam angles that determines the width of the detectable area. A narrow beam angle is better suited to detect objects farther away, whereas a broad beam angle better detects objects at short distances. These sensors are typically between $30–50 and can be easily read by the Arduino.

The Maxbotix brand of ultrasonic range finders have built-in processing that enables it to output independent serial, analog, and PWM signals at the same time to increase interfacing flexibility (see Figure 2-24). These range finders accurately measure distances from about 6 inches to 25 feet, and are well suited for obstacle avoidance and detection on robots. I prefer to use this brand of ultrasonic range finder because it is reliable and easy to interface to the Arduino using any of the three built-in output signals.

The MaxBotix LV-EZ0 ultrasonic range finder with an effective range of 6 inches to 25 feet (Sparkfun.com part# SEN-08502)

Figure 2.24. The MaxBotix LV-EZ0 ultrasonic range finder with an effective range of 6 inches to 25 feet (Sparkfun.com part# SEN-08502)

Laser Range Finder

This type of range finder uses a laser to scan the objects around it, much like a laser scanner at the grocery store checkout. A laser range finder can have a viewing angle of up to 240 degrees, giving it a much wider view of its surroundings than other sensors. Each time the laser makes a rotation, it takes distance readings at set intervals. When the rotation is complete, the signal is compiled to create a snapshot of the surrounding area. Although this sensor has advanced features, the maximum detection range is around 15 feet and they are expensive (usually costing around $1,000). Until the price comes down a bit, we won't test any of these units.

Orientation (Positioning)

There are several different sensors that can determine one or more aspects of a robot's position or orientation. A GPS sensor can tell you where the sensor is on a map using latitude and longitude coordinates, whereas an accelerometer and gyroscope can tell you the angular position (tilt) or rotational speed of your robot. Using these sensors, we can create an auto-leveling platform for a Segway type robot or upload a set of GPS coordinates for a robot to navigate to.

Accelerometer

An accelerometer measures gravitational force or acceleration. By tilting an accelerometer along its measured axis, we can read the gravitational force relative to the amount of tilt. Accelerometers are available with up to three axis of sensing and can be directly interfaced to the Arduino with an Analog output signal. Many devices currently use accelerometers for input control, shock detection, stabilization platforms, and auto-leveling or tilt interfaces—you can find these devices inside of digital cameras, cell phones, laptop computers, and Nintendo Wii controllers to name a few.

Most accelerometers available today are small surface mount components, but there are many different breakout boards that have the sensor and all necessary filtering components (resistors and capacitors) soldered into place, so you can easily interface them to an Arduino. Sparkfun.com has a large selection of these "Arduino-ready" sensor boards in different configurations ranging from $20 to $50.

There are three axes that can be measured by an accelerometer and they are labeled X, Y, and Z, which correspond to the roll, pitch, and yaw respectively (see Figure 2-25). A single axis accelerometer measures either the X or Y axis, a dual axis accelerometer measures both X and Y axes, and a triple axis accelerometer measures all three axes. Each measured axis represents a separate Degree of Freedom (DOF) from the sensor—thus a triple axis accelerometer might be labeled as 3 DOF.

This figure shows the three axes of rotation: roll, pitch, and yaw with their corresponding accelerometer symbols X, Y, and Z.

Figure 2.25. This figure shows the three axes of rotation: roll, pitch, and yaw with their corresponding accelerometer symbols X, Y, and Z.

Accelerometers are used for measuring gravitational changes, but they are also extremely sensitive to vibrations and sudden movements or shocks, which can cause the output signal to become distorted. If an accelerometer is used for angle approximation, it is helpful to have another device that can correct for its short-term errors caused by vibrations. A "gyroscope" sensor can measure angular changes, but using a different method that is less susceptible to sudden gravitational changes.

Gyroscope

A gyroscope is a sensor that is used to detect changes in rotational speed along its measured axis. These devices are often used in conjunction with accelerometers to produce a stable angle approximation of a robot for auto-leveling projects. Like an accelerometer, gyroscopes are also labeled by what axis they measure: X, Y, or Z.

Gyroscopes take excellent measurements in the short term, but suffer from a long-term mechanical error called "drift" that leads them away from their starting point, even without moving. To correct this drift, we need to use an accelerometer with a stable long-term angle reading to use as a reference point for the gyroscope.

Inertial Measurement Unit (IMU)

To get around the hassle of buying a separate accelerometer and gyroscope, manufacturers combine multiple accelerometers and gyroscopes on the same circuit board to save space and cost. A circuit board that incorporates multiple sensors that are physically aligned with each other is called an inertial measurement unit (or IMU). The IMU shown in Figure 2-26 has two gyroscopes and one accelerometer to measure a total of six axes. By combining the readings from the accelerometer X axis and the gyroscope X axis, we can create a filtered angle measurement for a single axis auto-leveling platform in Chapter 11.

This is the Sparkfun Razor 6 DOF inertial measurement unit (part# SEN-10010) used in Chapter 11. This IMU contains two gyroscopes and one accelerometer that together provide six separately measured aspects of the IMUs orientation.

Figure 2.26. This is the Sparkfun Razor 6 DOF inertial measurement unit (part# SEN-10010) used in Chapter 11. This IMU contains two gyroscopes and one accelerometer that together provide six separately measured aspects of the IMUs orientation.

Sparkfun.com has an excellent tutorial/buying guide for accelerometers, gyroscopes, and IMUs, explaining their features and capabilities. If you are interested in learning more about these sensors, it is definitely worth reading:

Sparkfun.com IMU tutorial: http://www.Sparkfun.com/tutorials/167

Global Positioning Satellite (GPS)

Global Positioning Satellite (GPS) systems are sensors that use the signals from multiple tracking satellites in space to calculate its own position, and then outputs a specific set of latitude and longitude coordinates. The GPS sensor must be able to pick up at least three GPS satellite signals to determine its own position, though most connect to as many as 20 satellites at a time to get the best signal. The sensor itself can be purchased from $50–$150 and provides position, time, and speed detection. Most units need only a power and ground signal to begin outputting a serial data stream, which can be read by the Arduino.

The signal output by the EM406 GPS sensor (shown in Figure 2-27) is a NMEA standard string that must be decoded to obtain the valuable information like the latitude, longitude, speed, and direction. This signal is transmitted through a standard Serial connection to the Arduino and can be used in conjunction with other sensors to autonomously guide a robot from one point to another. Although a GPS can provide information about the speed and direction of your bot, it must be moving to calculate these values. By simply taking the difference between two position readings, we can determine what direction the bot is traveling and adjust its power accordingly to steer it to the correct ending coordinates.

The EM406 GPS sensor from Sparkfun.com (part# GPS-00465) can be connected directly to the Arduino to create an autonomously navigating vehicle (like the Robo-boat in Chapter 9).

Figure 2.27. The EM406 GPS sensor from Sparkfun.com (part# GPS-00465) can be connected directly to the Arduino to create an autonomously navigating vehicle (like the Robo-boat in Chapter 9).

GPS is unique in that the satellites can be accessed from almost anywhere in the world, but because the satellites that it connects to are in space (really long way away!), the ground-level accuracy can vary from 10–30 feet. This means that you can't expect your bot to stop at the same spot each time using only standard GPS guidance. Your robot might end up 10 feet away from where you intended and its path might vary a few feet from your expectations. GPS also cannot detect movable objects or trees that might be in its path, so if your robot is to travel on the ground, it is a good idea to have other object detecting sensors installed to assist the GPS guidance (like ultrasonic range finders or bump sensors).

Real Time Kinetic (RTK) GPS

To eliminate the gap in accuracy from a standard GPS system, there is a system called Real Time Kinetic (RTK) GPS, which uses a separate "base station" with a known position to calculate the exact position of the GPS receiver down to 1 centimeter! This type of GPS typically requires more setup to get it running, placing it above the scope of this book. If you need 1 centimeter accuracy from a GPS unit, it is possible.

Non-Autonomous Sensors

Some sensors are not used for autonomous control, but rather to enable a user to do things not otherwise possible. A wireless camera for instance can enable a robot operator to be in one location, while the robot is operated in a different location. You can also use ultrasonic range finders, bump sensors, GPS positioning, temperature sensors, or hardware monitoring devices to assist your operation of the bot.

In Chapter 8, we build a dual motor-controller for a large mobile robot that has built-in current sensors installed to monitor the amount of power going through the controller. If the amperage exceeds a level that is pre-defined in the code, the Arduino immediately shuts down power to the motors (for a few seconds) to prevent overheating. This type of protection is essential for any robot that will have an unknown workload. Having your robot stop responding for a few seconds to cool down is better than it getting overheated and failing (requiring repair).

Camera

By far the most instantly rewarding sensor to put on your bot is the wireless camera. There are several systems for under $100 that include both the wireless color camera (with audio) and the receiver that plugs into a TV, computer, or a small LCD monitor mounted to your R/C transmitter. These systems are commonly built on either a 900MHz or 2.4GHz radio link that can transmit a video signal anywhere from 300–800 feet depending on the strength of the radio and obstructions between the transmitter and receiver.

You can use a 2.4GHz wireless camera (see Figure 2-28) with a microphone to transmit an audio and video signal from your bot to a command station where you watch and control. This keeps you safe while the bot goes where you need it to. Again, this sensor is best utilized with other types of sensors to notify the robot if it has hit something or if it is approaching an object that the camera cannot see.

This 2.4GHz wireless camera from Sparkfun.com (part# WRL-09189) is an excellent way to add a remote set of eyes and ears to your robot for about $75.

Figure 2.28. This 2.4GHz wireless camera from Sparkfun.com (part# WRL-09189) is an excellent way to add a remote set of eyes and ears to your robot for about $75.

Current Sensor

A current sensor is used to measure the amperage passing a given point at a given time. If either motor draws an excess amount of current, we can program the Arduino (which controls the motor-controller) to stop driving that motor for a specified amount of time (1–2 seconds) to keep from overheating. By protecting the motor-controller, it is far less likely to fail.

There are several types of current sensors, but I prefer to use a current sensor IC, like the ACS-712 (5 amp) or ACS-714 (30 amp) from Allegro MicroSystems (Figure 2-29). Although only available as an 8-SOIC (Small Outline IC) surface mount package, it is easily interfaced to both the Arduino and a motor. The IC only needs +5v and GND signals to begin outputting an analog voltage at the VOUT pin, which is easily read using the Arduino on any analog input pin—you can even use the Arduino regulated +5v supply to power the current sensor IC.

The ACS-712 bi-directional current sensor can be used in series with one of the load terminals to measure the amperage level. It can easily be read using an Analog input on the Arduino.

Figure 2.29. The ACS-712 bi-directional current sensor can be used in series with one of the load terminals to measure the amperage level. It can easily be read using an Analog input on the Arduino.

In Figure 2-30, you can see a simple schematic depicting how you might connect the ACS714 current sensor to your Arduino. The current sensor IC needs a 5v power supply and a bypass capacitor connected from the filter pin to GND. You must then route at least one of the motor supply wires through the current sense pins of the sensor (as shown in Figure 2-30).

A simple schematic for the ACS-712/714 current sensor IC in a circuit

Figure 2.30. A simple schematic for the ACS-712/714 current sensor IC in a circuit

In Chapter 8: The Explorer-Bot, we use the ACS-714 bi-directional 30-amp current sensor (big brother to the ACS-712 shown in Figure 2-29) to sense the current passing through either drive motor. Using the Arduino to monitor the analog output voltage of the current sensor, we can tell the Arduino to stop sending motor commands if the reading from the current sensor is above a certain level—this is called "over-current" protection and can keep a motor-controller from destroying itself.

Summary

This chapter briefly discussed the various interfacing and control methods that are used in this book, as well as some of the different sensors.

Relays are reliable and easy to interface, but produce Back-EMF and must use a protection diode to drive directly from the Arduino. Transistors are available in several forms and can be driven directly by the Arduino, but must be specifically placed as either a high-side or low-side switch. ESCs are available for those who do not want to build their own circuits, but typically cost more to buy than building a similar motor-controller using transistors or relays.

We then talked about some various control methods using both wired and wireless links, including infrared controllers and radio control. Radio control is implemented using a variety of different methods, the most common of which is 2.4GHz. We use 2.4GHz hobby radio control systems and Xbee wireless serial links to both control and monitor robotic functions. Wireless radio control is used on the Explorer-bot, Lawnbot, and Battle-bot projects in this book.

Lastly, we explored some of the sensor types to create an autonomous (self-guided) robot. We use contact switches on the Bug-bot, infrared detectors on the Line-bot, ultrasonic sensors on the Wall-bot, and GPS on the Robo-boat to guide these four bots around their course without any user input. Sensors can also be used to assist a human for easier or improved control, as with the Explorer-bot or the Seg-bot.

In the next chapter, we discuss the various types of electric motors, batteries, and wheels that are popularly used in robotics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset