The Arduino Inventor's Guide (51 page)

BOOK: The Arduino Inventor's Guide
10.18Mb size Format: txt, pdf, ePub

TABLE 10-2:
Selected approximate frequencies for notes across three octaves

NOTE

APPROX. FREQ.

NOTE

APPROX. FREQ.

NOTE

APPROX. FREQ.

C
3

131

C
4

262

C
5

524

C

3
/D

3

139

C

4
/D

4

277

C

5
/D

5

554

D
3

147

D
4

294

D
5

587

D

3
/E

3

156

D

4
/E

4

311

D

5
/E

5

622

E
3

165

E
4

330

E
5

659

F
3

175

F
4

349

F
5

698

F

3
/G

3

185

F

4
/G

4

370

F

5
/G

5

740

G
3

196

G
4

392

G
5

784

G

3
/A

3

208

G

4
/A

4

415

G

5
/A

5

831

A
3

220

A
4

440

A
5

880

A

3
/B

3

233

A

4
/B

4

466

A

5
/B

5

932

B
3

247

B
4

494

B
5

988

Modify

To make things more interesting, add a
distortion pedal
to this piano project. This button will switch up octaves to give you 16 notes for the price of 8. Connect a button to pin 2 on your Arduino as shown in
Figure 10-16
.

The sketch needs just few extra lines of code for the pedal, which will cycle a multiplier variable each time the button is pressed. This is known as a
state machine
, because each time the button is pressed, the state of a variable is changed. The code for this modification is in the book’s resources at
https://www.nostarch.com/arduinoinventor/
in the file
P10_TinyPiano_v2.ino
.

FIGURE 10-16:
Adding a button to pin 2 for octave control

The new code lines declare a state variable,
octaveMultiplier
, and then add a
pinMode()
command to set up pin 2 as an
INPUT
. When the button is pressed, the state variable,
octaveMultiplier
, is incremented, altering the frequency so that the note goes up.

Try it out. When you press the button, the notes should all go up by an octave. You now can play up to 16 notes with this simple Arduino instrument!

Bonus Project: Binary Trumpet

The SoftPot is a nice touch, but as a bonus we’ve built an Arduino instrument that uses actual buttons as keys instead of the SoftPot. This last project is called the Binary Trumpet. It uses three buttons to specify which note to play and the fourth button to play the note, like blowing on a trumpet. With three buttons, you can specify up to eight different combinations using the keypresses shown in
Table 10-3
.

TABLE 10-3:
Binary trumpet button sequences

BUTTON 1

BUTTON 2

BUTTON 3

NOTE TO PLAY

Up

Up

Up

C (262 Hz)

Up

Up

Down

D (294 Hz)

Up

Down

Up

E (330 Hz)

Up

Down

Down

F (349 Hz)

Down

Up

Up

G (392 Hz)

Down

Up

Down

A (440 Hz)

Down

Down

Up

B (494 Hz)

Down

Down

Down

C (524 Hz)

You may recognize this pattern as a binary sequence. It counts in the order 000, 001, 010, 011, 100, 101, 110, and 111.

To make room for the four buttons on the breadboard, move the buzzer up a little and then add those buttons, as shown in
Figure 10-17
.

FIGURE 10-17:
Binary Trumpet wiring diagram

The complete code for this modification is in the book’s resources at
https://www.nostarch.com/arduinoinventor/
in the file
P10_TinyBinaryTrumpet.ino
. Playing notes using the Binary Trumpet will take a bit of getting used to, but the sequence of presses in
Table 10-3
should help you as you pick it up.

As it turns out, with four buttons, you can actually play up to 16 different notes. Can you figure out how to modify the example to do this? Take a look at the book’s resources to see how.

Whether you use the Tiny Electric Piano or the Binary Trumpet, we hope this helps you on your way to a future career in making music. Now go forth and find an audience to show off your newest skills!

Appendix
More Electronics Know-How

This appendix provides a how-to on using a multimeter and soldering, as well as a handy reference for reading the color bands on resistors.

MEASURING ELECTRICITY WITH A MULTIMETER

A
multimeter
is an indispensable tool used to diagnose and troubleshoot circuits. As its name states, it is a meter capable of measuring multiple things related to electricity—namely, current, continuity, resistance, and voltage. Let’s take a look at how to use a multimeter. We will be using the SparkFun VC830L (TOL-12966; shown in
Figure A-1
) throughout the tutorial, but these methods should apply to most multimeters.

Parts of a Multimeter

A multimeter has three main parts, labeled in
Figure A-1
.

FIGURE A-1:
A typical multimeter

The display can usually show four digits and a negative sign. The selection knob allows the user to set the multimeter to read different things such as milliamps (mA) of current, voltage (V), and resistance (Ω). The numbers along the outside of the selection knob indicate the maximum value or range for any given setting.

On some multimeters, the display doesn’t show the units. In these cases, it is assumed that the values displayed have the same units as the setting, so if you have the range set to 200 Ω, the number displayed will be in Ω. If you have the range set to 2 kΩ, 20 kΩ, or 200 kΩ, then the value displayed will be in units of kΩ.

