80
SIK GUIDE Your Guide to the SparkFun Inventor’s Kit for Arduino

Guía arduino

Embed Size (px)

Citation preview

Page 1: Guía arduino

SIK GUIDEYour Guide to the SparkFun Inventor’s Kit for Arduino

AREF GN

D 13 12 11 10˜9

8

6 5

7

4 3 2 1 0

DIGITAL (PWM )

RESET

LTX RX

SIK GUIDEYour Guide to the SparkFun Inventor’s Kit for Arduino

AREF GN

D 13 12 11 10˜9

8

6 5

7

4 3 2 1 0

DIGITAL (PWM )

RESET

LTX RX

Page 2: Guía arduino

The SparkFun Inventor's Guide is your map for navigating the waters of beginning embedded electronics. This booklet contains all the information you will need to explore the 14 circuits of the SparkFun Inventor's Kit for Arduino. At the center of this manual is one core philosophy - that anyone can (and should) play around with electronics. When you're done with this guide, you'll have the know-how to start creating your own projects and experiments. Now enough talking - let's get inventing!

sparkfun.com

Welcome to the SparkFun Inventor’s Guide

Table of Contents

Page 3: Guía arduino

What is an Arduino? 1

Download Arduino Software (IDE) 3

Install Drivers for Arduino 4

Identify your Arduino 7

Download “SIK Guide Code” 8

The World Runs on Circuits 9

Inventory of Parts 11

13

15

17

24

28

32

36

40

44

48

52

Section 2: Getting Started with Circuits

Section 1: Getting Started with Arduino

Arduino Uno

Breadboard

Circuit #1 - Your First Circuit: Blinking a LED

Circuit #3 - RGB LED

Circuit #4 - Multiple LEDs

Circuit #5 - Push Buttons

Circuit #6 - Photo Resistor

Circuit #7 - Temperature Sensor

Circuit #8 - A Single Servo

Circuit #9 - Flex Sensor

56Circuit #10 - Soft Potentiometer

60Circuit #11 - Piezo Element

64Circuit #12 - Spinning a Motor

68Circuit #13 - Relay

72Circuit #14 - Shift Register

Circuit #2 - Potentiometer

Page 4: Guía arduino

Arduino is an open-source physical computing platform designed to make experimenting with electronics more fun and intuitive. Arduino has its own unique, simplified programming language, a vast support network, and thousands of potential uses, making it the perfect platform for both beginner and advanced DIY enthusiasts.

arduino.cc

The Arduino Revolution

What is an Arduino?

The friendly blue board in your hand (or on your desk) is the Arduino. In some ways you could think of Arduino as the child of traditional desktop and laptop computers. At its roots, the Arduino is essentially a small portable computer. It is capable of taking inputs (such as the push of a button or a reading from a light sensor) and interpreting that information to control various outputs (like a blinking LED light or an electric motor).

That's where the term "physical computing" is born - an Arduino is capable of taking the world of electronics and relating it to the physical world in a real and tangible way. Trust us - this will all make more sense soon.

A Computer for the Physical World

// Arduino UNO SMD R3

The Arduino Uno is one of several development boards based on the ATmega328. We like it mainly because of its extensive support network and its versatility. It has 14 digital input/output pins (6 of which can be PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. Don’t worry, you’ll learn about all these later.

Page 5: Guía arduino

cb

a

d

e

f

g

h

c

b

a

f

e

d

h

g

Power-Lacing High Tops

Old Toy Email Notifer

Bug Zapper Counter

Re-Programmed Traffic Light

Auto-Plant Watering

Camera Time-lapse operation

Quadcopter

Auto-Coffee Maker

Page 6: Guía arduino

In order to get your Arduino up and running, you'll need to download some software first from www.arduino.cc (it's free!). This software, known as the Arduino IDE, will allow you to program the Arduino to do exactly what you want. It’s like a word processor for writing programs. With an internet-capable computer, open up your favorite browser and type in the following URL into the address bar:

Access the Internet

Download the Arduino IDE (Integrated Development Environment)

user

•••••••••

Windows

Mac OS X

Linux: 32 bit, 64 bit

N

W

S

E

NW

SW

SE

NE

arduino.cc/en/Main/Software < case sensitive >

Choose the appropriate Operating System installation package for your computer.

Download Click on the “ + ” sign next to your appropriate computer operating system.

Windows

Mac OS X

source

Linux: 32 bit, 64 bit

1

Page 7: Guía arduino

// Connect your Arduino Uno to your ComputerUse the USB cable provided in the SIK kit to connect the Arduino to one of your computer’s USB inputs.

// Install Drivers

Depending on your computer’s operating system, you will need to follow specific instructions. Please consult the URLs below for specific instructions on how to install the drivers onto your Arduino Uno.

* You will need to scroll to the section labeled “Install the drivers”.

23

Linux: 32 bit / 64 bit, Installation ProcessGo to the web address below to access the instructions for installations on a Linux-based computer.

http://www.arduino.cc/playground/Learning/Linux

Macintosh OS X Installation ProcessMacs do not require you to install drivers. Enter the following URL if you have questions. Otherwise proceed to next page. http://arduino.cc/en/Guide/MacOSX

Windows Installation ProcessGo to the web address below to access the instructions for installations on a Windows-based computer.

http://arduino.cc/en/Guide/Windows

Page 8: Guía arduino

1 2 3 5 6

7

8

9

4

Open the Arduino IDE software on your computer. Poke around and get to know the interface. We aren’t going to code right away, this is just an introduction. �e step is to set your IDE to identify your Arduino Uno.

// Open the Arduino IDE:

Page 9: Guía arduino

// The three most important commands for this guide are seen below:

GUI (Graphical User Interface)

Verify: Compiles and approves your code. It will catch errors in syntax (like missing semi-colons or parenthesis). // See Diagram Below1

Upload: Sends your code to the Arduino board. When you click it, you should see the lights on your board blink rapidly. // See Diagram Below2

New: This buttons opens up a new code window tab.3

Open: This button will let you open up an existing sketch. // See Diagram Below4

Save: This saves the currently active sketch.5

Serial Monitor: This will open a window that displays any serial information your Arduino is transmitting. It is very useful for debugging.6

Code Area: This is the area where you compose the code for your sketch. 8

Message Area: This is where the IDE tells you if there were any errors in your code.9

Sketch Name: This shows the name of the sketch you are currently working on.7

Verify

Upload

Open

Page 10: Guía arduino

File Edit Sketch Tools Help

Auto FormatArchive SketchFix Encoding & ReloadSerial Monitor

Arduino UnoArduino Duemilanove w/ ATmega328]Arduino Diecimila or Duemilanove w/ ATmega168Arduino Nano w/ ATmega328Arduino Nano w/ ATmega168Arduino Mega 2560 or Mega ADKArduino Mega (ATmega1280)Arduino Mini Arduino Mini w/ATmega168Arduino EthernetArduino FioArduino BT w/ ATmega328Arduino BT w/ATmega168LilyPad Arduino w/ ATmega328LilyPad Arduino w/ ATmega168Arduino Pro or Pro Mini (5V, 16 MHz) w/ATmega328Arduino Pro or Pro Mini (5V, 16 MHz) w/ATmega168Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ATmega328Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ATmega168Arduino NG or older w/ ATmega168Arduino NG or older w/ ATmega8

ProgrammerBurn Bootloader

BoardSerial Port

// Select your board: Arduino Uno

Select the serial device of the Arduino board from the Tools | Serial Port menu. This is likely to be

com3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your Arduino board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port.

Select the serial device of the Arduino board from the Tools > Serial Port menu. On the Mac, this should be something with /dev/tty.usbmodem (for the Uno or Mega 2560) or /dev/tty.usbserial (for older boards) in it.

http://www.arduino.cc/playground/Learning/Linux

Tools Help

Auto FormatArchive SketchFix Encoding & ReloadSerial Monitor

com 1com 12

ProgrammerBurn Bootloader

BoardSerial Port

Tools Help

Auto FormatArchive SketchFix Encoding & ReloadSerial Monitor

/dev/tty.usbmodem262471 /dev/cu.usbmodem262471 /dev/tty.Bluetooth-Modem /dev/cu.Bluetooth-Modem /dev/tty.FireFly-7256-SPP /dev/cu.FireFly-7256-SPP /dev/tty.tiPhone-WirelessiAP-1 /dev/cu.tiPhone-WirelessiAP-1 /dev/tty.Bluetooth-PDA-Sync /dev/cu.Bluetooth-PDA-Sync

ProgrammerBurn Bootloader

BoardSerial Port

// S

elec

t you

r Ser

ial D

evic

e

4W

WW

.ARD

UINO

.CC

RESET

A5ANALOG IN

WW

W.A

RDUI

NO.C

C

RESET

A5ANALOG IN

Page 11: Guía arduino

Type in the following URL to download the code:

Download Arduino Code (For use with the circuits in this guide)

sparkfun.com/sikcode

5

Unzip the file “SIK Guide Code”. It should be located in your browser’s “Downloads” folder. Right click the zipped folder and choose “unzip”.

Copy the “SIK Guide Code” folder into Arduino’s folder named “examples”.

Copy the “SIK Guide Code” folder into Arduino’s folder named “examples”.

Unzip the file “SIK Guide Code”. It should be loacted in your browser’s “Downloads” folder. Right click the zipped folder and choose “unzip”.

Find “Arduino” in your applications folder. Right click(ctrl + click) on “Arduino”. Select “Show Package Contents”.

http://www.arduino.cc/playground/Learning/Linux

// C

opy

“SIK

Gui

de C

ode”

into

“Ex

ampl

es”

libra

ry in

Ard

uino

fold

er

ProgramsStart arduino examples

Contents

Resources

Java

examplesArduino

Move to Trash

Open

Show Package Contents

Page 12: Guía arduino

Everywhere you look, you'll find circuits. The cell phone in your pocket, the computer that controls your car's emissions system, your video game console - all these things are chock full of circuits. In this guide, you'll experiment with some simple circuits and learn the gist of the world of embedded electronics.

The World Runs on Circuits:

Getting Started with Circuits

A circuit is basically an electronics loop with a starting point and an ending point - with any number of compo-nents in between. Circuits can include resistors, diodes, inductors, sensors of all sizes and shapes, motors, and any other handful of hundreds of thousands of components.

Circuits are usually divided into three categories - analog circuits, digital circuits, or mixed-signal circuits. In this guide, you will explore all three sets of circuits.

