Tải bản đầy đủ (.pdf) (10 trang)

Practical Arduino Cool Projects for Open Source Hardware- P3 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (218.89 KB, 10 trang )

Acknowledgments
More thanks than I can express go to Ann, Amelia, and Thomas. Their patience during this project has
been amazing. Many thanks go to Hugh Blemings, my partner in crime, whose patient discussions
during many late-night phone calls helped me understand far more about Arduino.
Thanks also go out to the technical reviewers who provided us the benefit of their expertise and
years of experience: Andy Gelme, Marc Alexander, Nathan Seidle, Trent Lloyd, Scott Penrose, Thomas
Sprinkmeier, and Philip Lindsay.
Of course, thanks go to the core Arduino team whose vision conjured the whole Arduino ecosystem
into existence: Massimo Banzi, David Cuartielles, Tom Igoe, Gianluca Martino, David Mellis, and
Nicholas Zambetti.
The parts suppliers who were so helpful when it came to sourcing the random assortment of bits
needed for developing these projects, were also invaluable: SparkFun, AsyncLabs, and NKC Electronics.
Any many thanks go to Arduino developers everywhere! The amazing success of Arduino is due to
the strong community that has blossomed around it. It's a beautiful thing when imaginative people have
new tools placed in their hands, and the results have been inspirational to both Hugh and myself.
Finally, many thanks to Michelle Lowman and James Markham, our editors at Apress, who had to
turn out of bed early to catch Hugh and I in a totally different time zone on our weekly Skype call; and
copyeditor Vanessa Moore, who put the finishing touches on all our words
xix
Introduction
Phenomenon is an overused and overloaded term, but somehow it seems appropriate for Arduino—an
endeavor that has caught the attention of an astonishingly wide range of people and provided
opportunities for those who might otherwise have never picked up a soldering iron or written a single
line of code. From dyed-in-the-wool hardware hackers to web page developers, robotics enthusiasts to
installation artists, textile students to musicians: all can be found in the Arduino community. The
versatility of the platform encompassing both hardware and software, combined with its inherent
openness, has captured the imagination of tens of thousands of developers.
One of Arduino's many strengths is the sheer volume of information available in both printed form
and on the web. Getting started is really pretty easy, as the core Arduino team intended. There are
plenty of excellent introductory works already available both online and in print, so we didn't want to
waste your time by providing yet another "blinking LED" tutorial. We figure that if you've gotten as far as


picking up a 400+ page book about Arduino it's a good sign that you're ready for something a bit more
substantial and wanting to learn more about the why rather than just the how.
We don't want you to be just a color-by-numbers painter, only able to assemble other peoples’
designs by dutifully plugging in wires according to a position overlay without really understanding the
meaning behind it. We want you to become a true artist, able to conceptualize, design, and assemble
your own creations.
We would be terribly disappointed if all our readers just reproduced our projects exactly as
presented in the book, never deviating from the script. We want you to take these projects as inspiration
and examples of how to apply a variety of handy techniques and then adapt them to suit your own
requirements, coming up with new ideas that put ours to shame. We also hope that you'll share your
creations with us and with the world, inspiring others in turn.
So we haven't included assembly overlays, and we don't expect you to slavishly follow a series of
steps to exactly reproduce what we've prototyped. Instead we've included circuit diagrams, parts lists,
photos, and in-depth explanations. That may seem a little scary and the idea of learning how to read a
schematic may feel overwhelming, but a little effort invested to learn this fundamental skill will pay off
many times over as you progress to designing and debugging your own projects.
Thus we have consciously left material out of Practical Arduino. We do not, for example, cover how
to set up basic software tools such as the Arduino IDE. This is for two reasons—firstly because it is
described very well on the web site itself, and secondly because anything that we
provide in written form here will be out of date in a few short months! Instead we focused on providing
the sort of information and background explanation that you will continue to draw on for years to come.
We hope that by following through the projects in this book, assembling some for yourself and
reading through the others, you will gain a number of insights into the flexibility of Arduino as a platform
for taking software and hardware and linking them to the physical world around us.
xx
C H A P T E R 1

  

Introduction