Most multimeters come with two probes, which are plugged into two of the three ports on the front of the unit. The three ports are labeled COM, mAVΩ, and 10A. COM stands for
common
and should almost always be connected to ground, negative, or the – of a circuit. The mAVΩ port allows the measurement of current (up to 200 mA), voltage (V), and resistance (Ω). The 10A port is the special connection used for measuring currents greater than 200 mA.

Most probes have a banana-type connector on the end that plugs into the multimeter, allowing for different types of probes to be used. For most measurements, connect the red probe into the mAVΩ port and the black probe into the COM port.

Measuring Continuity

Measuring continuity is possibly the single most important function for troubleshooting and debugging circuits. This feature allows us to test for conductivity and to trace where electrical connections have been made or not made. Set the multimeter to the continuity setting, marked with a diode symbol with propagation waves around it (like sound coming from a speaker), though this may vary among multimeters.

Touch the two probe ends together and you should hear a ringing tone—this is why checking for continuity is sometimes called “ringing out” a circuit. You can use this method to test which holes on a solderless breadboard are connected and which ones aren’t. The probe tips are usually too big to insert directly into a breadboard, but you can stick two wires in the same row on a breadboard and touch the ends of the probes to each wire. You should hear the tone indicating that these two wires are connected through the row. You can also use this method to trace out a circuit. Because you often can’t see where all of the wires go, this is a quick way to test whether two points are connected electrically. When you’re checking for continuity, it doesn’t matter which side of the probe you connect, because you’re just checking that one side is connected electrically to the other.

Measuring Resistance

The continuity setting simply rings a tone when the resistance is low, but to get an actual value for the resistance, you need to use a resistance setting. Turn the knob to one of the resistance settings marked by the omega symbol (Ω), which represents
ohms,
the unit for measuring resistance. Make sure that the resistor or the element
you’re measuring is not powered or connected to your circuit. A resistor, like many electrical elements, has two ends. To measure its resistance, simply touch the ends of the probes to the ends of the resistor. As with continuity, it doesn’t matter which side you connect to red and which side you connect to black.

There are several possible resistance range settings available. These settings represent the maximum value you can measure. If you want to measure a small resistor to a high degree of accuracy, you would set the multimeter low—to 200 Ω, for example.

If you try to measure a resistance greater than the range, the multimeter will simply display
[1. ]
, with no zeros displayed. If the resistance is greater than your chosen range, try moving the range up a notch and measuring it again.

Give it a try! If you measure the resistance of the 330 Ω resistor (orange-orange-brown), what values do you record for each setting? All resistors have a tolerance band; most are typically 5 percent. What is the percentage error for your measurement? Is it within the tolerance?

Test the resistance of the photoresistor. Hold your hand or something else opaque above the photoresistor, and measure the resistance for various heights.

Measuring Voltage

Voltage is a measurement of electrical potential between two points, sometimes also called the
potential difference
. Similar to the resistance settings, the various settings for measuring voltage specify the maximum value.

You’ll notice that there are two range symbols, one with two straight lines and one with a squiggly line. The two straight lines indicate
direct current (DC)
measurements, which are most commonly used in electronics. The squiggly line represents
alternating current (AC)
, the type of electricity found in the walls of your house. Be sure that you have the knob turned to the right setting—you probably want DC. The 20 V setting is the best choice for the projects in this book, since all voltages are limited to 5 V on the Arduino.

Now, try to measure the voltage on an Arduino board. Plug your Arduino board into your computer using the USB cable for power. To measure voltage, connect the black probe to GND (ground). Now, use the red probe to test the voltage at various points or pins (with respect to GND). What does the 5 V pin show? How about the 3.3 V pin?

Measuring Current

Current
is the rate of movement of charges in a circuit and is measured in
amperes
(
amps
). In order to capture the rate of moving charges and thus measure current, you need to break the circuit and connect the meter in-line at the place where you want to measure current. Adjust the knob to the appropriate current range that you expect to measure. If you’re measuring anything that might be above 200 mA, switch the selection knob to 10A and move the red probe into the port marked 10A on the body of the multimeter. If you’re not sure, this is the safest range to start with, to avoid damage to your meter.

To measure the current going through a simple LED and resistor circuit, for example, you could splice into the circuit between the LED and the resistor (see
Figure A-2
). The current path must go through the meter. Because this is a series circuit, you could measure the current anywhere along this path: before the LED, after the LED, or after the resistor.

FIGURE A-2:
Splicing a multimeter into a circuit to measure current

When measuring current, be very careful not to exceed the limits of your multimeter—you should be able to find the range of current your multimeter can handle from its user manual. If you go beyond the current limits, you run the risk of also blowing a fuse on the multimeter. (Don’t worry if you blow the fuse—a replacement is pretty inexpensive. In order to swap in the new fuse, you’ll probably need to open the back of the multimeter with a screwdriver.) The standard mAVΩ port can usually handle up to 200 mA.

HOW TO SOLDER

Soldering is one of the most basic skills used in prototyping electronics projects. It involves melting a special metal,
solder
, between two components to hold them together more permanently (see
Figure A-3
).

FIGURE A-3:
Soldering

Other books

Never Enough by Joe McGinniss
Sweetness by Pearlman, Jeff
Thirst by Ilia Bera
Forbidden by Jacquelyn Frank
Shattered Circle by Linda Robertson
Desert Kings by James Axler