What is an Electrical Circuit?

// Simple and Complex Circuits

In this guide, you will be primarily exploring simple circuits - but that doesn't mean you can't do amazing things with simple tools! When you've finished the SIK, your knowledge of circuits will enable you to explore amazing projects and unleash the power of you imagination.

a b c d e f g h i123456789101112131415161718192021222324252627282930

a b c d e f g h i

123456789101112131415161718192021222324252627282930

Page 13: Guía arduino
Page 14: Guía arduino

Inventory of Parts

* ACTUAL SIZE

(1N4148)Diode

x2

x1

Piezo Element

Various ColorsJumper Wire

x30

Push Button

x2x1

DC Motor

x1

DC Motor

x1

Potentiometer

(Light Emitting Diode)

x10 x10 x1

LED (5mm) +-

330Ω Resistor

x25 * ACTUAL SIZE

10KΩ Resistor

x25 * ACTUAL SIZE

(TMP36)

x1

Temp. Sensor

FRONT

BACK

(P2N2222AG)

x2

Transistor

P2N2

222A

A18

FRONT

BACK

x1

Photo Resistor

Page 15: Guía arduino

Standard Solderless Breadboard

a b c d e f g h i123456789101112131415161718192021222324252627282930

a b c d e f g h i

123456789101112131415161718192021222324252627282930

UNO - PTH VersionArduino Board

AREFGND

13121110

98

65

7

43210

3.3V5V

RESET

GNDGNDVIN

DIGITAL (PW

M )

WWW.ARDUINO.CC

POW

ER

RESET

RESET-EN

RX TX L

A1 A2

A0

A3A4A5

MADE

IN ITA

LY

ANALO

G IN

TXRX

Standard Solderless Breadboard

a b c d e f g h i123456789101112131415161718192021222324252627282930

a b c d e f g h i

123456789101112131415161718192021222324252627282930

UNO - SMD VersionArduino BoardFlex Sensor

x1

Soft Potentiometer

x1

Servo

x1

Relay

x1

x1

x1

(IC)Integrated Circuit

x1

Page 16: Guía arduino

3

1

2

4 5

7

8

9

6

10

Page 17: Guía arduino

// Pins Diagram

Arduino Uno

Power In (Barrel Jack) - Can be used with either a 9V or 12V wall-wart or battery.1

Power In (USB Port) - Provides power and communicates with your board when plugged into your computer via USB.2

LED (RX: Receiving) - This shows when the Arduino is receiving data (such as when being programmed).3

LED (TX: Transmitting) - This shows when your Arduino is transmitting data (such as when running a program). 4

LED (Pin 13: Troubleshooting) - This LED is incorporated into your sketch to show if your program is running properly.5

Pins (ARef, Ground, Digital, Rx, Tx) - These various pins can be used for inputs, outputs, power, and ground. // See Diagram Below 6

LED (Indicates Arduino is ON) - This is a simple power indicator LED.7

Reset Button - This is a way to manually reset your Arduino, which makes your code restart.8

Pins (Analog In, Power In, Ground, Power Out, Reset) - These various pins can be used for inputs, outputs, power, and ground. // See Diagram Below10

ICSP Pins (Uploading Code without Bootloader) - This is for "In-Circuit Serial Programming," used if you want to bypass the boot loader.9

Power Out

Reset

IOREF

RFU

Power Out

Ground

Ground

Power In

Analog

Analog

Analog

Analog

Analog

Analog

Ground

ARef

SDA

SCL

Digital

Digital

Digital

Digital

Digital

Digital

Digital

Digital

Digital

Digital

Digital

Digital

TX - Out

RX - In

10

6

= PWM/Analog out compatible (i.e. )

The header pins are one of the most important parts for putting our example circuits together. Take a moment and locate the input/output ports of your Arduino Uno.

Page 18: Guía arduino

a b c d e f g h i123456789101112131415161718192021222324252627282930

a b c d e f g h i

123456789101112131415161718192021222324252627282930

This line divides the board in half, restricting electricity to one half or the other.

1

2

Page 19: Guía arduino

Power:

Each + sign runs power anywhere in the vertical column.

Ground:

Each - sign runs to ground anywhere in the vertical column.

Horizontal Rows:

Each of these rows numbered 1-30 are comprised of five horizontal sockets. Components placed in the same row will be connected in a circuit when power is running.

Vertical Connection (+ Power and - Ground // See Diagram Below)1

Horizontal Connection (a-e & f-j // See Diagram Below)2

How’s it all connected?

View of the inside

f g h ia b c d e123456789101112131415161718192021222324252627282930

a b c d e f g h i

123456789101112131415161718192021222324252627282930

Breadboard

Above the breadboard

CONNECTED!

LED

Making a Connection:

Inside the breadboard

Page 20: Guía arduino

CIRCUIT #1 - Your First Circuit

5V Current Your Arduino runs on �ve volts. �is is the power that will be supplied from your computer via USB and will be the driving force behind any components you use in your circuits. By plugging your Arduino board into your computer, you are supplying it with just the right voltage it needs to thrive! 5V can’t hurt you, so don’t be afraid to touch anything in your citcuit.

Peel sticker off back of Breadboard and stick into place.

How It Works:

ASSEMBLE WRITE UPLOAD

Make sure the text on the Arduino and Breadboard are facing up so you can read them.

a b c d e f g h i123456789101112131415161718192021222324252627282930

a b c d e f g h i

123456789101112131415161718192021222324252627282930

Screw the Arduino board down and into place.

Connect the USB cable.

Page 21: Guía arduino

PART

S: LED

1X

Wire

3X

330ΩResistor

1X This section lists the parts you will need to complete the circuit.

Circuit 2

LEDs (light-emitting diodes) are small, powerful lights that are used in many di�erent applications. To start o� the SIK, we will work on blinking an LED. �at's right - it's as simple as turning a light on and o�. It might not seem like much, but establishing this important baseline will give you a solid foundation as we work toward more complex experiments.

Blinking a LED 1

Each Circuit begins with a brief description of the what you are putting together and the expected result.

This is a schematic of your circuit.

Arduino

LED(Light Emitting Diode)

GND(ground) (-)

Resistor(Orange-Orange-Brown)

(330ohm)

This is an illustration of how the completed circuit should look. It is not necessary to use the black holder for the Arduino and Breadboard, but we recommend it for the first time inventor!

Components like Resistors need to have their legs bent into 90° angles in order to correctly fit the breadboard sockets.

Pin 13

Page 22: Guía arduino

Circ

uit 1

: Blin

king

a L

ED

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

LED:

Mak

e su

re th

e sh

ort l

eg,

mar

ked

with

flat

side

, goe

s int

o th

e ne

gativ

e po

sitio

n (-)

. 33

0Ω R

esis

tor:

The

colo

r ban

ding

sh

ould

read

ora

nge-

oran

ge-

brow

n-go

ld. T

he co

mpo

nent

legs

ca

n go

in e

ither

hol

e.

Jum

per W

ire: A

ll ju

mpe

r wire

s w

ork

the

sam

e. T

hey

are

used

to

conn

ect t

wo

poin

ts to

geth

er. T

his

guid

e w

ill sh

ow th

e w

ires w

ith d

iffer

ent

colo

red

insu

latio

ns fo

r cla

rity,

but

usi

ng

diffe

rent

com

bina

tions

of c

olor

s is

com

plet

ely

acce

ptab

le.

Fla

t E

dg

e

Sho

rt L

eg

Page 23: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

LED

(5m

m)

+-

+-

c2c3

Jum

per W

irePi

n 13

e2

330Ω

Res

isto

ra3

GN

Da3

Jum

per W

ireG

ND

Jum

per W

ire5V5V

+“5

V” o

n th

e Ar

duin

o co

nnec

ts to

the

row

mar

ked

“+”

on th

e Br

eadb

oard

.

“Pin

13”

on

the

Ardu

ino

conn

ects

to so

cket

“e2”

on

the

Brea

dboa

rd.

“GN

D”

on th

e Ar

duin

o sh

ould

be

conn

ecte

d to

the

row

mar

ked

“-”

on th

e Br

eadb

oard

.

Com

pone

nts l

ike

LEDs

are

inse

rted

into

the

Brea

dboa

rd so

cket

s c2(

long

leg)

c3(

shor

t leg

).

Resis

tors

are

pla

ced

in B

read

boar

d so

cket

s onl

y. Th

e “-

” sym

bol r

epre

sent

s any

sock

et in

its v

ertic

al co

lum

n.

Brea

dboa

rd: T

he w

hite

bac

kgro

und

repr

esen

ts a

conn

ectio

n to

a

brea

dboa

rd so

cket

.

Ardu

ino:

The

blu

e ba

ckgr

ound

re

pres

ents

a co

nnec

tion

to o

ne o

f th

e Ar

duin

o he

ader

pin

s.

Page 24: Guía arduino

Open Up the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open the code for Circuit 1 by accessing the “SIK Guide Code” you downloaded and placed into your “Example” folder earlier.

Open Your First Sketch:

File Edit Sketch Tools Help

1.Basics2.Digital3.Analog4.Communication5.Control6.Sensors7.Displays8.StringsArduinoISPSIK Guide Code

EEPROMEthernetFirmataLiquid CrystalSDServoSoftwareSerialSPIStepperWire

Page SetupPrint

NewOpen...SketchbookExamplesCloseSaveSave As...UploadUpload Using Progammer

Circuit #1Circuit #2Circuit #3Circuit #4Circuit #5Circuit #6Circuit #7Circuit #8Circuit #9Circuit #10Circuit #11Circuit #12Circuit #13Circuit #14

Circuit #1

/* Blink

Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain.

*/

void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT); }