Fundamentals
Arduino is a fusion of three critical elements: hardware, software, and community. To get the most out of
it you need to have a basic understanding of all three elements, and for most people the biggest
challenge of the three will be hardware. Before you jump into any of the projects please take the time to
read through this chapter. Not only will it provide background information that the projects require, but
it could save your life!
Sharing Your Work
One of the key aspects of the success of Arduino has been the community that has sprung up around it
due to the open nature of the Arduino software and hardware. The software used on Arduino is entirely
open source and the hardware design information (schematics, PCB layouts, etc.) have been made
available under Creative Commons licenses.
In practice, this means it is easy to adapt both the software and the hardware to your needs, and
then contribute what you do back into the Arduino project as a whole.
The authors are unashamed proponents of this model and would encourage you to consider making
your own work available back to the Arduino community in a similar way. For software source code,
please provide explicit copyright and/or licensing information in the source files. Doing so makes it
possible for others to reuse your code in their own work and know that they are doing so with your
permission. For that reason, wherever possible we’ve licensed Practical Arduino code under the GNU
General Public License (GPL).
Similarly for hardware details, even if it’s a simple schematic on a web page, it never hurts to be
explicit about if/how it can be reused.
Practical Electronics for Software Developers
One of the beauties of designing projects around Arduino is that much of the low-level electronic detail
is taken care of for you. For all but the most simple of projects, having some basic skill in electronics will
serve you well and allow you to understand what is going on behind the scenes. To that end, we’ve
gathered together some basic and not-so-basic things you will find helpful in the remainder of this
chapter. The reference material in Chapter 16 covers some more advanced topics that may be of use as
you develop more complex projects of your own. We also introduce some topics within the different
project chapters. We encourage you to read through the projects for this reason, even if you’re not
planning on trying it out yourself.

1
CHAPTER 1  INTRODUCTION
Current, Voltage, and Power
Current, voltage, and power are interrelated and worth understanding if you’re to avoid inadvertently
cooking your hardware.
Voltage is measured in units of volts (V). With the symbol V, it is the measure of potential in a circuit.
The oft used analogy is water - voltage then becomes the height from which the water is flowing or
falling. Greater height, more potential energy from the water flow, similarly greater voltage, more
potential energy.
Current is measured in units of amperes (A), usually abbreviated to amps, and is the rate of flow of
electric charge past a point. The symbol used for current is I. To continue the water analogy, current
might be considered the width/depth of the water flow.
Power is the amount of energy in a system, and is measured in units of watts (W). With the symbol P,
in quantitative terms for an electrical circuit, it is equal to current × voltage. Hence, P = I × V. To round
out the water analogy, there is a lot more power in Niagara Falls than the downpipe on the side of your
house.
Bringing these three quantities into an Arduino context, the typical voltage supply rail on an
Arduino board is 5V, or 3.3V on some designs. The output pin of an ATMega168 can provide a maximum
of 20mA (0.02A). The total current you can pass through the output circuitry of the 168 as a whole is
100mA, which at 5V is 0.5W.
We’ll have more to say about maximum currents and current limiting in the next section.
Units of measure
Unsurprisingly, electronics makes extensive use of SI Units and SI Prefixes.
When it comes to smaller quantities, you will see m for milli, µ for micro, and n for nano. These equate to
10
–3
, 10
–6
and 10
–9

, respectively; thus, 20mA is 0.02A, 45µs is 0.000045 seconds, etc.
For larger figures, you’ll come across k for kilo (10
3
) and M for mega (10
6
), most often when dealing with
frequencies (8MHz–8,000,000Hz) or resistances (10kΩ–10,000Ω).
While not part of the SI system, a convention you’ll see used is decimal points being replaced with the unit
of measure; thus 3.3V becomes 3V3, 1.5kΩ becomes 1k5, etc. This alternative approach is common in
Europe and Australia, but less so in North America.
Mains Is Nasty
No discussion of electronics would be complete without discussing issues around mains voltages—that
which comes out of the socket in your home—early in the piece.
As a first approximation, any voltage over 40V or so has the potential to give you a tingle or mild
shock. Not far above this, sufficient current can flow through your body, your heart in particular, and
really ruin your day. While it varies with individual physiology, anything over 20mA flowing through your
heart will be fatal.
2
CHAPTER 1  INTRODUCTION
In practice, this means that you should take extreme care if you have to deal with mains voltages.
We strongly encourage you to avoid them if possible, but if you must interact with mains, some
precautions you must take include the following:
• Don’t work on mains-based projects solo. Have someone around who knows
where to turn off the power and provide first aid if things go wrong.
• Don’t work on the hardware when tired.
• Wear insulated footwear, such as rubber sole shoes.
• Only touch the hardware with one hand at a time. This, combined with insulated
shoes, lessens the chance of your heart being in the current path.
• Isolate (i.e., unplug) the hardware whenever possible. Only work on equipment
live as a last resort.

