You are on page 1of 11

B UILDING A B LAMINATR

232

Download Motors/Blaminatr/Blaminatr.pde

#include <Servo.h> const unsigned int MOTOR_PIN = 9; const unsigned int MOTOR_DELAY = 15; class Blaminatr { Team _team; Servo _servo; public: Blaminatr(const Team& team) : _team(team) {} void attach(const int sensor_pin) { _servo.attach(sensor_pin); delay(MOTOR_DELAY); } void blame(const char* name) { _servo.write(_team.get_position(name)); delay(MOTOR_DELAY); } };

A Blaminatr object aggregates a Team object and a Servo object. The constructor initializes the Team instance while we can initialize the Servo instance by calling the attach( ) method. The most interesting method is blame( ). It expects the name of the team member to blame, calculates his position, and moves the servo accordingly. Lets put it all together now:
Download Motors/Blaminatr/Blaminatr.pde Line 1 5 10 15 -

const unsigned int MAX_NAME = 30; const unsigned int BAUD_RATE = 9600; const unsigned int SERIAL_DELAY = 5; char* members[] = { "nobody", "Bob", "Alice", "Maik", NULL }; Team team(members); Blaminatr blaminatr(team); void setup() { Serial.begin(BAUD_RATE); blaminatr.attach(MOTOR_PIN); blaminatr.blame("nobody"); } void loop() { char name[MAX_NAME + 1];
Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

W HAT I F I T D OESN T W ORK ?


if (Serial.available()) { int i = 0; while (Serial.available() && i < MAX_NAME) { const char c = Serial.read(); if (c != -1 && c != '\n') name[i++] = c; delay(SERIAL_DELAY); } name[i] = 0; Serial.print(name); Serial.println(" is to blame."); blaminatr.blame(name); } }

233

20 25 30

We dene a list of member names that is terminated by a NULL pointer. The lists rst entry is nobody, so we dont have to deal with the rare edge case when nobody is to blame. Then we use members to initialize a new Team object and pass this object to the Blaminatrs constructor. In the setup( ) function, we initialize the serial port and attach the Blaminatrs servo motor to the pin we dened in MOTOR_PIN. Also, we initialize the Blaminatr by blaming nobody. The loop( ) function is nearly the same as in Section 10.3, First Steps with a Servo Motor , on page 225. The only difference is that we do not control a servo directly but call blame( ) in line 28. Thats it! You can now start to draw your own display and create your own arrow. Attach them directly to the motor oreven betterput everything into a nice box. Compile and upload the software and start to blame. Of course, you can use motors for more serious projects. For example, you can use them to build robots running on wheels or similar devices. But you cannot attach too many motors to a naked Arduino, because it is not meant for driving bigger loads. So if you have a project in mind that needs a signicant number of motors, you should consider buying a motor shield6 or use a special shield such as the Roboduino.7

10.5

What If It Doesnt Work?


Working with motors is surprisingly easy, but still a lot of things can go wrong. The biggest problem is that motors consume a lot of power,
6. 7.

You can nd them at http://adafruit.com or http://makershed.com.


http://store.curiousinventor.com/roboduino.html
Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

E XERCISES

234

More Motors Projects Motors are fascinating. Search the net and youll nd numerous projects combining the Arduino with them. A fun project is the Arduino Hypnodisk. It uses a servo motor to rotate a hypno disca rotating disk with a spiral printed on it that has an hypnotic effect. An infrared rangender changes the motors speed, so the closer you get to the disc, the faster it spins. A useful and exciting project is the USB hourglass. It uses an Arduino and a servo motor to turn a sand timer, and it observes the falling sand using an optical sensor. Whenever all the sand has fallen through, the device turns the timer automatically. Thats all nice, but the devices main purpose is to generate true random numbers. Falling sand is a perfect basis for generating true randomness (see the sidebar on page 73), and the USB hourglass uses the signals from its optical sensor to generate random numbers, sending them to the serial port.
. . http://www.ickr.com/photos/kevino/4583084700/in/pool-make http://home.comcast.net/~hourglass/

so you cannot simply attach every motor to an Arduino. Also, you cannot easily drive more than one motor, especially not with the small amount of power you get from a USB port. If your motor does not run as expected, check its specication, and attach an AC or DC adapter to your Arduino if necessary. You also shouldnt attach too much weight to your motor. Moving an arrow made of paper is no problem, but you might run into problems if you attach bigger and heavier things. Also, be careful not to put any obstacles into the motors way. The motors shaft always needs to move freely. Some motors have to be adjusted from time to time, and usually you have to do that with a very small screw driver. Refer to the motors specication for detailed instructions.