void loop() { digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(13, LOW); // set the LED off delay(1000); // wait for a second}

// Circuit #1

Page 25: Guía arduino

Verify

Upload

// The result of a completed circuit with correct code after verified and uploaded.

�is compiles your code. �e IDE changes it from text into instructions the computer can understand.

�is sends the instructions via the USB cable to the computer chip on the Arduino board. �e Arduino will then begin running your code automatically.

Page 26: Guía arduino

Circuit 2 Arduino Code:1

Troubleshooting:

LED Not Lighting Up?LEDs will only work in one direction. Try taking it out and twisting it 180 degrees (no need to worry, installing it backwards does no permanent harm).

Program Not Uploading �is happens sometimes, the most likely cause is a confused serial port, you can change this in tools>serial port>

Still No Success?A broken circuit is no fun, send us an e-mail and we will get back to you as soon as we can: [email protected]

You should see your LED blink on and o�. If it isn't, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

Before you can use one of the Arduino's pins, you need to tell the Arduino whether it is an INPUT or OUTPUT. We use a built-in "function" called pinMode() to do this.

When you're using a pin as an OUTPUT, you can command it to be HIGH (output 5 Volts), or LOW (output 0 Volts).

digitalWrite(13, HIGH);

pinMode(13, OUTPUT);

Code to Note:

Real World Application:

Almost all modern �at screen televisions and monitors have LED indicator lights to show they are on or o�.

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 1

What you Should See:

This is where you will find the Arduino code for each circuit.

Remember to Verify and Upload your code.

See if your circuit is complete and working in this section.

Begin to understand how the Arduino code works. See below.

This is a section dedicated to the most common mistakes made while assembling the circuit.

Here you will find examples of the circuit you just completed in the real world. Many of the theories in these circuits are used in things you use everyday!

Page 27: Guía arduino

Pin 13

Pin AØ

Circuit 2PA

RTS: Wire

19X

CIRCUIT #5

IC

1X

330ΩResistor

8X

LED

8X

Circuit 2

In this circuit you’ll work with a potentiometer. A potentiometer is also known as a variable resistor. When it’s connected with 5 volts across its two outer pins, the middle pin outputs a voltage between 0 and 5, depending on the position of the knob on the potentiometer. In this circuit, you’ll learn how to use a potentiometer to control the brightness of an LED.

Potentiometer

PART

S: Wire

6X

CIRCUIT #2 2

LED

1X

330ΩResistor

1X

Arduino PotentiometerArduino

+5 Volts

LED

resistor(Orange-Orange-Brown)

GND(ground) (-)

(330ohm)

Potentiometer

1X

p.10p.24

Page 28: Guía arduino

Circ

uit 2

: Pot

entio

met

er

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 29: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Pote

ntio

met

er

e6Ju

mpe

r Wire

e8Ju

mpe

r Wire

Jum

per W

ireG

ND

Jum

per W

ire5V

Pin

13j2

0

Jum

per W

ireG

ND

Jum

per W

ire5V5V

+

+-

+-

h20

h21

LED

(5m

m)

330Ω

Res

isto

ri2

1+

Jum

per W

ireA0

e7

+

a6 a7 a8If

you

look

clos

ely at

you

r Ard

uino

, you

'll se

e som

e pin

s lab

eled

"DIG

ITAL

", an

d so

me l

abele

d "A

NAL

OG

". W

hat's

the d

i�er

ence

?

Man

y of

the d

evice

s you

'll in

terfa

ce to

, suc

h as

LED

s and

pus

hbut

tons

, hav

e on

ly tw

o po

ssibl

e sta

tes:

on an

d o�

, or a

s the

y're

kno

wn

to th

e Ard

uino

, "H

IGH

" (5

Vol

ts) an

d "L

OW

" (0

Vol

ts). �

e dig

ital p

ins o

n an

Ard

uino

are

grea

t at g

ettin

g th

ese s

igna

ls to

and

from

the o

utsid

e wor

ld, a

nd ca

n ev

en d

o tri

cks l

ike s

imul

ated

dim

min

g (b

y bl

inki

ng o

n an

d o�

reall

y fa

st), a

nd se

rial

com

mun

icatio

ns (t

rans

ferr

ing

data

to an

othe

r dev

ice b

y en

codi

ng it

as p

atte

rns

of H

IGH

and

LOW

).

But t

here

are a

lso a

lot o

f thi

ngs o

ut th

ere t

hat a

ren'

t jus

t "on

" or "

o�".

Tem

pera

ture

leve

ls, co

ntro

l kno

bs, e

tc. a

ll ha

ve a

cont

inuo

us ra

nge o

f valu

es

betw

een

HIG

H an

d LO

W. F

or th

ese s

ituat

ions

, the

Ard

uino

o�e

rs six

analo

g in

puts

that

tran

slate

an in

put v

olta

ge in

to a

num

ber t

hat r

ange

s fro

m 0

(0 V

olts)

to

102

3 (5

Vol

ts). �

e ana

log

pins

are p

erfe

ct fo

r mea

surin

g all

thos

e "re

al w

orld

" valu

es, a

nd al

low

you

to in

terfa

ce th

e Ard

uino

to al

l kin

ds o

f thi

ngs.

Digi

tal v

ersu

s An

alog

:

DIGI

TAL

0 vo

lts 0

5 vo

lts

1023

ANAL

OG

HIGH

on 5 vo

lts

LOW

off

0 vo

lts

Page 30: Guía arduino

MP3 players’ volume control is an example of a potentiometer in action.

Circuit 2 Arduino Code:2

Troubleshooting:

Sporadically Working �is is most likely due to a slightly dodgy connection with the potentiometer's pins. �is can usually be conquered by holding the potentiometer down.

Not WorkingMake sure you haven't accidentally connected the potentiometer's wiper to digital pin 2 rather than analog pin 2. (the row of pins beneath the power pins).

Still BackwardYou can try operating the circuit upside down. Sometimes this helps.

You should see the LED blink faster or slower in accordance with your potentiometer. If it isn't working, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

A "variable" is a number you've given a name to. You must introduce, or "declare" variables before you use them; here we're declaring a variable called sensorValue, of type "int" (integer). Don't forget that variable names are case-sensitive!

int sensorValue;

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 2

What you Should See:

The Arduino is very very fast, capable of running thousands of lines of code each second. To slow it down so that we can see what it's doing, we'll often insert delays into the code. Delay() counts in milliseconds; there are 1000 ms in one second.

delay(sensorValue);

We use the analogRead() function to read the value on an

analog pin. analogRead() takes one parameter, the analog

pin you want to use ("sensorPin"), and returns a number

("sensorValue") between 0 (0 Volts) and 1023 (5 Volts).

sensorValue = analogRead(sensorPin);

Page 31: Guía arduino

Circuit 2PA

RTS:

p.10

IC

1X

330ΩResistor

8X

LED

8X

You know what’s even more fun than a blinking LED? A colored one. RGB, or red-green-blue, LEDs have three di�erent color-emitting diodes that can be combined to create all sorts of colors. In this circuit, you’ll learn how to use an RGB LED to create unique color combinations. Depending on how bright each diode is, nearly any color is possible!

RGB LED

PART

S:CIRCUIT #3 3

p.28

Potentiometer

1X

TransistorP2N2222AG

1X

Pin 11 Pin 10 Pin 9

red

green

blue

resistor(330ohm)(Orange-Orange-Brown)

GND(ground) (-)

Wire

6X

330ΩResistor

3X

LED

1X

red

commongreen

blue

Page 32: Guía arduino

Circ

uit 3

: RGB

LED

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

* T

he lo

nges

t le

ad is

the

co

mm

on(

gnd

).

Page 33: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

e7e1

15V

330Ω

Res

isto

rg4

e4 e7e1

15V

330Ω

Res

isto

rg6

e6 e7e1

15V

330Ω

Res

isto

rg7

e7

RGB

LED

(5m

m)

Jum

per W

ireG

ND

Jum

per W

ire5V5V

+

Jum

per W

irePi

n 9

h4

Jum

per W

irePi

n 10

h6

Jum

per W

irePi

n 11

h7

e5Ju

mpe

r Wire

We'v

e se

en th

at th

e Ar

duin

o ca

n re

ad a

nalo

g vo

ltage

s (vo

ltage

s bet

wee

n 0

and

5 V

olts)

usin

g th

e an

alog

Rea

d() f

unct

ion.

Is th

ere

a w

ay fo

r the

Ard

uino

to

outp

ut a

nalo

g vo

ltage

s as w

ell?

�e

answ

er is

no.

.. an

d ye

s. �

e Ar

duin

o do

es n

ot h

ave

a tr

ue a

nalo

g vo

ltage

ou

tput

. But

, bec

ause

the

Ardu

ino

is so

fast,

it c

an fa

ke it

usin

g so

met

hing

ca

lled

PW

M ("

Pul

se-W

idth

Mod

ulat

ion"

).

�e

Ardu

ino

is so

fast

that

it c

an b

link

a pi

n on

and

o�

alm

ost 1

000

times

per

se

cond

. PW

M g

oes o

ne st

ep fu

rthe

r by

vary

ing

the

amou

nt o

f tim

e th

at th

e bl

inki

ng p

in sp

ends

HIG

H v

s. th

e tim

e it

spen

ds L

OW

. If i

t spe

nds m

ost o

f its

tim

e H

IGH

, a L

ED c

onne

cted

to th

at p

in w

ill a

ppea

r brig

ht. I

f it s

pend

s m

ost o

f its

time

LOW

, the

LED

will

look

dim

. Bec

ause

the

pin

is bl

inki

ng

muc

h fa

ster t

han

your

eye

can

det

ect,

the

Ardu

ino

crea

tes t

he il

lusio

n of

a

"tru

e" a

nalo

g ou

tput

.

The

shoc

king

trut

h be

hind

ana

logW

rite(

):

90%90

%

50%

50%

0.5

v

2.5

v

4.5

v

LOW

(0 v

olts

)

HIG

H (5

vol

ts)

LOW

(0 v

olts

)

HIG

H (5

vol

ts)

LOW

(0 v

olts

)

HIG

H (5

vol

ts)

10%

10%

a4a5

a6a7

Page 34: Guía arduino

Many electronics such as videogame consoles use RGB LEDs to have the versatility to show di�erent colors in the same area. Often times the di�ent colors represent di�erent states of working condition.

Circuit 2 Arduino Code:3

Troubleshooting:

LED Remains Dark or Shows Incorrect ColorWith the four pins of the LED so close together, it’s sometimes easy to misplace one. Double check each pin is where it should be.

Seeing Red�e red diode within the RGB LED may be a bit brighter than the other two. To make your colors more balanced, use a higher ohm resistor. Or adjust in code.

analogWrite(RED_PIN, redIntensity); to

analogWrite(RED_PIN, redIntensity/3);

You should see your LED turn on, but this time in new, crazy colors! If it isn't, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 3

What you Should See:

A for() loop is used to step a number across a range, and repeatedly runs code within the brackets {}. Here the variable "x" starts a 0, ends at 767, and increases by one each time ("x++").