• Assume equipment is live unless you are absolutely sure it isn’t.
• Ensure any metalwork is securely earthed.
• Ensure any wiring carrying mains voltage is of a suitable gauge (thickness) and
insulation rating.
• Ensure any mains connections are well insulated and, as far as possible, hard to
touch accidentally.
• If you are at all unsure of any aspect, consult an expert. Mains voltages don’t
provide much in the way of second chances.
• Don’t be discouraged. If you’re just using regular low-voltage Arduino
applications, it’s pretty hard to hurt yourself!
Reading Schematics
A schematic or circuit diagram is a diagram that describes the interconnections in an electrical or
electronic device. In the projects presented in Practical Arduino, we’ve taken the approach of providing
both a photograph and/or line drawing of the completed device along with a schematic. While learning
to read schematics takes a modest investment of your time, it will prove useful time and time again as
you develop your projects. With that in mind, we present a quick how-to in this section.
Figure 1-1 is a photo of the hardware equivalent of “hello world!”—a battery, a switch, an LED, and a
resistor. Figure 1-2 is the corresponding schematic diagram with some additional annotations to make
clear what corresponds to what.

3
CHAPTER 1  INTRODUCTION

Figure 1-1. A battery, resistor, LED and switch – the hardware version of “hello world!”


Figure 1-2. Schematic representation of the circuit shown in Figure 1-1
You’ll note that the lines on the schematic closely follow the physical layout of the circuit. This isn’t
always the case, but a well drawn schematic will strive to have some correspondence to the physical
layout as well where possible.

4
CHAPTER 1  INTRODUCTION
The component symbols used in schematics vary a little depending on the age of the diagram
(valves anyone?) and where it was drawn— European schematics often use slightly different symbols
than ones from North America or Australia. We won’t try to cover all the differences here, as there are
excellent references on the Internet that do so.
Figure 1-3 shows how we represent two wires passing each other, but not electrically connected,
and two wires that are electrically connected.


Figure 1-3. Crossed wires where there is no connection (left) and connected (right)
Schematics also make use of symbols to show implied interconnections. Figure 1-4 is an admittedly
rather contrived example of how power and ground signals are used to simplify larger diagrams. They
are an example of a “net”—a connection in a circuit diagram that is inferred by a label or name rather
than a physical line on the schematic. You’ll see examples of this power/ground and “net” shorthand
throughout Practical Arduino.



Figure 1-4. Example of the usage of “nets” in a schematic
5
CHAPTER 1  INTRODUCTION
Resistance and Resistors
Resistance, as the name suggests, is the restriction of current flow in a circuit. The most common
example of a circuit element that does this is a resistor, a component specifically designed to resist the
flow of current in the circuit. The other situation where resistance is encountered (sorry ) is when using
long lengths of wire, which we discuss further in the following section.
Resistance is measured in units of ohms (Ω or R) and uses the symbol R. If you connect resistors in
series (see Figure 1-5) the total resistance is the sum of the resistors—in the example shown, a 1k resistor
is connected in series with a 500 Ω resistor yielding a total of 1.5k Ω.



Figure 1-5. Two resistors connected in series
Resistors connected in series can be used to create a “voltage divider” circuit, which can be useful
for reducing an input voltage by a known fixed ratio before being measured. We discuss this further in
the section “Input Conditioning” in Chapter 5.
Resistors connected in parallel (see Figure 1-6) are more or less the opposite of resistors in series.
The total resistance is given by

R = R1 × R2 / (R1 + R2)

In the example shown, the parallel 10k resistors result in a resistance of 5k.


Figure 1-6. Resistors in parallel
Two rules of thumb: (1)resistors in series give a total equal to the sum of the resistances, and
(2)resistors in parallel give a resistance lower than the lowest value resistor.
6
CHAPTER 1  INTRODUCTION
Ohm’s Law and Current Limiting
Ohm’s law provides three formulae that tie voltage, current and resistance together.

R = V / I , I = V / R , V = I × R