10.6

Exercises
Add an Ethernet shield to the Blaminatr so you can blame people via Internet and not only via the serial port. Pointing your
Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

E XERCISES

235

Figure 10.6: A motorized thermometer

web browser to an address such as http://192.168.1.42/blame/Maik should blame me, for example. Create a thermometer based on a TMP36 temperature sensor and a servo motor. Its display could look like Figure 10.6; that is, you have to move an arrow that points to the current temperature. Use an IR receiver to control the Blaminatr. For example, you could use the channel key of your TV sets remote control to proceed the Blaminatr from one name to the other.

Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

Part III

Appendixes

Download from Wow! eBook <www.wowebook.com>

Appendix A

Basics of Electronics
We didnt need a lot of theory or background to create our rst Arduino projects. But its a good idea to learn a bit about electricity and about soldering if you want to build bigger and more sophisticated projects. In this appendix, youll learn the basics of electricity, and youll learn about Ohms law, which is probably the most important law in electronics. Also, youll learn more about resistors, and youll see that soldering isnt as difcult as it might seem.

A.1 Current, Voltage, and Resistance


To build your rst projects with the Arduino, you didnt need to know much about electricity. But at some point, youll need to understand what current, voltage, and resistance is all about. For example, you already know that you always have to put a resistor in front of an LED, but you might not know exactly why, and you might not know how to calculate the resistors size for a given LED. Lets remedy that. An electrical circuit resembles a water circuit in many respects. In Figure A.1, on the following page, you can see a water circuit on the left and an electrical circuit on the right. Isnt it fascinating how similar they are and that you can even nd a connection between them when you use a water-driven dynamo that acts as a power supply? Lets take a closer look at their most important attributes. While water ows in a water circuit, electrons ow in an electrical circuit. Voltage is electricitys equivalent of water pressure and is measured in volts (V). Voltage is the initial cause for a current, and the higher the voltage, the faster the current ows.

Download from Wow! eBook <www.wowebook.com>

C URRENT, V OLTAGE , AND R ESISTANCE

238

Current

Current (I)

Water Pump

Water Mill

Dynamo

Power Supply Voltage (V)

+
Resistance (R)

Pipe

Wire

Figure A.1: Water circuits and electrical circuits are similar.

In electronics, current is the amount of electricity owing through an electric line. It is the equivalent of the actual ow of water in a water circuit. While we measure the water ow in liters per minute, we measure current in ampere. One ampere means that approximately 6.24 1018 electrons are owing per second. Every component in a circuitbe it water or electricityresists some amount of current. In a water circuit, its the pipes the water is owing through or perhaps a water mill. In an electrical circuit, it is the wire or a light bulb. Resistance is an important physical phenomenon that is closely related to current and voltage. We measure it in Ohms, and its ofcial symbol is . The German physicist Georg Ohm found out that current depends on voltage and resistance. He postulated the following form we call Ohms law today:1 I (current) = V (voltage) / R (resistance) This is equivalent to the following: R (resistance) = V (voltage) / I (current) V (voltage) = R (resistance) I (current) So, for two given values, you can calculate the third one. Ohms law is the only formula youll absolutely have to learn when learning electron1.

We use I as the currents letter for historical reasons. In the past, it stood for inductance.

Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

C URRENT, V OLTAGE , AND R ESISTANCE

239

ics. When working with LEDs, for example, it helps you calculate the size of the resistor you need. If you look at a LEDs data sheet, youll usually nd two values: a forward voltage and a current rating. The forward voltage usually is somewhere between 1.8V and 3.6V, and the maximum current often is 20 mA (milliamperes). Lets say we have an LED with a maximum of 2.5 volts and a safe current of 20 mA. We also assume that we have a power supply delivering 5 volts (as the Arduino does, for example). Whats the right size of the resistor we need to put in front of the LED? We have to make sure that the resistor takes 5 2.5 = 2.5 volts from the circuit, so only 2.5 volts are left for the LED. This value is called voltage drop. Also, we want a maximum of 20 mA to ow through the LED. This implies that a maximum of 20 mA (0.02 A) should ow through our resistor also. Now that we know that 2.5 V and 0.02 A should pass the LED, we can use Ohms law to calculate the resistance R: R=V/I In our case, we have the following: R = 2.5V / 0.02A = 125 This means we need a 125 resistor for our LED. If you do not have a 125 resistor, use a bigger one such as 150 or 220. It will still protect the LED and only slightly decrease its brightness. Thats because wed decrease the current even more: I = 2.5V / 150 = 17mA I = 2.5V / 220 = 11mA