for (x = 0; x < 768; x++){}

The Arduino is very very fast, capable of running thousands of lines of code each second. To slow it down so that we can see what it's doing, we'll often insert delays into the code. Delay() counts in milliseconds; there are 1000 ms in one second.

delay(sensorValue);

"If / else" statements are used to make choices in your programs. The

statement within the parenthesis () is evaluated; if it's true, the code within

the first brackets {} will run. If it's not true, the code within the second

brackets {} will run.

if (x <= 255){}else{}

Page 35: Guía arduino

So you have gotten one LED to blink on and o� – fantastic! Now it's time to up the stakes a little bit – by connecting EIGHT LEDS AT ONCE. We'll also give our Arduino a little test by creating various lighting sequences. �is circuit is a great setup to start practicing writing your own programs and getting a feel for the way Arduino works.

Along with controlling the LEDs, you’ll learn about a couple programming tricks that keep your code neat and tidy:

for() loops - used when you want to run a piece of code several times

arrays[ ] - used to make managing variables easier by grouping them together

Multiple LEDsPin 2 Pin 3 Pin 4 Pin 5

GND

LEDLight Emitting Diode

LEDLight Emitting Diode

resistor(330ohm)(Orange-Orange-Brown)

resistor(330ohm)(Orange-Orange-Brown)

Pin 6 Pin 7 Pin 8 Pin 9

GND(ground) (-)

p.32

PART

S: LED

8X

Wire

10X

330ΩResistor

8X

CIRCUIT #4 4Pin 2 Pin 3 Pin 4 Pin 5

Pin 6 Pin 7 Pin 8 Pin 9

Page 36: Guía arduino

Circ

uit 4

: Mul

tiple

LED

s

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 37: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Com

pone

nt:

Imag

e Re

fere

nce:

330Ω

Res

isto

ra1

8G

ND

330Ω

Res

isto

r

330Ω

Res

isto

r

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire5V

Jum

per W

ire

LED

(5m

m)

+-

+-

c2c3

e2Pi

n 2

330Ω

Res

isto

ra1

5G

ND

330Ω

Res

isto

ra1

2G

ND

330Ω

Res

isto

ra9

GN

D

330Ω

Res

isto

ra6

GN

D

330Ω

Res

isto

ra3

GN

Dc2

3c2

4

LED

(5m

m)

+-

+-

c23

c24

LED

(5m

m)

+-

+-

c20

c21

LED

(5m

m)

+-

+-

c17

c18

LED

(5m

m)

+-

+-

c14

c15

LED

(5m

m)

+-

+-

c11

c12

LED

(5m

m)

+-

+-

c8c9

LED

(5m

m)

+-

+-

c5c6

a3G

ND

a24

GN

D

a21

GN

D

Pin

3

Pin

3

e5

GN

D-

Pin

4e8

Pin

5e1

1

Pin

6e1

4

Pin

7e1

7

Pin

8e2

0

Pin

9e2

3 +

Page 38: Guía arduino

Circuit 2 Arduino Code:4

Troubleshooting:

Some LEDs Fail to Light It is easy to insert an LED backwards. Check the LEDs that aren't working and ensure they the right way around.

Operating out of sequenceWith eight wires it's easy to cross a couple. Double check that the �rst LED is plugged into pin 2 and each pin there after.

Starting AfreshIts easy to accidentally misplace a wire without noticing. Pulling everything out and starting with a fresh slate is often easier than trying to track down the problem.

�is is similar to circuit number one, but instead of one LED, you should see all the LEDs blink. If they aren't, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

Code to Note:

Real World Application:

Scrolling marquee displays are generally used to spread short segments of important information. �ey are built out of many LEDs.

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 4

What you Should See:

When you have to manage a lot of variables, an "array" is a handy way to group them together. Here we're creating an array of integers, called ledPins, with eight elements.

int ledPins[] = {2,3,4,5,6,7,8,9};

Computers like to do the same things each time they run. But sometimes you want to do things randomly, such as simulating the roll of a dice. The random() function is a great way to do this. See http://arduino.cc/en/Reference/Random for more information.

index = random(8);

You refer to the elements in an array by their position. The first

element is at position 0, the second is at position 1, etc. You refer to

an element using "ledPins[x]" where x is the position. Here we're

making digital pin 2 HIGH, since the array element at position 0 is "2".

digitalWrite(ledPins[0], HIGH);

Page 39: Guía arduino

Circuit 2PA

RTS: Wire

19X

IC

1X

330ΩResistor

8X

LED

8X

Circuit 2

Up until now, we’ve focused solely on outputs. Now we’re going to go to the other end of spectrum and play around with inputs. In this circuit, we’ll be looking at one of the most common and simple inputs – a push button. �e way a push button works with Arduino is that when the button is pushed, the voltage goes LOW. �e Arduino reads this and reacts accordingly. In this circuit, you will also use a pull-up resistor, which helps clean up the voltage and prevents false readings from the button.

Push Buttons

PART

S: Wire

7X

CIRCUIT #5 5Pin 2

+5 Volts

resistor

Pin 3

LED

GND(ground) (-)

resistor(Orange-Orange-Brown)

(330ohm)

Push Button

2X

LED

1X

330ΩResistor

1X

10KΩResistor

2X

Pin 13

Pin 2 Pin 3

p.10p.36

Page 40: Guía arduino

Circ

uit 5

: Pus

h Bu

ttons

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 41: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

LED

(5m

m)

+-

+-

h20

h21

Push

But

ton

d4g4

d6g6

Push

But

ton

d9g9

d11

g11

Jum

per W

irePi

n 2

h6

Jum

per W

irePi

n 3

h11

Jum

per W

irePi

n 13

j20

i4Ju

mpe

r Wire

i9Ju

mpe

r Wire

10KΩ

Res

isto

ra1

5i6

+

10KΩ

Res

isto

ri1

1+

330Ω

Res

isto

rj2

1+

One

of t

he th

ings

that

mak

es th

e Ard

uino

so u

sefu

l is t

hat i

t can

mak

e com

plex

dec

ision

s ba

sed

on th

e inp

ut it

's ge

tting

. For

exam

ple,

you

coul

d m

ake a

ther

mos

tat t

hat t

urns

on

a he

ater

if it

get

s too

cold

, a fa

n if

it ge

ts to

o ho

t, w

ater

s you

r plan

ts if

they

get

too

dry,

etc.

In o

rder

to m

ake s

uch

decis

ions

, the

Ard

uino

pro

vide

s a se

t of l

ogic

oper

atio

ns th

at le

t you

bu

ild co

mpl

ex "i

f" st

atem

ents.

�ey

inclu

de:

You

can

com

bine

thes

e fun

ctio

ns to

bui

ld co

mpl

ex if

() sta

tem

ents.

For e

xam

ple:

if ((

mod

e ==

hea

t) &

& ((

tem

pera

ture

< th

resh

old)

|| (o

verr

ide

== tr

ue))

){ di

gita

lWri

te(H

EAT

ER, H

IGH

);} ...

will

turn

on

a hea

ter i

f you

're in

hea

ting

mod

e AN

D th

e tem

pera

ture

is lo

w, O

R if

you

tu

rn o

n a m

anua

l ove

rrid

e. U

sing

thes

e log

ic op

erat

ors,

you

can

prog

ram

you

r Ard

uino

to

mak

e int

ellig

ent d

ecisi

ons a

nd ta

ke co

ntro

l of t

he w

orld

arou

nd it

!

How

to u

se lo

gic

like

a Vu

lcan

:

==

EQUI

VALE

NCE

A ==

B is

true

if A

and

B a

re th

e SA

ME.

!=DI

FFER

ENCE

A !=

B is

true

if A

and

B a

re N

OT T

HE S

AME.

&&

AND

A &

& B

is tr

ue if

BOT

H A

and

B ar

e TR

UE.

||OR

A ||

B is

true

if A

or B

or B

OTH

are

TRUE

.

!NO

T!A

is T

RUE

if A

is F

ALSE

, and

FAL

SE if

A is

TRU

E.

Page 42: Guía arduino

�e buttons we used here are similar to the buttons in most videogame controllers.

Circuit 2 Arduino Code:5

Troubleshooting:

Light Not Turning On �e pushbutton is square, and because of this it is easy to put it in the wrong way. Give it a 90 degree twist and see if it starts working.

Light Not Fading A bit of a silly mistake we constantly made, when you switch from simple on o� to fading, remember to move the LED wire from pin 13 to pin 9.

Underwhelmed No worries, these circuits are all super stripped down to make playing with the components easy, but once you throw them together the sky is the limit.

You should see the LED turn on and o� as you press the button. If it isn't working, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 5

What You Should See:

The digital pins can be used as inputs as well as outputs. Before you do either, you need to tell the Arduino which direction you're going.

pinMode(button2Pin, INPUT);

Because we've connected the button to GND, it will read LOW when it's being pressed. Here we're using the "equivalence" operator ("==") to see if the button is being pressed.

if (button1State == LOW)

To read a digital input, you use the digitalRead() function. It will return HIGH if there's 5V present at the pin, or LOW if there's 0V present at the pin.

button1State = digitalRead(button1Pin);

Page 43: Guía arduino

Circuit 2

So you’ve already played with a potentiometer, which varies resistance based on the twisting of a knob. In this circuit, you’ll be using a photo resistor, which changes resistance based on how much light the sensor receives. Since the Arduino can’t directly interpret resistance (rather it reads voltage), we use a voltage divider to use our photo resistor. �is voltage divider will output a high voltage when it is getting a lot of light and a low voltage when it is not.

Photo Resistor

PART

S: Wire

6X

CIRCUIT #6 6

LED

1X

330ΩResistor

1X

Photo Resistor

1X

LED

GND(ground) (-)

photoresistor

+5 Volts

resistor(Orange-Orange-Brown)

(330ohm)

resistor(Brown-Black-Orange)

(10k ohm)

10KΩResistor

1X

Pin 9

Pin AØ

p.40

Page 44: Guía arduino

Circ

uit 6

: Ph

oto

Resi

stor

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 45: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Phot

o Re

sist

or

j1Ju

mpe

r Wire

j6Ju

mpe

r Wire

Jum

per W

ireGN

D

Jum

per W

ire5V5V

+

+-

+-

h20

h21

LED

(5m

m)

330Ω

Res

isto