Thus, if an output pin on the Arduino is supplying five volts and is connected to a 250 Ω resistor in
turn connecting to ground, the resistor will reduce the current flow to 20mA. This resistor is said to be
acting as a current-limiting resistor.
 The resistor “soaks up” this current by converting it into heat, so it is conventional to have a power rating for a
resistor— this denotes the maximum power the resistor can dissipate at room temperature assuming a free flow
of air around it. Typical low power resistors have a rating of 1/4W or 1/2W— when driving LEDs and other low-

power devices from 5V supplies, you can usually ignore the power rating as you’re well within it. Another analogy
for a resistor power rating is a household water pipe— put too much pressure (power) through the pipe and the
pipe bursts, or the resistor smokes!
As noted above, the ATMega168 can supply a maximum of 20mA per output pin and a total of
100mA per device. Although there is internal current-limiting circuitry (a resistor!) on the chip itself, it is
considered good engineering practice to use external current-limiting resistors if at all possible;
otherwise, you run the risk of damaging the chip.
To understand current limiting a little further, we take the example of driving a red LED (see Figure
1-7).


Figure 1-7. Example of current limiting for a LED
7
CHAPTER 1  INTRODUCTION
As seen in Figure 1-7, the Arduino’s I/O pin connects to a resistor to the LED to ground. Note that
the meter across the LED is measuring 2.1V. This is known as the “forward voltage drop” or V
f
and varies
with the color of the LED and the model of LED itself. The meter across the resistor is measuring 2.9V,
which is the difference between the supply rail (5V) and the forward voltage drop of the LED (2.1V).
Using Ohm’s law, we can calculate the current through the 250 Ω resistor as I = V / R = 2.1 / 250 Ω =
11.6mA. This is a reasonable figure for a modern red LED.
It should be apparent that by changing the value of the resistor, we can increase or decrease the
current through the LED as required. While we’ve used an LED in this example, it could be replaced by
any other device.
Choosing Wire
A final aspect of resistance that is worth mentioning is so-called IR (internal resistance) loss— losing
power in the wires that interconnect your circuit. In the majority of applications, this can be safely
ignored, but if you are constructing circuits that use higher currents and/or long cabling runs, IR losses
should be considered.

The problem stems from the fact that any conventional wire conductor has resistance, the exception
being superconductors which remain a little out of reach.
Common stranded “hookup wire” with a diameter of 1.25mm (AWG16) has a resistance of around
0.014 ohms/meter. By way of example, say you use two 1m lengths of this wire to supply 5V to your
Arduino board consuming 100mA. The voltage drop in each conductor can be calculated using Ohm’s
law as being V = IR = 0.1 × 0.014 or 0.0014V (1.4mV). There are two conductors (supply and ground) so
the total drop becomes 2.8mV— not a large enough drop to give a second thought.
However, consider if you were using a longer run of cable— maybe down the other end of the house
(say, 15m)—and drawing higher current (perhaps a few high-wattage LEDs or a solenoid) to arrive at
500mA. The voltage drop now increases to 7mV per meter or 0.21V over the 30m combined up and back
length. In practice, an Arduino would probably cope with this, but other devices being used may not. In
any case, good engineering practice suggests you ought to opt for thicker wire.
As another reference point, most of us have some Cat5 network cabling lying around, which is rated
at 0.094 ohm/meter (worse than the hookup wire mentioned previously). While it’s okay for short
distances/low currents, do some math before you use it for longer runs/higher currents. On the plus
side, you get eight conductors (four pairs) so you can parallel them up to increase the current handling
capability through lower IR loss.
Another way to work around cable IR loss is to increase the voltage in use—so rather than supplying
5V from the near end, use 12V and have a voltage regulator at the far end to drop this back to 5V. This is
arguably a better engineered solution, as the supply seen by the Arduino will be cleaner as the regulation
is closer to the board.
As an aside, this same principle of raising the operating voltage is applied in the mains power supply
where the main grid supply runs at tens of thousands of volts to reduce the cable loss.
Diodes
A signal or rectifier diode is a component that allows current to flow (essentially) unimpeded in one
direction, when “forward biased,” but blocks it all but completely in the opposite direction when
“reverse biased”—a one-way gate for electrons, if you will. We use diodes extensively in the various
projects in Practical Arduino, so we dig into them a little here.
8

×