Resistors
Youll hardly ever nd an electronics project that doesnt need resistors. So, youll need them often and should get familiar with them a bit more. Usually youll use carbon or metal resistors. Metal resistors are more precise and dont create so much noise, but carbon resistors are a bit cheaper. In simple circuits, it usually doesnt matter which type you use. The most important attribute of a resistor is its resistance value that is measured in Ohm. Only a few vendors actually print this value on the resistor, because resistors are small parts, and its hard to read text
Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

C URRENT, V OLTAGE , AND R ESISTANCE

240

Color Code Zeros Black 0 Brown 1 0 Red 2 00 Orange 3 000 Yellow 4 0000 Green 5 00000 Blue 6 000000 Violet 7 0000000 Gray 8 00000000 White 9 000000000

Figure A.2: Resistor values are encoded using colors.

that is so small it ts on them. So, they use a trick and encode the value using colored stripes. Usually you nd four or ve stripes on a resistor (at least on throughhole parts; SMD resistors dont have them). One of them is separated from the others by a gap (see Figure A.3, on the next page). The separate stripe is on the right side of the resistor, and it tells you about the resistors accuracy. Gold stands for an accuracy of 5 percent, silver for 10 percent, and no stripe means 20 percent. Using the remaining stripes, you can calculate the resistor value. You read the stripes from left to right, and every color stands for a digit (see Figure A.2). The rightmost stripethat is the third or fourth one stands for an amount of zeros to be added to the preceding digits. In Figure A.3, on the next page, you can see three examples: On the rst resistor we nd four stripes: brown (1), green (5), brown (1 zero), silver (10%). That means we have a resistor value of 150. The second resistor has four stripes again: yellow (4), violet (7), orange (3 zeros), gold ( 5%). So, this resistor has a value of 47000 = 47k . The third resistor has ve stripes: brown (1), red (2), red (2), green (5 zeros), silver (10%), so the value is 12,200,000 = 12.2M.

Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

L EARNING H OW TO S OLDER
brown brown green Gap silver

241

150 10%
yellow orange violet gold

47k 5%
brown red silver

red

green

12.2M 10%

Figure A.3: Colored stripes tell you about resistor values.

In the beginning, the color coding seems to be complicated, but youll get used to it quickly. Also, you can nd countless tools for determining resistor values on the Internet.2 For the books projects, this is all the theory of electricity you need to know. To learn more about electronics, have a look at Make: Electronics [Pla10] or at http://lcamtuf.coredump.cx/electronics/.

A.2 Learning How to Solder


You can build nearly all of the books projects by plugging parts into a breadboard or directly into the Arduino board. But sooner or later youll have to learn how to solder if you want to become an expert in electronics. Thats mainly because youll learn the most by building projects, and even the simplest kits require some sort of soldering. A lot of people think that soldering is difcult or requires expensive equipment, so they never try to do it. The truth is that its cheap and
2. http://harkopen.com/tutorials/using-wolfram-alpha-electric-circuits

Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

L EARNING H OW TO S OLDER

242

pretty easy. It requires some practice, but after only a few solder joints youll see that its not rocket science. In this book, we have one project that requires you to solder a pin header to an ADXL335 breakout board. We need it for building the motion-sensing game controller in Chapter 6, Building a Motion-Sensing Game Controller , on page 132. In this section, youll learn how to do it, and youll need the following equipment (shown in Figure A.4): A 2530 watt soldering iron with a tip (preferably 1/16") and a soldering stand. Standard 60/40 solder (rosin-core) spool for electronics work. It should have a 0.031" diameter. A sponge. Before you start to solder, prepare your work area. Make sure that you can easily access all your tools and that you have something to protect your work area from drops of solder. Wearing safety glasses is always a good idea! Even seemingly simple and harmless activities such as cutting wires, for example, can be very dangerous! Bring all parts into the right position: attach the pin header to the breakout board, and make sure you cannot accidentally move it while soldering.

Figure A.4: You need these tools for soldering.

Report erratum

Download from Wow! eBook <www.wowebook.com>

this copy is (P1.0 printing, Janurary, 2011)

You might also like