r (se

nsor

)i2

1+

10KΩ

Res

isto

ri1

i5

Jum

per W

ireA0

j5

+

f5f6

Jum

per W

irePi

n 9

j20

Man

y of

the

sens

ors y

ou'll

use

(pot

entio

met

ers,

phot

ores

istor

s, et

c.) a

re

resis

tors

in d

isgui

se. �

eir r

esist

ance

cha

nges

in p

ropo

rtio

n to

wha

teve

r th

ey're

sens

ing

(ligh

t lev

el, e

tc.).

�e

Ardu

ino'

s ana

log

inpu

t pin

s mea

sure

vol

tage

, not

resis

tanc

e. B

ut w

e ca

n ea

sily

use

resis

tive

sens

ors w

ith th

e Ar

duin

o by

incl

udin

g th

em a

s par

t of a

"v

olta

ge d

ivid

er".

A vo

ltage

div

ider

con

sists

of tw

o re

sisto

rs. �

e "t

op"

resis

tor i

s the

sens

or

you'

ll be

usin

g. �

e "b

otto

m"

one

is a

norm

al, �

xed

resis

tor.

Whe

n yo

u co

nnec

t the

top

resis

tor t

o 5

Vol

ts, a

nd th

e bo

ttom

resis

tor t

o gr

ound

, the

m

iddl

e w

ill o

utpu

t a v

olta

ge p

ropo

rtio

nal t

o th

e va

lues

of t

he tw

o re

sisto

rs.

Whe

n on

e of

the

resis

tors

cha

nges

(as i

t will

whe

n yo

ur se

nsor

sens

es th

ings

), th

e ou

tput

vol

tage

will

cha

nge

as w

ell!

Alth

ough

the

sens

or's

resis

tanc

e w

ill v

ary,

the

resis

tive

sens

ors (

�ex

sens

or,li

ght s

enso

r, so

ftpot

, and

trim

pot)

in th

e SI

K a

re a

roun

d 10

K O

hms.

We

usua

lly w

ant t

he �

xed

resis

tor t

o be

clo

se to

this

valu

e, so

usin

g a

10K

re

sisto

r is a

gre

at c

hoic

e fo

r the

�xe

d "b

otto

m"

resis

tor.

Mea

surin

g re

sist

ive

sens

ors:

Pin

3

5 vo

lts

GN

D(g

roun

d) (

-)

Pin

3

Page 46: Guía arduino

A street lamp uses a light sensor to detect when to turn the lights on at night.

Circuit 2 Arduino Code:6

Troubleshooting:

LED Remains Dark�is is a mistake we continue to make time and time again, if only they could make an LED that worked both ways. Pull it up and give it a twist.

It Isn't Responding to Changes in LightGiven that the spacing of the wires on the photo-resistor is not standard, it is easy to misplace it. Double check it’s in the right place.

Still Not Quite WorkingYou may be in a room which is either too bright or dark. Try turning the lights on or o� to see if this helps. Or if you have a �ashlight near by give that a try.

You should see the LED grow brighter or dimmer in accordance with how much light your photoresistor is reading. If it isn't working, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 6

What You Should See:

When we read an analog signal using analogRead(), it will

be a number from 0 to 1023. But when we want to drive a

PWM pin using analogWrite(), it wants a number from 0 to

255. We can "squeeze" the larger range into the smaller

range using the map() function.

lightLevel = map(lightLevel, 0, 1023, 0, 255);

Because map() could still return numbers outside the "to"

range, we'll also use a function called constrain() that will

"clip" numbers into a range. If the number is outside the

range, it will make it the largest or smallest number. If it is

within the range, it will stay the same.

lightLevel = constrain(lightLevel, 0, 255);

Page 47: Guía arduino

Circuit 2

A temperature sensor is exactly what it sounds like – a sensor used to measure ambient temperature. �is particular sensor has three pins – a positive, a ground, and a signal. For every centigrade degree it reads, it outputs 10 millivolts. In this circuit, you’ll learn how to integrate the temperature sensor with your Arduino, and use the Arduino IDE’s debug window to display the temperature.

Temperature Sensor

CIRCUIT #7 7

p.44

Pin AØ

TMP36(precision

temperaturesensor)

GND(ground) (-)

5 Volts

+5vsignalgnd

PART

S: Wire

5X

Temp. Sensor

1

When you’re building the circuit be careful not to mix up the temperature sensor and the transistor, they’re almost identical.

X

FRONT

BACK

Page 48: Guía arduino

Circ

uit 7

: Tem

pera

ture

Sen

sor

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 49: Guía arduino

Tem

pera

ture

Sen

sor

f5f7

f6

Com

pone

nt:

Imag

e Re

fere

nce:

j5Ju

mpe

r Wire

j7Ju

mpe

r Wire

Jum

per W

ireG

ND

Jum

per W

ire5V5V

+

Jum

per W

ireA0

j6

+

�is

circ

uit u

ses t

he A

rdui

no ID

E's s

eria

l mon

itor

. To

open

this,

�rs

t upl

oad

the

prog

ram

then

clic

k th

e bu

tton

whi

ch lo

oks l

ike

a m

agni

fyin

g gl

ass i

n a

squa

re.

Open

ing

your

ser

ial m

onito

r:

1

2

3

Page 50: Guía arduino

Building climate control systems use a temperature sensor to monitor and maintain their settings.

Circuit 2 Arduino Code:7

Troubleshooting:

Nothing Seems to Happen�is program has no outward indication it is working. To see the results you must open the Arduino IDE's serial monitor (instructions on previous page).

Gibberish is Displayed�is happens because the serial monitor is receiving data at a di�erent speed than expected. To �x this, click the pull-down box that reads "*** baud" and change it to "9600 baud".

Temperature Value is Unchanging Try pinching the sensor with your �ngers to heat it up or pressing a bag of ice against it to cool it down.

You should see be able to read the temperature your temperature sensor is detecting on the serial monitor in the Arduino IDE. If it isn't working, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshoot-ing tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 7

What You Should See:

voltage: 0.73 deg C: 22.75 deg F: 72.96

voltage: 0.73 deg C: 22.75 deg F: 72.96

voltage: 0.73 deg C: 22.75 deg F: 72.96

voltage: 0.73 deg C: 22.75 deg F: 72.96

voltage: 0.73 deg C: 22.75 deg F: 72.96

Before using the serial monitor, you must call Serial.begin() to initialize it. 9600 is the "baud rate", or communications speed. When two devices are communicating with each other, both must be set to the same speed.

Serial.begin(9600);

Serial.print() will print everything on the same line. Serial.println() will move to the next line. By using both of these commands together, you can create easy-to-read printouts of text and data.

Serial.println(degreesF);

The Serial.print() command is very smart. It can print out almost anything you can throw at it, including variables of all types, quoted text (AKA "strings"), etc.

See http://arduino.cc/en/Serial/Print for more info.

Serial.print(degreesC);

Page 51: Guía arduino

Circuit 2

Servos are ideal for embedded electronics applications because they do one thing very well that spinning motors cannot – they can move to a position accurately. By varying the pulse of voltage a servo receives, you can move a servo to a speci�c position. For example, a pulse of 1.5 milliseconds will move the servo 90 degrees. In this circuit, you’ll learn how to use PWM (pulse width modulation) to control and rotate a servo.

A Single Servo

PART

S: Wire

8X

CIRCUIT #8 8

p.48

Servo

1X

Mini Servo

+5 volts(5V)

GND(ground) (-)

gnd(black)

signal(white)

+5v(red)

Pin 9

Page 52: Guía arduino

Circ

uit 8

: A S

ingl

e Se

rvo

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 53: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Serv

oe5

e7e6

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

e6 e7e5

5V

Jum

per W

irePi

n 9

a7

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ireG

ND

b5

+

a6+

Ardu

ino

give

s you

a ve

ry u

sefu

l set

of b

uilt-

in co

mm

ands

for d

oing

bas

ic in

put a

nd o

utpu

t, m

akin

g de

cisio

ns u

sing

logi

c, so

lvin

g m

ath

prob

lems,

etc.

But t

he re

al po

wer

of A

rdui

no is

th

e hug

e com

mun

ity u

sing

it, an

d th

eir w

illin

gnes

s to

shar

e the

ir w

ork.

Libr

aries

are c

ollec

tions

of n

ew co

mm

ands

that

hav

e bee

n pa

ckag

ed to

geth

er to

mak

e it e

asy

to in

clude

them

in y

our s

ketc

hes.

Ardu

ino

com

es w

ith a

hand

ful o

f use

ful l

ibra

ries,

such

as

the s

ervo

libr

ary

used

in th

is ex

ampl

e, th

at ca

n be

use

d to

inte

rface

to m

ore a

dvan

ced

devi

ces (

LCD

disp

lays,

stepp

er m

otor

s, et

hern

et p

orts,

etc.)

See h

ttp:

//ar

duin

o.cc

/en/

Ref

eren

ce/L

ibra

ries

for a

list

of th

e sta

ndar

d lib

rarie

s and

in

form

atio

n on

usin

g th

em.

But a

nyon

e can

crea

te a

libra

ry, a

nd if

you

wan

t to

use a

new

sens

or o

r out

put d

evice

, ch

ance

s are

that

som

eone

out

ther

e has

alre

ady

writ

ten

one t

hat i

nter

face

s tha

t dev

ice to

the

Ardu

ino.

Man

y of

Spa

rkFu

n's p

rodu

cts c

ome w

ith A

rdui

no li

brar

ies, a

nd y

ou ca

n �n

d ev

en

mor

e usin

g G

oogl

e and

the A

rdui

no P

laygr

ound

at h

ttp:

//ar

duin

o.cc

/pla

ygro

und/

. And

w

hen

YOU

get

the A

rdui

no w

orki

ng w

ith a

new

dev

ice, c

onsid

er m

akin

g a l

ibra

ry fo

r it a

nd

shar

ing

it w

ith th

e wor

ld!

To

use a

libr

ary

in a

sket

ch, s

elect

it fr

om S

ketc

h >

Impo

rt L

ibra

ry.

Expa

nd y

our h

oriz

ons

usin

g Li

brar

ies:

File

Edit

Sket

chTo

ols

Hel

p

EEP

RO

MEt

her

net

Firm

ata

Liq

uid

Cry

stal

SD Serv

oSo

ftw

areS

eria

lSP

ISt

epp

erW

ire

Ver

ify

/ C

om

pile

Sho

w S

ketc

h F

old

erA

dd

File

...Im

po

rt L

ibra

ry

Page 54: Guía arduino

Circuit 2 Arduino Code:8

Troubleshooting:

Servo Not Twisting Even with colored wires it is still shockingly easy to plug a servo in backward. �is might be the case.

Still Not WorkingA mistake we made a time or two was simply forgetting to connect the power (red and brown wires) to +5 volts and ground.

Fits and StartsIf the servo begins moving then twitches, and there's a �ashing light on your Arduino board, the power supply you are using is not quite up to the challenge. Using a wall adapter instead of USB should solve this problem.

You should see your servo motor move to various locations at several speeds. If the motor doesn't move, check your connections and make sure you have veri�ed and uploaded the code, or see the troubleshooting tips below.

Code to Note:

Real World Application:

Robotic arms you might see in an assembly line or sci-� movie probably have servos in them.

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 8

What You Should See:

#include is a special "preprocessor" command that inserts a library (or any other file) into your sketch. You can type this command yourself, or choose an installed library from the "sketch / import library" menu.

#include <Servo.h>

Servos don't spin all the way around, but they can be commanded to move to

a specific position. We use the servo library's write() command to move a

servo to a specified number of degrees(0 to 180). Remember that the servo

requires time to move, so give it a short delay() if necessary.

servo1.write(180);

The servo library adds new commands that let you control a servo. To prepare the Arduino to control a servo, you must first create a Servo "object" for each servo (here we've named it "servo1"), and then "attach" it to a digital pin (here we're using pin 9).

Servo servo1;

servo1.attach(9);

Page 55: Guía arduino

PART

S: IC

1X

330ΩResistor

8X

LED

8X

In this circuit, we will use a �ex sensor to measure, well, �ex! A �ex sensor uses carbon on a strip of plastic to act like a variable resistor, but instead of changing the resistance by turning a knob, you change it by �exing (bending) the component. We use a "voltage divider" again to detect this change in resistance. �e sensor bends in one direction and the more it bends, the higher the resistance gets; it has a range from about 10K ohm to 35K ohm. In this circuit we will use the amount of bend of the �ex sensor to control the position of a servo.

Flex Sensor

PART

S:CIRCUIT #9 9

Potentiometer

1X

Servo

1X

Flex Sensor

1X

10KΩResistor

1X

Wire

11X

+5 volts(5V)

+5 volts(5V)

GND(ground) (-)

Mini Servo

resistor

flexsensor

(10K ohm)(Brown-Black-Orange)

p.10p.52

Pin AØ

Pin 9

Page 56: Guía arduino

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Circ

uit 9

: Fle

x Se

nsor

Page 57: Guía arduino

It ha

ppen

s to

ever

yone

- yo

u w

rite a

sket

ch w

hich

succ

essfu

lly co

mpi

les an

d up

load

s, bu

t you

can'

t �gu

re o

ut w

hy it

's no

t doi

ng w

hat y

ou w

ant i

t to.

Lar

ger c

ompu

ters

have

scre

ens,

keyb

oard

s, an

d m

ice th

at y

ou ca

n us

e to

debu

g yo

ur co

de, b

ut ti

ny

com

pute

rs lik

e the

Ard

uino

hav

e no

such

thin

gs.

�e k

ey to

visi

bilit

y in

to a

micr

ocon

trolle

r is o

utpu

t. �

is ca

n be

alm

ost a

nyth

ing,

in

cludi

ng L

EDs a

nd b

uzze

rs, b

ut o

ne o

f the

mos

t use

ful t

ools

is th

e ser

ial m

onito

r. U

sing

Seri

al.p

rint

() an

d pr

intln

(), y

ou ca

n ea

sily

outp

ut h

uman

-read

able

text

and

data

from

the A

rdui

no to

a w

indo

w b

ack

on th

e hos

t com

pute

r. �

is is

grea

t for

you

r sk

etch

's �n

al ou

tput

, but

it's

also

incr

edib

ly u

sefu

l for

deb

uggi

ng.

Debu

ggin

g yo

ur s

ketc

hes

usin

g th

e Se

rial M

onito

r:Co

mpo

nent

:Im

age

Refe

renc

e:

i20

i24

Serv

oe1

e3e2

e1Ju

mpe

r Wire

i19

Jum

per W

ire

h24

+Ju

mpe

r Wire

h24

+Ju

mpe

r Wire

h24

+Ju

mpe

r Wire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire

e2Ju

mpe

r Wire

e3Ju

mpe

r Wire

Flex

Sen

sor

h19

h20

10KΩ

Res

isto

r

j20

A0

b1+

a2+

a3Pi

n 9

5V5V+

GN

D

And

if yo

u ru

n th

e cod

e aga

in, y

ou'll

see t

he

outp

ut y

ou w

ante

d:fo

r (x

= 1

; x

< 9

; x+

+){

Se

rial

.pri

nt(x

);}

1234

5678

You

want

ed 1

to 8

, but

the l

oop

is ac

tuall

y givi

ng yo

u 0

to 7

. Who

ops!

Now

you

just

need

to �

x the

loop

.

0123

4567

Let's

say

you

wan

ted

a for

() lo

op fr

om 1

to 8

, bu

t you

r cod

e jus

t doe

sn't

seem

to b

e wor

king

rig

ht. J

ust a

dd S

eria

l.beg

in(9

600)

; to

your

se

tup(

) fun

ctio

n, an

d ad

d a S

eria

l.pri

nt()

or

prin

tln()

to y

our l

oop:

for

(x =

0; x

< 8

; x++

){

Se

rial

.pri

nt(x

);}

Page 58: Guía arduino

Controller accessories for videogame consoles like Nintendo’s “Power Glove” use �ex-sensing technology. It was the �rst video game controller attempting to mimic hand movement on a screen in real time.

Circuit 2 Arduino Code:9

Troubleshooting:

Servo Not TwistingEven with colored wires it is still shockingly easy to plug a servo in backwards. �is might be the case.

Servo Not Moving as Expected�e sensor is only designed to work in one direction. Try �exing it the other way (where the striped side faces out on a convex curve).

Servo Doesn’t Move very FarYou need to modify the range of values in the call to the map() function.

You should see the servo motor move in accordance with how much you are �exing the �ex sensor. If it isn't working, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 9

What You Should See:

Because the flex sensor / resistor combination won't give us a full zero to five-volt range, we're using the map() function as a handy way to reduce that range. Here we've told it to only expect values from 600 to 900, rather than 0 to 1023.

servoposition = map(flexposition, 600, 900, 0, 180);

Because map() could still return numbers outside the "to"

range, we'll also use a function called constrain() that will

"clip" numbers into a range. If the number is outside the

range, it will make it the largest or smallest number. If it is

within the range, it will stay the same.

Serial.print("sensor: ");

Serial.print(flexposition);

Serial.print(" servo: ");

Serial.println(servoposition);

Page 59: Guía arduino

Circuit 2PA

RTS:

CIRCUIT #5

p.10

IC

1X

330ΩResistor

8X

LED

8X

Circuit 2

In this circuit, we are going to use yet another kind of variable resistor – this time, a soft potentiometer (or soft pot). �is is a thin and �exible strip that can detect where pressure is being applied. By pressing down on various parts of the strip, you can vary the resistance from 100 to 10K ohms. You can use this ability to track movement on the soft pot, or simply as a button. In this circuit, we’ll get the soft pot up and running to control an RGB LED.

Soft Potentiometer

PART

S:CIRCUIT #10 10

p.56

Wire

9X

330ΩResistor

3X

LED

1X

Soft Potentiometer

1X

330ΩResistor

3X

10KΩResistor

1X

Pin 0P

in 9

red

Pin

10

green

Pin

11

blue

+5 volts

V+

gnd

wiper

soft

po

t

resistor(Orange-Orange-Brown)

(330ohm)

Brown-Black-Orange)resistor (10K ohm)

GND(ground) (-)

Pin AØP

in 9

Pin

10

Pin

11

Page 60: Guía arduino

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Circ

uit 1

0: S

oft P

oten

tiom

eter

Page 61: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Com

pone

nt:

Imag

e Re

fere

nce:

Jum

per W

ireG

ND

RGB

LED

(5m

m)

a4a5

a6a7

e7e1

15V

330Ω

Res

isto

rg4

e4 e7e1

15V

330Ω

Res

isto

rg6

e6 e7e1

15V

330Ω

Res

isto

rg7

e7

Jum

per W

ire5V5V

+

Jum

per W

irePi

n 9

h4

Jum

per W

irePi

n 10

h6

Jum

per W

irePi

n 11

h7

e5Ju

mpe

r Wire

j18

Jum

per W

ire

j20

Jum

per W

ire

Jum

per W

irej1

9A0

+

5VSo

ft Po

tent

iom

eter

h19

h18

h20

10KΩ

Res

isto

ri1

9

Page 62: Guía arduino

�e knobs found on many objects, like a radio for instance, are using similar concepts to the one you just completed for this circuit.

Circuit 2 Arduino Code:10

Troubleshooting:

LED Remains Dark or Shows Incorrect ColorWith the four pins of the LED so close together, it’s sometimes easy to misplace one. Try double checking each pin is where it should be.

Bizarre Results�e most likely cause of this is if you’re pressing the potentiometer in more than one position. �is is normal and can actually be used to create some neat results.

You should see the RGB LED change colors in accordance with how you interact with the soft potentiometer. If it isn't working, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board, or see the troubleshoot-ing tips below.

These big, scary functions take a

single Value (RGBposition) and

calculate the three RGB values

necessary to create a rainbow of

color. The functions create three

"peaks" for the red, green, and blue

values, which overlap to mix and

create new colors. Even if you're

not 100% clear how it works, you

can copy and paste this (or any)

function into your own code and

use it yourself.

redValue = constrain(map(RGBposition, 0, 341, 255, 0), 0, 255)

+ constrain(map(RGBposition, 682, 1023, 0, 255), 0, 255);

greenValue = constrain(map(RGBposition, 0, 341, 0, 255), 0, 255)

- constrain(map(RGBposition, 341, 682, 0,255), 0, 255);

blueValue = constrain(map(RGBposition, 341, 682, 0, 255), 0, 255)

- constrain(map(RGBposition, 682, 1023, 0, 255), 0, 255);

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 10

What You Should See:

Page 63: Guía arduino

Circuit 2

In this circuit, we'll again bridge the gap between the digital world and the analog world. We'll be using a buzzer that makes a small "click" when you apply voltage to it (try it!). By itself that isn't terribly exciting, but if you turn the voltage on and o� hundreds of times a second, the buzzer will produce a tone. And if you string a bunch of tones together, you've got music! �is circuit and sketch will play a classic tune. We'll never let you down!

Buzzer

PART

S: Wire

4X

CIRCUIT #11 11

p.60

Piezo Element

1X

Piezo Element

GND(ground) (-)

Pin 9

If the buzzer doesn't easily fit into the holes on the breadboard, try rotating it slightly.

Page 64: Guía arduino

Circ

uit 1

1: P

iezo

Ele

men

ts

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 65: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Piez

o El

emen

t+

-j9

j7

Jum

per W

irePi

n 9

j9

i7

Jum

per W

ireG

ND

Jum

per W

ire

Jum

per W

ire5V5V

+

Ardu

ino

cont

ains

a w

ealth

of b

uilt-

in fu

nctio

ns th

at ar

e use

ful f

or al

l kin

ds o

f thi

ngs.

(See

htt

p://

ardu

ino.

cc/e

n/R

efer

ence

for a

list)

. But

you

can

also

easil

y cr

eate

you

r ow

n fu

nctio

ns. H

ere's

a sim

ple e

xam

ple n

amed

"add

", w

hich

adds

two

num

bers

toge

ther

and

retu

rns t

he re

sult.

Let

's br

eak

it do

wn.

Your

func

tions

can

take

in v

alues

("pa

ram

eter

s"),

and

retu

rn a

valu

e, as

this

one d

oes.

But y

ou ca

n als

o do

eith

er o

r non

e of t

hose

thin

gs, i

f you

wish

.

If yo

u'll

be p

assin

g pa

ram

eter

s /to

/ you

r fun

ctio

n, p

ut th

em (a

nd th

eir ty

pes)

in th

e pa

rent

hese

s afte

r the

func

tion

nam

e. If

you

won

't be

giv

ing

your

func

tion

any

para

met

ers,

just

use a

n em

pty

pare

nthe

sis ()

afte

r the

nam

e.

If yo

u'll

be re

turn

ing

a valu

e /fr

om/ y

our f

unct

ion,

put

the t

ype o

f the

retu

rn v

alue i

n fro

nt o

f the

func

tion

nam

e. �

en in

you

r fun

ctio

n, w

hen

you'

re re

ady

to re

turn

the

valu

e, pu

t in

a ret

urn(

) sta

tem

ent.

If yo

u w

on't

be re

turn

ing

a valu

e, pu

t "vo

id" i

n fro

nt o

f the

func

tion

nam

e (ju

st lik

e you

've a

lread

y se

en fo

r the

setu

p() a

nd lo

op()

fu

nctio

ns).

Whe

n yo

u w

rite y

our o

wn

func

tions

, you

mak

e you

r cod

e nea

ter a

nd ea

sier t

o re

-use

.

Crea

ting

your

ow

n fu

nctio

ns:

int a

dd(i

nt p

aram

eter

1, in

t par

amet

er2)

{ in

t x;

x

= pa

ram

eter

1 +

para

met

er2;

re

turn

(x);

}

Page 66: Guía arduino

Many modern megaphones have settings that use a loud ampli�ed buzzer. �ey are usually very loud and quite good at getting people’s attention.

Circuit 2 Arduino Code:11

Troubleshooting:

No SoundGiven the size and shape of the piezo element it is easy to miss the right holes on the breadboard. Try double checking its placement.

Can't �ink While the Melody is PlayingJust pull up the piezo element whilst you think, upload your program then plug it back in.

Tired of Twinkle Twinkle Little Star�e code is written so you can easily add your own songs.

You should see - well, nothing! But you should be able to hear you piezo element playing "Twinkle, Twinkle Little Star" (or possibly, "�e ABCs"). If it isn't working, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board or see the troubleshooting tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 11

What You Should See:

Up until now we've been working solely with numerical

data, but the Arduino can also work with text. Characters

(single, printable, letters, numbers and other symbols) have

their own type, called "char". When you have an array of

characters, it can be defined between double-quotes (also

called a "string"), OR as a list of single-quoted characters.

char notes[] = "cdfda ag cdfdg gf ";

char names[] = {'c','d','e','f','g','a','b','C'};

One of Arduino's many useful built-in commands is the

tone() function. This function drives an output pin at a

certain frequency, making it perfect for driving buzzers and

speakers. If you give it a duration (in milliseconds), it will

play the tone then stop. If you don't give it a duration, it

will keep playing the tone forever (but you can stop it with

another function, noTone() ).

tone(pin, frequency, duration);

Page 67: Guía arduino

Circuit 2

Remember before when you played around with a servo motor? Now we are going to tackle spinning a motor. �is requires the use of a transistor, which can switch a larger amount of current than the Arduino can. When using a transistor, you just need to make sure its maximum specs are high enough for your use. �e transistor we are using for this circuit is rated at 40V max and 200 milliamps max – perfect for our toy motor!

Spinning a Motor

PART

S: Wire

6X

CIRCUIT #12 12

1X

DC Motor

1X

Diode1N4148

p.64

TransistorP2N2222AG

1X

When you’re building the circuit be careful not to mix up the transistor and the temperature sensor, they’re almost identical.

330ΩResistor

1X

P2N2

222A

A18

P2N2

222A

A18

FRONT

BACK

GND(ground) (-)

basetransistor P2N2222AG

collector

mo

tor

mul

tim

eter

diode

emitter

+5 volts(5V)

resistor(Orange-Orange-Brown)

(330ohm)

Pin 9

Page 68: Guía arduino

Circ

uit 1

2 : S

pinn

ing

a M

otor

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 69: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Tran

sist

or P

2N22

22AG

a1a3

a2

Jum

per W

ire

Jum

per W

irePi

n 9

j2

Jum

per W

ire

Jum

per W

ire

Jum

per W

ire5V

Jum

per W

ireG

ND

Diod

e 1N

4148

a3G

ND

b7b1

1

e7e1

1

e7e1

1

DC M

otor

5V33

0Ω R

esis

tor

g2e2 e1 a7

+

+

e3d1

1

At th

is po

int y

ou're

pro

babl

y sta

rting

to g

et y

our o

wn

idea

s for

circ

uits

that

do

fun

thin

gs, o

r help

solv

e a re

al pr

oblem

. Exc

ellen

t! H

ere a

re so

me t

ips o

n pr

ogra

mm

ing

in

gene

ral.

Mos

t of t

he sk

etch

es y

ou w

rite w

ill b

e a lo

op w

ith so

me o

r all

of th

ese s

teps

:

1. P

erfo

rm so

me

sort

of i

nput

2. M

ake

som

e ca

lcul

atio

ns o

r dec

isio

ns3.

Per

form

som

e so

rt o

f out

put

4. R

epea

t! (O

r not

!)

We'v

e alre

ady

show

n yo

u ho

w to

use

a bu

nch

of d

i�er

ent i

nput

sens

ors a

nd o

utpu

t de

vice

s (an

d w

e stil

l hav

e a fe

w m

ore t

o go

). Fe

el fre

e to

mak

e use

of t

he ex

ampl

es in

yo

ur o

wn

sket

ches

- th

is is

the w

hole

idea

beh

ind

the "

Ope

n So

urce

" mov

emen

t.

It's u

suall

y pr

etty

easy

to p

ull p

ieces

of d

i�er

ent s

ketc

hes t

oget

her,

just

open

them

in

two

win

dow

s, an

d co

py an

d pa

ste b

etw

een

them

. �is

is on

e of t

he re

ason

s we'v

e be

en p

rom

otin

g "g

ood

prog

ram

min

g ha

bits"

. �in

gs li

ke u

sing

cons

tant

s for

pin

nu

mbe

rs, an

d br

eaki

ng y

our s

ketc

h in

to fu

nctio

ns, m

ake i

t muc

h ea

sier t

o re

-use

you

r co

de in

new

sket

ches

. For

exam

ple,

if yo

u pu

ll in

two

piec

es o

f cod

e tha

t use

the s

ame

pin,

you

can

easil

y ch

ange

one

of t

he co

nsta

nts t

o a n

ew p

in. (

Don

't fo

rget

that

not

all

of t

he p

ins s

uppo

rt an

alog

Wri

te()

; the

com

patib

le pi

ns ar

e mar

ked

on y

our b

oard

.)

If yo

u ne

ed h

elp, t

here

are i

nter

net f

orum

s whe

re y

ou ca

n as

k qu

estio

ns. T

ry

Ardu

ino'

s for

um at

ard

uino

.cc/

foru

m, a

nd S

park

Fun'

s at f

orum

.spar

kfun

.com

. W

hen

you'

re re

ady

to m

ove t

o m

ore a

dvan

ced

topi

cs, t

ake a

look

at A

rdui

no's

tuto

rials

page

at a

rdui

no.c

c/en

/Tut

oria

l. M

any

of S

park

Fun'

s mor

e adv

ance

d pr

oduc

ts w

ere p

rogr

amm

ed w

ith A

rdui

no, (

allow

ing

you

to ea

sily

mod

ify th

em),

or

have

Ard

uino

exam

ples

for t

hem

. See

our

pro

duct

pag

es fo

r inf

o.

Fina

lly, w

hen

you

crea

te so

met

hing

reall

y co

ol, c

onsid

er sh

arin

g it

with

the w

orld

so

that

oth

ers c

an le

arn

from

you

r gen

ius.

(And

be s

ure t

o let

us k

now

so w

e can

put

it

on o

ur h

ome p

age!)

Putti

ng it

all

toge

ther

:

P2N2222AA18

Page 70: Guía arduino

Circuit 2 Arduino Code:12

Troubleshooting:

Motor Not SpinningIf you sourced your own transistor, double check with the data sheet that the pinout is compatible with a P2N2222AG (many are reversed).

Still No LuckIf you sourced your own motor, double check that it will work with 5 volts and that it does not draw too much power.

Still Not WorkingSometimes the Arduino board will disconnect from the computer. Try un-plugging and then re-plugging it into your USB port.

�e DC Motor should spin if you have assembled the circuit’s components correctly, and also veri�ed/uploaded the correct code. If your circuit is not working check the troubleshooting section below.

Code to Note:

Real World Application:

Radio Controlled(RC) cars use Direct Current(DC) motors to turn the wheels for propulsion.

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 12

What You Should See:

The Arduino's serial port can be used to receive as well as

send data. Because data could arrive at any time, the Arduino

stores, or "buffers" data coming into the port until you're

ready to use it. The Serial.available() command returns the

number of characters that the port has received, but haven't

been used by your sketch yet. Zero means no data has arrived.

while (Serial.available() > 0)

If the port has data waiting for you, there are a number of

ways for you to use it. Since we're typing numbers into the

port, we can use the handy Serial.parseInt() command to

extract, or "parse" integer numbers from the characters it's

received. If you type "1" "0" "0" to the port, this function

will return the number 100.

speed = Serial.parseInt();

Page 71: Guía arduino

Circuit 2PA

RTS:

CIRCUIT #5 5

p.10

IC

1X

LED

8X

Circuit 2

In this circuit, we are going to use some of the lessons we learned in circuit 12 to control a relay. A relay is basically an electrically controlled mechanical switch. Inside that harmless looking plastic box is an electromagnet that, when it gets a jolt of energy, causes a switch to trip. In this circuit, you’ll learn how to control a relay like a pro – giving your Arduino even more powerful abilities!

Relays

PART

S:CIRCUIT #13 13

p.68

TransistorP2N2222AG

1X

Relay

1X 1X

Diode1N4148

Wire

14X

330ΩResistor

2X

LED

2X

GND(ground) (-)

basetransistor P2N2222AG

collector

diode(flyback)

coil

NC

NO

com

emitter

5 volts

5 volts

resistor(Orange-Orange-Brown)

(330ohm)

resistor(Orange-Orange-Brown)

(330ohm)

LED LED When the relay is o�, the COM (common) pin will be connected to the NC (Normally Closed) pin. When the relay is on, the COM (common) pin will be connected to the NO (Normally Open) pin.

Pin 2

Page 72: Guía arduino

Circ

uit 1

3: R

elay

s

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 73: Guía arduino

Rela

ye9

f9

e15

f15

Com

pone

nt:

Imag

e Re

fere

nce:

e2Ju

mpe

r Wire

h9Ju

mpe

r Wire

+

f5f7

f6

Diod

e 1N

4148

a3G

ND

b7b1

1

Tran

sist

or P

2N22

22AG

a2a4

a3

e7e1

15V

330Ω

Res

isto

rg3

e3 e7e1

15V

330Ω

Res

isto

rg2

e2

Com

pone

nt:

Imag

e Re

fere

nce:

LED

(5m

m)

+-

+-

c19

c20

LED

(5m

m)

+-

+-

c22

c23

i13

Jum

per W

irei1

3e2

2

j5Ju

mpe

r Wire

j7j9

Jum

per W

irei1

5e1

9

Jum

per W

iree1

5e1

9

Jum

per W

iree1

5e1

9

Jum

per W

ireb1

4e1

9

Jum

per W

irea7

a9

Jum

per W

iree4

e9

++ +

Jum

per W

iree1

5e1

9

Jum

per W

iree1

5e1

9

Jum

per W

irea2

3+

Jum

per W

ireG

ND

Jum

per W

ire5V5V

+

Jum

per W

irePi

n 2

j3

Jum

per W

irea2

0

P2N2222AA18

Page 74: Guía arduino

Garage door openers use relays to operate. You might be able to hear the clicking if you listen closely.

Circuit 2 Arduino Code:13

Troubleshooting:

LEDs Not LightingDouble-check that you've plugged them in correctly. �e longer lead (and non-�at edge of the plastic �ange) is the positive lead.

No Clicking Sound�e transistor or coil portion of the circuit isn't quite working. Check the transistor is plugged in the right way.

Not Quite Working�e included relays are designed to be soldered rather than used in a breadboard. As such you may need to press it in to ensure it works (and it may pop out occasionally).

You should be able to hear the relay contacts click, and see the two LEDs alternate illuminating at 1-second intervals. If you don't, double-check that you have assembled the circuit correctly, and uploaded the correct sketch to the board. Also, see the troubleshooting tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 13

What You Should See:

When we turn on the transistor, which in turn energizes

the relay's coil, the relay's switch contacts are closed. This

connects the relay's COM pin to the NO (Normally Open)

pin. Whatever you've connected using these pins will turn

on. (Here we're using LEDs, but this could be almost anything.)

digitalWrite(relayPin, HIGH);

The relay has an additional contact called NC (Normally

Closed). The NC pin is connected to the COM pin when

the relay is OFF. You can use either pin depending on

whether something should be normally on or normally off.

You can also use both pins to alternate power to two

devices, much like railroad crossing warning lights.

digitalWrite(relayPin, LOW);

Page 75: Guía arduino

Circuit 2

Shift Register

PART

S: Wire

19X

CIRCUIT #14 14

p.72

Now we are going to step into the world of ICs (integrated circuits). In this circuit, you’ll learn all about using a shift register (also called a serial-to-parallel controller). �e shift register will give your Arduino an additional eight outputs, using only three pins on your board. For this circuit, you’ll practice by using the shift register to control eight LEDs.

IC

1X

330ΩResistor

8X

LED

8X

GND(ground) (-)

+5 volts

+5 volts

15 1016

1

2

3

4

5

6

78

11

12

14

13

data

clock

latch

resistors(Orange-Orange-Brown)

(330ohm) LEDs

4

Pin 3

Pin 4

Pin 2

Bend legs to 90° angle.

Align notch on top, inbetween “e5” and “f5” on

the breadboard.

1

2

3

4

5

6

7

8

QB

QC

QD

QE

QF

QG

QH

GND

VCC

QA

SER

OE

RCLK

SRCLK

SRCLR

QH’

16

15

14

13

12

11

10

9

Page 76: Guía arduino

Circ

uit 1

4: S

hift

Regi

ster

ab

cd

ef

gh

i1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

ab

cd

ef

gh

i

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Page 77: Guía arduino

Com

pone

nt:

Imag

e Re

fere

nce:

Com

pone

nt:

Imag

e Re

fere

nce:

LED

(5m

m)

+-

+-

c14

c15

LED

(5m

m)

+-

+-

c17

c18

LED

(5m

m)

+-

+-

c20

c21

LED

(5m

m)

+-

+-

c23

c24

LED

(5m

m)

+-

+-

h14

h15

LED

(5m

m)

+-

+-

h17

h18

LED

(5m

m)

+-

+-

h20

h21

LED

(5m

m)

+-

+-

h23

h24 a3a15

330Ω

Res

isto

r

a3G

ND

c23

a18

330Ω

Res

isto

r

a3G

ND

c23

a21

330Ω

Res

isto

r

a3G

ND

c23

a24

330Ω

Res

isto

r

a3G

ND

j15

c24

330Ω

Res

isto

r

a3G

ND

j18

c24

330Ω

Res

isto

r

a3G

ND

j21

c24

330Ω

Res

isto

r

a3G

ND

j24

c24

330Ω

Res

isto

r

ICe5 f5

e6 f6

e7 f7

e8 f8

e9 f9

e10

f10

e11

f11

e12

f12 a3G

ND

Jum

per W

ire+

+

a3G

ND

Jum

per W

ire+

+ a3G

ND

Jum

per W

irej5

+ a3G

ND

Jum

per W

irej6

a14

Jum

per W

ire

a3G

ND

Jum

per W

irej8

a14

Jum

per W

ire

Jum

per W

ire

a3G

ND

Jum

per W

iref1

4a8 a3

GN

DJu

mpe

r Wire

f17

a9 a3G

ND

Jum

per W

iref2

0a1

0 a3G

ND

Jum

per W

iref2

3a1

1 a3G

ND

Jum

per W

irea2

3a7 a3

GN

DJu

mpe

r Wire

a20

a6 a3G

ND

Jum

per W

irea1

7a5 a3

GN

DJu

mpe

r Wire

a14

j6

Jum

per W

ire

Jum

per W

ire

j7Pi

n 2

j9Pi

n 4

j10

Pin

3

a3G

ND

Jum

per W

irej1

1a1

4+

j10

5V+

GN

D

Page 78: Guía arduino

Similar to circuit #4, a scrolling marquee display delivers a mesage with multiple LEDs. Essentially the same task the shift register achieves here in Circuit #14.

Circuit 2 Arduino Code:14

Troubleshooting:

�e Arduino's power LED goes out �is happened to us a couple of times, it happens when the chip is inserted backward. If you �x it quickly nothing will break.

Not Quite WorkingSorry to sound like a broken record but it is probably something as simple as a crossed wire.

FrustrationShoot us an e-mail, this circuit is both simple and complex at the same time. We want to hear about problems you have so we can address them in future editions: [email protected]

You should see the LEDs light up similarly to in circuit 4 (but this time, you're using a shift register). If they aren't, make sure you have assembled the circuit correctly and veri�ed and uploaded the code to your board, or see the troubleshooting tips below.

Code to Note:

Real World Application:

Open Arduino IDE // File > Examples > SIK Guide > Circuit # 14

What You Should See:

You'll communicate with the shift register (and a lot of other parts)

using an interface called SPI, or Serial Peripheral Interface. This

interface uses a data line and a separate clock line that work together

to move data in or out of the Arduino at high speed. The MSBFIRST

parameter specifies the order in which to send the individual bits, in

this case we're sending the Most Significant Bit first.

shiftOut(datapin, clockpin, MSBFIRST, data);

Bits are the smallest possible piece of memory in a computer; each

one can store either a "1" or a "0". Larger numbers are stored as arrays

of bits. Sometimes we want to manipulate these bits directly, for

example now when we're sending eight bits to the shift register and

we want to make them 1 or 0 to turn the LEDs on or off. The Arduino

has several commands, such as bitWrite(), that make this easy to do.

bitWrite(data,desiredPin,desiredState);

Page 79: Guía arduino

This is just the beginning of your exploration into embedded electronics and coding. Our website has a wealth of tutorials to whet your appetite for more knowledge. We also host a community of hackers, engineers, DIYers, etc. in our forums. So log on to our website for more information about Arduino, or to plan ahead for your next project!

sparkfun.com

Visit us Online:

Learning More

NOTES:

Page 80: Guía arduino

This work is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported License.

To view a copy of this license visit: http://creativecommons.org/licenses/by-sa/3.0/

Or send a letter to:Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.