SlideShare a Scribd company logo
arduino
ABOUT OPEN SOURCE HARDWARE 
All of the projects at SparkFun and .:oomlout:. are open source. What does this mean? It means 
everything involved in making this kit, be it this guide, 3D models, or code is available for free 
download. But it goes further, you're also free to reproduce and modify any of this material, then 
distribute it for yourself. The catch? Quite simple, it is released under a Creative Commons (By - 
Share Alike) license. This means you must credit .:oomlout:. in your design and share your 
developments in a similar manner. Why? We grew up learning and playing with open source 
software and the experience was good fun, we think it would be lovely if a similar experience was 
possible with physical things. 
More details on the Creative Commons CC (By - Share Alike) License can be found at 
http://ardx.org/CCLI 
We’re a plucky little design company focusing on producing 
“delightfully fun open source products” 
To check out what we are up to 
http://www.oomlout.com 
A Few Words 
ABOUT THIS KIT 
The overall goal of this kit is fun. Beyond this, the aim is to get 
you comfortable using a wide range of electronic components 
through small, simple and easy circuits. The focus is to get each 
circuit working then giving you the tools to figure out why. If you 
encounter any problems, want to ask a question, or would like to 
know more about any part, extra help is only an e-mail away help@oomlout.com. 
ABOUT .: OOMLOUT :. 
ABOUT SPARKFUN 
SparkFun is an energetic young company seeking to make electronics fun, accessible, 
and approachable to everyone - from kids in elementary school to PhD-toting engineers. 
http://www.sparkfun.com/ 
ABOUT PROBLEMS 
We strive to deliver the highest level of quality in each and every thing we produce. If you ever find an 
ambiguous instruction, a missing piece, or would just like to ask a question, we’ll try our best to help out. 
help@oomlout.com 
(we like hearing about problems it helps us improve future versions) 
Thanks For Choosing .:oomlout:. 
and SparkFun
TBCN 
.: WHERE TO FIND EVERYTHING :. table of contents 
Before We Start 
{ASEM} Assembling the Pieces 02 
{INST} Installing the Software 03 
{PROG} A Small Programming Primer 04 
{ELEC} A Small Electronics Primer 06 
The Circuits 
{CIRC01} Getting Started - (Blinking LED) 08 
{CIRC02} 8 LED Fun - (Multiple LEDs) 10 
{CIRC03} Spin Motor Spin - (Transistor and Motor) 12 
{CIRC04} A Single Servo - (Servos) 14 
{CIRC05} 8 More LEDs - (74HC595 Shift Register) 16 
{CIRC06} Music - (Piezo Elements) 18 
{CIRC07} Button Pressing - (Pushbuttons) 20 
{CIRC08} Twisting - (Potentiometers) 22 
{CIRC09} Light - (Photo Resistors) 24 
{CIRC10} Temperature - (TMP36 Temperature Sensor) 26 
{CIRC11} Larger Loads - (Relays) 28 
{CIRC12} Colorful Light - (RGB LED) 30 
{CIRC13} Measuring Bends - (Flex Sensor) 32 
{CIRC14} Fancy Sensing - (Soft Potentiometer) 34 
01
01 ASEM 
assembling the 
02 
pieces 
Breadboard 
x1 
Arduino 
x1 
Arduino Holder 
x1 
.: PUTTING IT TOGETHER :. 
.: For an introduction to what an Arduino is, visit :. 
.: http://ardx.org/INTR :.
.: INSTALLING THE IDE :. 
This is the program used to write code for the Arduino. It may 
seem a little daunting at first but once you have it installed and 
start playing around, its secrets will reveal themselves. 
Step 1: Download the software 
http://arduino.cc/en/Main/Software 
download the software for your operating system 
Windows XP Mac OSX 
Step 2: Unzip the Software 
Unzip 
rr(rr- version #) 
arduino-00 -win.zip 
Recommended Path 
c:Program Files 
Step 3: Shortcut Icon 
Open 
rr (rr- version #) 
c:program filesarduino-00 
Right Click 
Arduino.exe (send to>Desktop (create shortcut)) 
Step 4: Plug In Your Arduino 
Plug your Arduino in: 
Using the included USB cable, plug your Arduino 
board into a free USB port. 
Wait for a box to pop up 
Step 5: Add new Hardware 
Skip searching the internet 
(click the next box when prompted to do so) 
Install from a specific location 
(click “Install from a list or specific location (Advanced)") 
Choose the Location 
Duemilanove Board 
c:program filesarduino-00 
rrdriversFTDI USB Drivers 
Uno Board 
c:program filesarduino-00 
rrdrivers 
Finished 
Vista, Seven 
Step 5: Add new Hardware 
Run Device Manager 
Start > Run > devmgmt.msc 
Choose the Arduino 
Other Devices > Arduino Uno (Uno) 
Update Driver 
click “Update Driver” 
Select Driver 
click “Browse My Computer for Driver Software” 
c:program filesarduino-00rrdrivers 
02 INST 
installing 
(software and hardware) 
Step 2: Open The .dmg 
Open (mount) 
arduino-00rr-mac.dmg (rr- version #) 
Step 3: Copy The Application 
Go to 
"Arduino" (in the devices section of finder) 
Move 
"Arduino" Application to the 
"Applications" folder 
Step 4: Install Drivers 
.:Duemilanove Boards Only:. 
Go to 
"Arduino" device 
Double Click & Install 
FTDI Drivers for Intel Macs ( _ _ ).pkg 
rrrr 
(FTDI Drivers for PPC Macs ( _ _ ).pkg 
rrrr 
Restart 
Step 5: Plug In Your Arduino 
Plug your Arduino in: 
Using the included USB cable, plug your Arduino 
board into a free USB port. 
Finished 
.: NOTE: :. 
.: Encountering problems? :. 
.: Would like more details? Using Linux? :. 
.: http://ardx.org/LINU :. 
Go to 
Finished 03
ARDUINO PROGRAMMING IN BRIEF 
// (single line comment) 
It is often useful to write notes 
to yourself as you go along 
about what each line of code 
does. To do this type two 
forward slashes and everything 
until the end of the line will be 
ignored by your program. 
{ } (curly brackets) 
Used to define when a block 
of code starts and ends (used 
in functions as well as loops). 
03 PROG 
programming 
04 
primer 
.:A Small Programming Primer:. 
The Arduino is programmed in the C language. This is a quick little primer targeted at people 
who have a little bit of programing experience and just need a briefing on the idiosyncracies of C 
and the Arduino IDE. If you find the concepts a bit daunting, don't worry, you can start going 
through the circuits and pick up most of it along the way. For a more in-depth intro, the 
Arduino.cc website is a great resource. 
STRUCTURE 
void setup(){ } 
All the code between the two 
curly brackets will be run once 
when your Arduino program 
first runs. 
Each Arduino program 
(often called a sketch) has 
two required functions 
(also called routines). 
void loop(){ } 
This function is run after setup 
has finished. After it has run 
once it will be run again, and 
again, until power is removed. 
SYNTAX 
; (semicolon) 
Each line of code must be 
ended with a semicolon (a 
missing semicolon is often 
the reason for a program 
refusing to compile). 
One of the slightly 
frustrating elements of C is 
its formatting requirements 
(this also makes it very 
powerful). If you remember 
the following you should be 
alright. 
/* */(multi line comment) 
If you have a lot to say you can 
span several lines as a 
comment. Everything between 
these two symbols will be 
ignored in your program. 
A program is nothing more 
than instructions to move 
numbers around in an 
intelligent way. Variables are 
used to do the moving. 
long (long) 
Used when an integer is not 
large enough. Takes 4 bytes (32 
bits) of RAM and has a range 
between -2,147,483,648 and 
2,147,483,647. 
int (integer) 
The main workhorse, stores a 
number in 2 bytes (16 bits). 
Has no decimal places and will 
store a value between -32,768 
and 32,767. 
boolean (boolean) 
A simple True or False 
variable. Useful 
because it only 
uses one bit of 
RAM. 
char (character) 
Stores one character using the 
ASCII code (ie 'A' = 65). Uses 
one byte (8 bits) of RAM. The 
Arduino handles strings as an 
array of char’s. 
float (float) 
Used for floating point math 
(decimals). Takes 4 bytes (32 
bits) of RAM and has a range 
between -3.4028235E+38 
and 3.4028235E+38. 
VARIABLES
03 PROG 
programming 
05 
primer 
MATH OPERATORS 
= (assignment) makes something equal to something else (eg. x 
= 10 * 2 (x now equals 20)) 
% (modulo) gives the remainder when one number is divided by 
another (ex. 12 % 10 (gives 2)) 
+ (addition) 
- (subtraction) 
* (multiplication) 
/ (division) 
Operators used for 
manipulating numbers. 
(they work like simple 
math). 
COMPARISON OPERATORS 
== (equal to) (eg. 12 == 10 is FALSE or 12 == 12 is TRUE) 
!= (not equal to) (eg. 12 != 10 is TRUE or 12 != 12 is FALSE) 
< (less than) (eg. 12 < 10 is FALSE or 12 < 12 is FALSE or 12 < 14 is TRUE) 
> (greater than) (eg. 12 > 10 is TRUE or 12 > 12 is FALSE or 12 > 14 is 
FALSE) 
Operators used for 
logical comparison. 
CONTROL STRUCTURE 
if(condition){ } 
else if( condition ){ } 
else { } 
This will execute the code between 
the curly brackets if the condition 
is true, and if not it will test the 
else if condition if that is also 
false the else code will execute. 
Programs are reliant on 
controlling what runs 
next, here are the basic 
control elements (there 
are many more online). 
for(int i = 0; i < 
#repeats; i++){ } 
Used when you would like to 
repeat a chunk of code a number 
of times (can count up i++ or 
down i-- or use any variable) 
DIGITAL 
digitalWrite(pin, value); 
Once a pin is set as an OUTPUT, 
it can be set either HIGH (pulled 
to +5 volts) or LOW (pulled to 
ground). 
pinMode(pin, mode); 
Used to set a pin's mode, pin 
is the pin number you would 
like to address 0-19 (analog 0- 
5 are 14-19). The mode can 
either be INPUT or OUTPUT. 
int digitalRead(pin); 
Once a pin is set as an INPUT 
you can use this to return 
whether it is HIGH (pulled to 
+5 volts) or LOW (pulled to 
ground). 
ANALOG 
int analogWrite(pin, 
value); 
Some of the Arduino's pins support 
pulse width modulation (3, 5, 6, 9, 10, 
11). This turns the pin on and off very 
quickly making it act like an analog 
output. The value is any number 
between 0 (0% duty cycle ~0v) and 
255 (100% duty cycle ~5 volts). 
The Arduino is a digital 
machine but it has the ability 
to operate in the analog 
realm (through tricks). 
Here's how to deal with 
things that aren't digital. 
int analogRead(pin); 
When the analog input pins are set 
to input you can read their voltage. 
A value between 0 (for 0 
volts) and 1024 (for 
5 volts) will be 
returned. 
.:For a full programming reference visit:. 
http://ardx.org/PROG
04 ELEC 
electronics 
Diode What it Does: No. of Leads: 
The electronic equivalent of a one way 2 
valve. Allowing current to flow in one Things to watch out for: 
direction but not the other. - Will only work in one direction (current will 
Identifying: flow if end with the line is connected to ground) 
Usually a cylinder with wires extending from More Details: 
either end. (and an off center line indicating polarity) http://ardx.org/DIOD 
Hobby Servo What it Does: No. of Leads: 
Takes a timed pulse and converts it into 3 
an angular position of the output shaft. Things to watch out for: 
Identifying: - The plug is not polarized so make sure 
A plastic box with 3 wires coming out one it is plugged in the right way. 
side and a shaft with a plastic horn out More Details: 
the top. http://ardx.org/SERV 
DC Motor What it Does: No. of Leads: 
Spins when a current is passed through it. 2 
Identifying: Things to watch out for: 
This one is easy, it looks like a motor. - Using a transistor or relay that is rated 
Usually a cylinder with a shaft coming out for the size of motor you're using. 
of one end. More Details: 
http://ardx.org/MOTO 
06 
primer 
.:A Small Electronics Primer:. 
ELECTRONICS IN BRIEF 
No previous electronic experience is required to have fun with this kit. Here are a few details 
about each component to make identifying, and perhaps understanding them, a bit easier. If 
at any point you are worried about how a component is used or why it's not working the 
internet offers a treasure trove of advice, or we can be contacted at help@oomlout.com 
COMPONENT DETAILS 
LED 
(Light Emitting Diode) 
What it Does: No. of Leads: 
Emits light when a small current is 2 (one longer, this one connects to positive) 
passed through it. (only in one direction) Things to watch out for: 
Identifying: - Will only work in one direction 
Looks like a mini light bulb. - Requires a current limiting resistor 
More Details: 
http://ardx.org/LED 
Resistors What it Does: No. of Leads: 
Restricts the amount of current that can 2 
flow through a circuit. Things to watch out for: 
Identifying: - Easy to grab the wrong value (double 
Cylinder with wires extending from either check the colors before using) 
end. The value is displayed using a color More Details: 
coding system (for details see next page) http://ardx.org/RESI 
Transistor What it Does: No. of Leads: 
Uses a small current to switch or amplify a 3 (Base, Collector, Emitter) 
much larger current. Things to watch out for: 
Identifying: - Plugging in the right way round (also a 
Comes in many different packages but you current limiting resistor is often needed on the base pin) 
can read the part number off the package. More Details: 
(P2N2222AG in this kit and find a datasheet online) http://ardx.org/TRAN
04 ELEC 
electronics 
What it Does: No. of Leads: 
Packages any range of complicated 2 - 100s (in this kit there is one with 3 (TMP36) and 
electronics inside an easy to use package. one with 16 (74HC595) 
Identifying: Things to watch out for: 
The part ID is written on the outside of the - Proper orientation. (look for marks showing pin 1) 
package. (this sometimes requires a lot of More Details: 
light or a magnifying glass to read). http://ardx.org/ICIC 
07 
primer 
Piezo Element 
What it Does: 
A pulse of current will cause it to click. A No. of Leads: 
stream of pulses will cause it to emit a 2 
tone. Things to watch out for: 
Identifying: - Difficult to misuse. 
In this kit it comes in a little black barrel, More Details: 
but sometimes they are just a gold disc. http://ardx.org/PIEZ 
IC (Integrated Circuit) 
Pushbutton What it Does: No. of Leads: 
Completes a circuit when it is pressed. 4 
Identifying: Things to watch out for: 
A little square with leads out the bottom - these are almost square so can be 
and a button on the top. inserted 90 degrees off angle. 
Potentiometer 
More Details: 
http://ardx.org/BUTT 
What it Does: No. of Leads: 
Produces a variable resistance dependant 3 
on the angular position of the shaft. Things to watch out for: 
Identifying: - Accidentally buying logarithmic scale. 
They can be packaged in many different More Details: 
form factors, look for a dial to identify. http://ardx.org/POTE 
Photo Resistor 
What it Does: No. of Leads: 
Produces a variable resistance dependant 2 
on the amount of incident light. Things to watch out for: 
Identifying: - Remember it needs to be in a voltage 
Usually a little disk with a clear top and a divider before it provides a useful input. 
curvy line underneath. More Details: 
http://ardx.org/PHOT 
RESISTOR COLOR CODE LEAD CLIPPING 
first digit 
second digit 
# of zeros 
tolerance 
Examples: 
green-blue-brown - 560 ohms 
red-red-red - 2 200 ohms (2.2k) 
brown-black-orange - 10 000 ohms (10k) 
0 - Black 5 - Green 20% - none 
1 - Brown 6 - Blue 10% - silver 
2 - Red 7 - Purple 5% - gold 
3 - Orange 8 - Grey 
4 - Yellow 9 - White 
Some components in this kit come with very long wire 
leads. To make them more compatible with a breadboard 
a couple of changes are required. 
LEDs: 
Clip the leads so the long lead is ~10mm (3/8”) long and 
the short one is ~7mm (9/32”). 
Resistors: 
Bend the leads down so they are 90 degrees to the 
cylinder. Then snip them so they are ~6mm 
(1/4”) long. 
Other Components: 
Other components may need clipping. 
Use your discretion when doing so. 
COMPONENT DETAILS (CONT.)
CIRC-01 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Schematic 
Arduino 
pin 13 
longer lead 
LED 
(light emitting diode) 
resistor (330ohm) 
(orange-orange-brown) 
gnd 
(ground) (-) 
08 
.:Getting Started:. 
.:(Blinking LED):. 
LEDs (light emitting diodes) are used in all sorts of clever things 
which is why we have included them in this kit. We will start off 
with something very simple, turning one on and off, repeatedly, 
producing a pleasant blinking effect. To get started, grab the parts 
listed below, pin the layout sheet to your breadboard and then plug 
everything in. Once the circuit is assembled you'll need to upload the program. To do this plug the 
Arduino board into your USB port. Then select the proper port in Tools > Serial Port > (the 
comm port of your Arduino). Next upload the program by going to File > Upload to I/O 
Board (ctrl+U). Finally, bask in the glory and possibility that controlling lights offers. 
If you are having trouble uploading, a full trouble shooting guide can be found here: http://ardx.org/TRBL 
Wire 
5mm Yellow LED 
x1 
330 Ohm Resistor 
Orange-Orange-Brown 
x1 
2 Pin Header 
x4 
CIRC-01 
Breadboard Sheet 
x1 
Parts: 
The Internet 
.:download:. 
+ 
breadboard layout sheet 
http://ardx.org/BBLS01S 
.:view:. 
assembly video 
http://ardx.org/VIDE01
CODE (no need to type everything in just click) 
NOT WORKING? (3 things to try) 
CIRC-01 
09 
File > Examples > 
1.Basic > Blink 
(example from the great arduino.cc site, check it out for other ideas) 
/* Blink 
* Turns on an LED on for one second, then off for one second, 
* repeatedly. 
* Created 1 June 2005 By David Cuartielles 
* http://arduino.cc/en/Tutorial/Blink 
* based on an orginal by H. Barragan for the Wiring i/o board 
*/ 
int ledPin = 13; // LED connected to digital pin 13 
// The setup() method runs once, when the sketch starts 
void setup() { // initialize the digital pin as an output: 
pinMode(ledPin, OUTPUT); } 
// the loop() method runs over and over again, 
// as long as the Arduino has power 
void loop() { 
digitalWrite(ledPin, HIGH); // set the LED on 
delay(1000); // wait for a second 
digitalWrite(ledPin, LOW); // set the LED off 
delay(1000); // wait for a second 
} 
LED Not Lighting Up? 
LEDs will only work in one 
direction. Try taking it out and 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC01 
twisting it 180 degrees. 
(no need to worry, installing it 
backwards does no permanent 
harm). 
Changing the pin: Control the brightness: 
The LED is connected to pin 13 but we can use any of Along with digital (on/off) control the Arduino can control 
the Arduino’s pins. To change it take the wire plugged some pins in an analog (brightness) fashion. (more details on 
into pin 13 and move it to a pin of your choice (from 0- this in later circuits). To play around with it. 
13) (you can also use analog 0-5, analog 0 is 14...) Change the LED to pin 9: (also change the wire) 
Then in the code change the line: ledPin = 13; -> int ledPin = 9; 
int ledPin = 13; -> int ledPin = newpin; Replace the code inside the { }'s of loop() with this: 
Then upload the sketch: (ctrl-u) analogWrite(ledPin, new number); 
Change the blink time: (new number) = any number between 0 and 255. 
Unhappy with one second on one second off? 0 = off, 255 = on, in between = different brightness 
In the code change the lines: Fading: 
digitalWrite(ledPin, HIGH); We will use another included example program. To open go to 
delay(time on); //(seconds * 1000) File > Examples > 3.Analog > Fading 
digitalWrite(ledPin, LOW); 
delay(time off); //(seconds * 1000) Then upload to your board and watch as the LED fades in and 
then out. 
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. 
help@oomlout.com 
Program Not Uploading 
This happens sometimes, 
the most likely cause is a 
confused serial port, you 
can change this in 
tools>serial port> 
MAKING IT BETTER 
MORE, MORE, MORE:
Wire 
CIRC-02 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Parts: 
Schematic 
pin 2 pin 3 pin 4 pin 5 
pin 6 pin 7 pin 8 pin 9 
10 
.:8 LED Fun:. 
.:Multiple LEDs:. 
We have caused one LED to blink, now it's time to up the 
stakes. Lets connect eight. We'll also have an opportunity to 
stretch the Arduino a bit by creating various lighting 
sequences. This circuit is also a nice setup to experiment with 
writing your own programs and getting a feel for how the Arduino works. 
Along with controlling the LEDs we start looking into a few simple programming methods to 
keep your programs small. 
for() loops - used when you want to run a piece of code several times. 
arrays[] - used to make managing variables easier (it's a group of variables). 
5mm Yellow LED 
x8 
330 Ohm Resistor 
Orange-Orange-Brown 
x8 
2 Pin Header 
x4 
CIRC-02 
Breadboard Sheet 
x1 
gnd 
gnd 
The Internet 
.:download:. 
LED 
resistor 
330ohm 
LED 
resistor 
330ohm 
breadboard layout sheet 
http://ardx.org/BBLS02S 
.:view:. 
assembly video 
http://ardx.org/VIDE02
CODE (no need to type everything in just click) 
NOT WORKING? (3 things to try) 
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. 
MAKING IT BETTER 
Switching to loops: Extra animations: 
In the loop() function there are 4 lines. The last Tired of this animation? Then try the other two 
three all start with a '//'. This means the line is sample animations. Uncomment their lines and upload 
treated as a comment (not run). To switch the the program to your board and enjoy the new light 
program to use loops change the void loop() animations. (delete the slashes in front of row 3 and then 4) 
code to: 
Testing out your own animations: //oneAfterAnotherNoLoop(); 
oneAfterAnotherLoop(); Jump into the included code and start changing 
//oneOnAtATime(); things. The main point is to turn an LED on use //inAndOut(); 
Upload the program, and notice that nothing has 
changed. You can take a look at the two it off use digitalWrite(pinNumber, LOW); . 
functions, each does the same thing, but use Type away, regardless of what you change you won't 
different approaches (hint: the second one uses break anything. 
a for loop). 
MORE, MORE, MORE: 
Operating out of sequence 
With eight wires it's easy to cross 
a couple. Double check that the 
first LED is plugged into pin 2 and 
each pin there after. 
CIRC-02 
digitalWrite(pinNumber, HIGH); then to turn 
11 
Download the Code from ( http://ardx.org/CODE02 ) 
(and then copy the text and paste it into an empty Arduino Sketch) 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC02 
Starting Afresh 
Its 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. 
//LED Pin Variables * will then turn them off 
int ledPins[] = {2,3,4,5,6,7,8,9}; 
//An array to hold the void oneAfterAnotherNoLoop(){ 
//pin each LED is connected to int delayTime = 100; 
//i.e. LED #0 is connected to pin 2 / / t h e t i m e ( i n m i l l i s e c o n d s ) t o p a u s e 
//between LEDs 
void setup() digitalWrite(ledPins[0], HIGH); //Turns on LED #0 
{ //(connected to pin 2) 
for(int i = 0; i < 8; i++){ delay(delayTime); //waits delayTime milliseconds 
//this is a loop and will repeat eight times ... 
pinMode(ledPins[i],OUTPUT); ... 
//we use this to set LED pins to output digitalWrite(ledPins[7], HIGH); //Turns on LED #7 
} //(connected to pin 9) 
} delay(delayTime); //waits delayTime milliseconds 
//Turns Each LED Off 
void loop() // run over and over again digitalWrite(ledPins[7], LOW); //Turns off LED #7 
{ delay(delayTime); //waits delayTime milliseconds 
oneAfterAnotherNoLoop(); ... 
//this will turn on each LED one by 
//one then turn each oneoff -----more code in the downloadable version------ 
//oneAfterAnotherLoop(); 
//this does the same as onAfterAnotherNoLoop 
//but with much less typing 
//oneOnAtATime(); 
//inAndOut(); 
} 
/* 
* oneAfterAnotherNoLoop() - Will light one then 
* delay for delayTime then light the next LED it
CIRC-03 
WHAT WE’RE DOING: 
THE CIRCUIT: 
CIRC-03 
Breadboard Sheet 
x1 
Transistor 
P2N2222AG 
Parts: 
Collector Emitter 
12 
.:Spin Motor Spin:. 
.:Transistor & Motor:. 
The Arduino's pins are great for directly controlling small electric 
items like LEDs. However, when dealing with larger items (like a 
toy motor or washing machine), an external transistor is required. A 
transistor is incredibly useful. It switches a lot of current using a 
much smaller current. A transistor has 3 pins. For a negative type (NPN) 
transistor, you connect your load to collector and the emitter to ground. Then when a small current 
flows from base to the emitter, a current will flow through the transistor and your motor will spin 
(this happens when we set our Arduino pin HIGH). There are literally thousands of different types of 
transistors, allowing every situation to be perfectly matched. We have chosen a P2N2222AG a rather 
common general purpose transistor. The important factors in our case are that its maximum voltage 
(40v) and its maximum current (200 milliamp) are both high enough for our toy motor (full details 
can be found on its datasheet http://ardx.org/2222). 
(The 1N4001 diode is acting as a flyback diode for details on why its there visit: http://ardx.org/4001) 
Wire 
Transistor 
P2N2222AG (TO92) 
x1 
10k Ohm Resistor 
Brown-Black-Orange 
x1 
2 Pin Header 
x4 
Toy Motor 
x1 
Arduino 
pin 9 
resistor 
(10kohm) 
gnd 
(ground) (-) 
Base 
Motor 
+5 volts 
The transistor will have 
P2N2222AG printed on it 
(some variations will have 
different pin assignments!) 
Diode 
(1N4001) 
x1 
Diode 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS03S 
.:view:. 
assembly video 
http://ardx.org/VIDE03 
.:NOTE: if your arduino is resetting you need to install the optional capacitor:. 
Schematic 
The Internet
CODE (no need to type everything in just click) 
Download the Code from ( http://ardx.org/CODE03 ) 
(then simply copy the text and paste it into an empty Arduino Sketch) 
int motorPin = 9; //pin the motor is connected to 
void setup() //runs once void motorOnThenOffWithSpeed(){ 
{ int onSpeed = 200;// a number between 
pinMode(motorPin, OUTPUT); //0 (stopped) and 255 (full speed) 
} int onTime = 2500; 
int offSpeed = 50;// a number between 
void loop() // run over and over again //0 (stopped) and 255 (full speed) 
{ int offTime = 1000; 
motorOnThenOff(); analogWrite(motorPin, onSpeed); 
//motorOnThenOffWithSpeed(); // turns the motor On 
//motorAcceleration(); delay(onTime); // waits for onTime milliseconds 
} analogWrite(motorPin, offSpeed); 
// turns the motor Off 
/* delay(offTime); // waits for offTime milliseconds 
* motorOnThenOff() - turns motor on then off } 
* (notice this code is identical to the code we 
used for void motorAcceleration(){ 
* the blinking LED) int delayTime = 50; //time between each speed step 
*/ for(int i = 0; i < 256; i++){ 
void motorOnThenOff(){ //goes through each speed from 0 to 255 
int onTime = 2500; //on time analogWrite(motorPin, i); //sets the new speed 
int offTime = 1000; //off time delay(delayTime);// waits for delayTime milliseconds 
digitalWrite(motorPin, HIGH); } 
// turns the motor On for(int i = 255; i >= 0; i--){ 
delay(onTime); // waits for onTime milliseconds //goes through each speed from 255 to 0 
digitalWrite(motorPin, LOW); analogWrite(motorPin, i); //sets the new speed 
// turns the motor Off delay(delayTime);//waits for delayTime milliseconds 
delay(offTime);// waits for offTime milliseconds } 
} } 
Still No Luck? 
NOT WORKING? (3 things to try) 
If you sourced your own 
motor, double check that it will 
work with 5 volts and that it 
does not draw too much 
power. 
CIRC-03 
Still Not Working? 
Sometimes the Arduino board 
computer. Try un-plugging and 
then re-plugging it into your 
13 
Motor Not Spinning? 
If you sourced your own 
transistor, double check with 
the data sheet that the pinout 
will disconnect from the 
USB port. 
MAKING IT BETTER 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC03 
is compatible with a 
P2N2222AG 
(many are reversed). 
Controlling speed: In the loop() section change it to this 
We played with the Arduino's ability to control the // motorOnThenOff(); 
brightness of an LED earlier now we will use the same motorOnThenOffWithSpeed(); 
feature to control the speed of our motor. The Arduino // motorAcceleration(); 
Then upload the program. You can change the speeds by 
does this using something called Pulse Width 
changing the variables onSpeed and offSpeed. 
Modulation (PWM). This relies on the Arduino's ability to 
operate really, really fast. Rather than directly Accelerating and decelerating: 
controlling the voltage coming from the pin the Arduino Why stop at two speeds, why not accelerate and decelerate 
will switch the pin on and off very quickly. In the the motor. To do this simply change the loop() code to read 
computer world this is going from 0 to 5 volts many // motorOnThenOff(); 
times a second, but in the human world we see it as a // motorOnThenOffWithSpeed(); 
voltage. For example if the Arduino is PWM'ing at 50% motorAcceleration(); 
we see the light dimmed 50% because our eyes are not Then upload the program and watch as your motor slowly 
quick enough to see it flashing on and off. The same accelerates up to full speed then slows down again. If you 
feature works with transistors. Don't believe me? Try it would like to change the speed of acceleration change the 
out. variable delayTime (larger means a longer acceleration time). 
MORE, MORE, MORE:
CIRC-04 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Parts: 
CIRC-04 
Breadboard Sheet 
x1 
Schematic 
signal 
(white) 
gnd 
(black) 
The Internet 
14 
.:A Single Servo:. 
.:Servos:. 
Spinning a motor is good fun but when it comes to projects 
where motion control is required they tend to leave us 
wanting more. The answer? Hobby servos. They are mass 
produced, widely available and cost anything from a couple of 
dollars to hundreds. Inside is a small gearbox (to make the movement more powerful) and 
some electronics (to make it easier to control). A standard servo is positionable from 0 to 
180 degrees. Positioning is controlled through a timed pulse, between 1.25 milliseconds (0 
degrees) and 1.75 milliseconds (180 degrees) (1.5 milliseconds for 90 degrees). Timing 
varies between manufacturer. If the pulse is sent every 25-50 milliseconds the servo will run 
smoothly. One of the great features of the Arduino is it has a software library that allows 
you to control two servos (connected to pin 9 or 10) using a single line of code. 
Wire 
3 Pin Header 
x1 
Mini Servo 
x1 
2 Pin Header 
x4 
Arduino 
pin 9 
gnd 
(ground) (-) 
+5v 
(red) 
Mini Servo 
+5 volts 
(5V) 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS04S 
.:view:. 
assembly video 
http://ardx.org/VIDE04
CODE (no need to type everything in just click) 
// Sweep 
// by BARRAGAN <http://barraganstudio.com> 
#include <Servo.h> 
Servo myservo; // create servo object to control a servo 
int pos = 0; // variable to store the servo position 
myservo.attach(9); // attaches the servo on pin 9 to the servo object 
Still Not Working 
A mistake we made a time or 
two was simply forgetting to 
connect the power (red and 
brown wires) to +5 volts and 
ground. 
CIRC-04 
15 
File > Examples > Servo > Sweep 
(example from the great arduino.cc site, check it out for other great ideas) 
void setup() { 
Servo Not Twisting? 
Even with colored wires it is 
still shockingly easy to plug a 
servo in backwards. This might 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC04 
be the case. 
Potentiometer control: 
void loop() { We have yet to experiment with inputs but if you would like int pulseTime = 2100; //(the number of microseconds 
to read ahead, there is an example program File > Servo > //to pause for (1500 90 degrees // 900 0 degrees 2100 180 degrees) 
Knob. This uses a potentiometer (CIRC08) to control the digitalWrite(servoPin, HIGH); 
delayMicroseconds(pulseTime); 
servo. You can find instructions online here: digitalWrite(servoPin, LOW); 
delay(25); 
http://ardx.org/KNOB } 
Self timing: Great ideas: 
While it is easy to control a servo using the Arduino's included Servos can be used to do all sorts of great things, here are a few of 
library sometimes it is fun to figure out how to program our favorites. 
something yourself. Try it. We're controlling the pulse directly 
Xmas Hit Counter 
so you could use this method to control servos on any of the http://ardx.org/XMAS 
Arduino's 20 available pins (you need to highly optimize this 
code before doing that). Open Source Robotic Arm (uses a servo controller as well as the Arduino) 
http://ardx.org/RARM 
int servoPin = 9; 
void setup(){ Servo Walker 
pinMode(servoPin,OUTPUT); } http://ardx.org/SEWA 
} 
void loop() { 
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees 
{ // in steps of 1 degree 
myservo.write(pos); // tell servo to go to position in variable 'pos' 
delay(15); // waits 15ms for the servo to reach the position 
} 
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees 
{ 
myservo.write(pos); // tell servo to go to position in variable 'pos' 
delay(15); // waits 15ms for the servo to reach the position 
} 
} 
Fits and Starts 
If the servo begins moving then 
twitches, and there's a flashing 
light on your Arduino board, the 
power supply you are using is 
not quite up to the challenge. 
Using a fresh battery instead of 
USB should solve this problem. 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
MORE, MORE, MORE:
There is a half moon 
cutout, this goes at the top 
CIRC-05 .:8 More LEDs:. 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Parts: 
Schematic 
+5 volts 
74HC595 
+5V 
The Internet 
16 
.:74HC595 Shift Register:. 
Time to start playing with chips, or integrated circuits (ICs) as they like to 
be called. The external packaging of a chip can be very deceptive. For 
example, the chip on the Arduino board (a microcontroller) and the one we 
will use in this circuit (a shift register) look very similar but are in fact rather 
different. The price of the ATMega chip on the Arduino board is a few dollars 
while the 74HC595 is a couple dozen cents. It's a good introductory chip, and once you're comfortable playing 
around with it and its datasheet (available online http://ardx.org/74HC595 ) the world of chips will be your oyster. 
The shift register (also called a serial to parallel converter), will give you an additional 8 outputs (to control LEDs 
and the like) using only three Arduino pins. They can also be linked together to give you a nearly unlimited 
number of outputs using the same four pins. To use it you “clock in” the data and then lock it in (latch it). To do 
this you set the data pin to either HIGH or LOW, pulse the clock, then set the data pin again and pulse the clock 
repeating until you have shifted out 8 bits of data. Then you pulse the latch and the 8 bits are transferred to the 
shift registers pins. It sounds complicated but is really simple once you get the hang of it. 
(for a more in depth look at how a shift register works visit: http://ardx.org/SHIF) 
Wire 
Shift Register 
74HC595 
x1 
2 Pin Header 
x4 
330 Ohm Resistor 
Orange-Orange-Brown 
x8 
CIRC-05 
Breadboard Sheet 
x1 
Red LED 
x8 
LED 
resistor 
(330ohm) 
gnd 
(ground) (-) 
pin 
4 
pin 
3 
pin 
2 
0 
1 
2 
3 
4 
5 
6 
7 
data 
clock 
latch 
gnd 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS05S 
.:view:. 
assembly video 
http://ardx.org/VIDE05
CODE (no need to type everything in just click) 
Not Quite Working 
Sorry to sound like a broken 
record but it is probably 
something as simple as a 
crossed wire. 
CIRC-05 
17 
The Arduino’s power 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC05 
LED goes out 
This happened to us a couple 
of times, it happens when the 
chip is inserted backwards. If 
you fix it quickly nothing will 
break. 
Doing it the hard way: //between LED updates 
An Arduino makes rather complex actions very easy, shifting out data is for(int i = 0; i < 8; i++){ 
changeLED(i,ON); 
one of these cases. However one of the nice features of an Arduino is delay(delayTime); 
you can make things as easy or difficult as you like. Let's try an } 
for(int i = 0; i < 8; i++){ 
example of this. In your loop switch the line: changeLED(i,OFF); 
updateLEDs(i) -> updateLEDsLong(i); delay(delayTime); 
Upload the program and notice nothing has changed. If you look at the } 
Uploading this will cause the lights to light up one after another and then off 
code you can see how we are communicating with the chip one bit at a 
in a similar manner. Check the code and wikipedia to see how it works, or 
time. (for more details http://ardx.org/SPI ). 
shoot us an e-mail if you have questions. 
Controlling individual LEDs: 
Time to start controlling the LEDs in a similar method as we did in More animations: 
Now things get more interesting. If you look back to the code from CIRC02 (8 
CIRC02. As the eight LED states are stored in one byte (an 8 bit value) 
LED Fun) you see we change the LEDs using digitalWrite(led, state), this is 
for details on how this works try http://ardx.org/BINA. An Arduino is 
the same format as the routine we wrote changeLED(led, state). You can use 
very good at manipulating bits and there are an entire set of operators 
the animations you wrote for CIRC02 by copying the code into this sketch and 
that help us out. Details on bitwise maths ( http://ardx.org/BITW ). 
changing all the digitalWrite()'s to changeLED()'s. Powerful? Very. (you'll also 
Our implementation. need to change a few other things but follow the compile errors and it works 
Replace the loop() code with itself out). 
int delayTime = 100; //the number of milliseconds 
//to delay 
Frustration? 
Shoot 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. 
help@oomlout.com 
Download the Code from ( http://ardx.org/CODE05 ) 
(copy the text and paste it into an empty Arduino Sketch) 
//Pin Definitions 
//The 74HC595 uses a serial communication 
//link which has three pins 
int data = 2; digitalWrite(latch, LOW); 
int clock = 3; 
int latch = 4; //Pulls the chips latch low 
shiftOut(data, clock, MSBFIRST, value); 
void setup() //runs once //Shifts out 8 bits to the shift register 
{ 
pinMode(data, OUTPUT); 
pinMode(clock, OUTPUT); digitalWrite(latch, HIGH); 
pinMode(latch, OUTPUT); } //Pulls the latch high displaying the data 
} 
void loop() // run over and over again 
{ ---------- More Code Online ---------- 
int delayTime = 100; 
//delay between LED updates 
for(int i = 0; i < 256; i++){ 
updateLEDs(i); 
delay(delayTime); } 
} 
/* 
* updateLEDs() - sends the LED states set 
* in value to the 74HC595 sequence 
*/ 
void updateLEDs(int value){ 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
MORE, MORE, MORE:
CIRC-06 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Parts: 
Schematic 
Arduino 
pin 9 
Piezo 
Element 
gnd 
(ground) (-) 
The Internet 
18 
.:Music:. 
.:Piezo Elements:. 
To this point we have controlled light, motion, and 
electrons. Let's tackle sound next. But sound is an 
analog phenomena, how will our digital Arduino cope? 
We will once again rely on its incredible speed which will let it 
mimic analog behavior. To do this, we will attach a piezo element to one of the 
Arduino's digital pins. A piezo element makes a clicking sound each time it is pulsed 
with current. If we pulse it at the right frequency (for example 440 times a second to 
make the note middle A) these clicks will run together to produce notes. Let's get to 
experimenting with it and get your Arduino playing "Twinkle Twinkle Little Star". 
Wire 
Piezo Element 
x1 
2 Pin Header 
x4 
CIRC-06 
Breadboard Sheet 
x1 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS06S 
.:view:. 
assembly video 
http://ardx.org/VIDE06
CODE (no need to type everything in just click) 
Download the Code from ( http://ardx.org/CODE06 ) 
(copy the text and paste it into an empty Arduino Sketch) 
/* Melody 
* (cleft) 2005 D. Cuartielles for K3 
* digitalWrite(speakerPin, 
* This example uses a piezo speaker to play melodies. It sends LOW); 
* a square wave of the appropriate frequency to the piezo, delayMicroseconds(tone); 
* generating the corresponding tone. } 
* } 
* The calculation of the tones is made following the 
* mathematical operation: void playNote(char note, int duration) { 
* char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; 
* timeHigh = period / 2 = 1 / (2 * toneFrequency) int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 
* 
* where the different tones are described as in the table: }; 
* // play the tone corresponding to the note name 
* note frequency period timeHigh for (int i = 0; i < 8; i++) { 
* c 261 Hz 3830 1915 if (names[i] == note) { 
* d 294 Hz 3400 1700 playTone(tones[i], duration); 
* e 329 Hz 3038 1519 } 
* f 349 Hz 2864 1432 } 
* g 392 Hz 2550 1275 } 
* a 440 Hz 2272 1136 
* b 493 Hz 2028 1014 void setup() { 
* C 523 Hz 1912 956 pinMode(speakerPin, OUTPUT); 
* } 
* http://www.arduino.cc/en/Tutorial/Melody 
*/ void loop() { 
int speakerPin = 9; if (notes[i] == ' ') { 
int length = 15; // the number of notes delay(beats[i] * tempo); // rest 
char notes[] = "ccggaagffeeddc "; // a space represents a rest } else { 
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; playNote(notes[i], beats[i] * tempo); 
int tempo = 300; } 
void playTone(int tone, int duration) { delay(tempo / 2); } 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
MORE, MORE, MORE: 
CIRC-06 
for (int i = 0; i < length; i++) { 
// pause between notes 
Tired of Twinkle Twinkle 
The code is written so you can 
easily add your own songs, 
check out the code below to 
19 
Can't Think While the 
Melody is Playing? 
Just pull up the piezo element 
whilst you think, upload your 
program then plug it back in. 
Little Star? 
get started. 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC06 
No Sound 
Given the size and shape of 
the piezo element it is easy to 
miss the right holes on the 
breadboard. Try double 
checking its placement. 
Playing with the speed: char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 
The timing for each note is calculated based on 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 
variables, as such we can tweak the sound of each note 1014, 956 }; 
or the timing. To change the speed of the melody you Composing your own melodies: 
need to change only one line. The program is pre-set to play 'Twinkle Twinkle Little Star' 
int tempo = 300; ---> int tempo = (new #) however the way it is programmed makes changing the song 
Change it to a larger number to slow the melody down, 
easy. Each song is defined in one int and two arrays, the int 
or a smaller number to speed it up. 
Tuning the notes: length defines the number of notes, the first array 
If you are worried about the notes being a little out of notes[] defines each note, and the second beats[] 
tune this can be fixed as well. The notes have been defines how long each note is played. Some Examples: 
calculated based on a formula in the comment block at Twinkle Twinkle Little Star int length = 15; 
the top of the program. But to tune individual notes just char notes[] = {"ccggaagffeeddc "}; 
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, adjust their values in the tones[] array up or down 1, 1, 2, 4 }; 
until they sound right. (each note is matched by its Happy Birthday (first line) 
int length = 13; name in the names[] (array ie. c = 1915 ) char notes[] = {"ccdcfeccdcgf "}; 
int beats[] = {1,1,1,1,1,2,1,1,1,1,1,2,4}; 
for (long i = 0; i < duration * 1000L; i += tone * 2) { } 
digitalWrite(speakerPin, HIGH); 
delayMicroseconds(tone);
CIRC-07 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Parts: 
Schematic 
The Internet 
20 
.:Button Pressing:. 
.:Pushbuttons:. 
Up to this point we have focused entirely on outputs, time to 
get our Arduino to listen, watch and feel. We'll start with a 
simple pushbutton. Wiring up the pushbutton is simple. There is 
one component, the pull up resistor, that might seem out of place. 
This is included because an Arduino doesn't sense the same way we do (ie button pressed, 
button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH 
or LOW. The button is set up to pull the Arduino's pin LOW when it is pressed, however, when 
the button is unpressed the voltage of the pin will float (causing occasional errors). To get the 
Arduino to reliably read the pin as HIGH when the button is unpressed, we add the pull up 
resistor. 
(note: the first example program uses only one of the two buttons) 
Wire 
Pushbutton 
x2 
2 Pin Header 
x4 
330 Ohm Resistor 
Orange-Orange-Brown 
x1 
CIRC-07 
Breadboard Sheet 
x1 
Red LED 
x1 
10k Ohm Resistor 
Brown-Black-Orange 
x2 
Arduino 
pin 13 
LED 
resistor 
(330 ohm) 
gnd 
(ground) (-) 
Arduino 
pin 2 
pin 3 
+5 volts 
resistor 
(pull-up) 
(10k ohm) 
pushbutton 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS07S 
.:view:. 
assembly video 
http://ardx.org/VIDE07
CODE (no need to type everything in just click) 
NOT WORKING? (3 things to try) 
Light Not Turning On 
The pushbutton is square 
and because of this it is easy 
to put it in the wrong way. 
Give it a 90 degree twist and 
MAKING IT BETTER 
MORE, MORE, MORE: 
Light Not Fading 
A bit of a silly mistake we 
constantly made, when you 
switch from simple on off to 
fading remember to move the 
LED wire from pin 13 to pin 9. 
CIRC-07 
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. 
21 
File > Examples > 2.Digital > Button 
(example from the great arduino.cc site, check it out for other great ideas) 
/* 
* Button 
* by DojoDave <http://www.0j0.org> 
* 
* Turns on and off a light emitting diode(LED) connected to digital 
* pin 13, when pressing a pushbutton attached to pin 7. 
* http://www.arduino.cc/en/Tutorial/Button 
*/ 
int ledPin = 13; // choose the pin for the LED 
int inputPin = 2; // choose the input pin (for a pushbutton) 
int val = 0; // variable for reading the pin status 
void setup() { 
pinMode(ledPin, OUTPUT); // declare LED as output 
pinMode(inputPin, INPUT); // declare pushbutton as input 
} 
void loop(){ 
val = digitalRead(inputPin); // read input value 
if (val == HIGH) { // check if the input is HIGH 
digitalWrite(ledPin, LOW); // turn LED OFF 
} else { 
digitalWrite(ledPin, HIGH); // turn LED ON 
} 
} 
Underwhelmed? 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC07 
see if it starts working. 
On button off button: Fading up and down: 
The initial example may be a little underwhelming (ie. I Lets use the buttons to control an analog signal. To do this 
don't really need an Arduino to do this), let’s make it a you will need to change the wire connecting the LED from pin 
little more complicated. One button will turn the LED on 13 to pin 9, also change this in code. 
the other will turn the LED off. Change the code to: int ledPin = 13; ----> int ledPin = 9; 
int ledPin = 13; // choose the pin for the LED Next change the loop() code to read. 
int inputPin1 = 3; // button 1 int value = 0; 
int inputPin2 = 2; // button 2 void loop(){ 
if (digitalRead(inputPin1) == LOW) { value--; } 
void setup() { else if (digitalRead(inputPin2) == LOW) { value++; } 
pinMode(ledPin, OUTPUT); // declare LED as output value = constrain(value, 0, 255); 
pinMode(inputPin1, INPUT); // make button 1 an input analogWrite(ledPin, value); 
pinMode(inputPin2, INPUT); // make button 2 an input delay(10); 
} } 
void loop(){ if (digitalRead(inputPin1) == LOW) { Changing fade speed: 
digitalWrite(ledPin, LOW); // turn LED OFF If you would like the LED to fade faster or slower, there is only } else if (digitalRead(inputPin2) == LOW) { 
digitalWrite(ledPin, HIGH); // turn LED ON one line of code that needs changing; } 
} delay(10); ----> delay(new #); Upload the program to your board, and start toggling the To fade faster make the number smaller, slower requires a 
LED on and off. larger number.
CIRC-08 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Schematic 
Potentiometer 
Parts: 
The Internet 
.:Twisting:. 
.:Potentiometers:. 
Along with the digital pins, the Arduino also has 6 
pins which can be used for analog input. These 
inputs take a voltage (from 0 to 5 volts) and convert 
it to a digital number between 0 (0 volts) and 1024 (5 volts) (10 bits of 
resolution). A very useful device that exploits these inputs is a potentiometer 
(also called a variable resistor). When it is connected with 5 volts across its 
outer pins the middle pin will read some value between 0 and 5 volts 
dependent on the angle to which it is turned (ie. 2.5 volts in the middle). We 
can then use the returned values as a variable in our program. 
Potentiometer Wire 
10k ohm 
x1 
2 Pin Header 
x4 
330 Ohm Resistor 
Orange-Orange-Brown 
x1 
CIRC-08 
Breadboard Sheet 
x1 
Yellow LED 
x1 
Arduino 
pin 13 
LED 
(light 
emitting 
diode) 
resistor (330ohm) 
(orange-orange-brown) 
gnd 
(ground) (-) 
+5 volts 
Arduino 
analog 
pin 0 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS08S 
.:view:. 
assembly video 
http://ardx.org/VIDE08 
22
CIRC-08 
CODE (no need to type everything in just click) 
File > Examples > 3.Analog > AnalogInput 
(example from the great arduino.cc site, check it out for other great ideas) 
/* Analog Input 
* Demonstrates analog input by reading an analog sensor on analog 
* pin 0 and turning on and off a light emitting diode(LED) connected to 
digital pin 13. 
* The amount of time the LED will be on and off depends on the value obtained by 
* analogRead(). 
* Created by David Cuartielles 
* Modified 16 Jun 2009 
* By Tom Igoe 
* http://arduino.cc/en/Tutorial/AnalogInput 
*/ 
int sensorPin = 0; // select the input pin for the potentiometer 
int ledPin = 13; // select the pin for the LED 
int sensorValue = 0; // variable to store the value coming from the sensor 
void setup() { 
pinMode(ledPin, OUTPUT); //declare the ledPin as an OUTPUT: 
Not Working 
Make 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) 
} 
void loop() { 
sensorValue = analogRead(sensorPin);// read the value from the sensor: 
digitalWrite(ledPin, HIGH); // turn the ledPin on 
delay(sensorValue); // stop the program for <sensorValue> milliseconds: 
digitalWrite(ledPin, LOW); // turn the ledPin off: 
delay(sensorValue); // stop the program for for <sensorValue> milliseconds: 
} 
Sporadically Working 
This is most likely due to a 
slightly dodgy connection with 
the potentiometer's pins. This 
can usually be conquered by 
taping the potentiometer down. 
Threshold switching: Then change the loop code to. 
Sometimes you will want to switch an output when a value void loop() { 
int value = analogRead(potPin) / 4; 
exceeds a certain threshold. To do this with a analogWrite(ledPin, value); 
potentiometer change the } loop() code to. Upload the code and watch as your LED fades in relation to 
void loop() { int threshold = 512; your potentiometer spinning. (Note: the reason we divide the 
if(analogRead(sensorPin) > threshold){ value by 4 is the analogRead() function returns a value from 0 
digitalWrite(ledPin, HIGH);} else{ digitalWrite(ledPin, LOW);} to 1024 (10 bits), and analogWrite() takes a value from 0 to 
} 255 (8 bits) ) 
This will cause the LED to turn on when the value is above Controlling a servo: 
512 (about halfway), you can adjust the sensitivity by This is a really neat example and brings a couple of circuits 
changing the threshold value. together. Wire up the servo like you did in CIRC-04, then open 
Fading: the example program Knob (File > Examples > Servo > 
Let’s control the brightness of an LED directly from the 
Knob ), then change one line of code. 
potentiometer. To do this we need to first change the pin int potpin = 0; ----> int potpin = 2; 
the LED is connected to. Move the wire from pin 13 to pin Upload to your Arduino and then watch as the servo shaft turns 
9 and change one line in the code. as you turn the potentiometer. 
int ledPin = 13; ----> int ledPin = 9; 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC08 
Still Backward 
You can try operating the 
circuit upside down. 
Sometimes this helps. 
23 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
MORE, MORE, MORE:
CIRC-09 
WHAT WE’RE DOING: 
THE CIRCUIT: 
24 
.:Light:. 
.:Photo Resistors:. 
Whilst getting input from a potentiometer can be useful 
for human controlled experiments, what do we use 
when we want an environmentally controlled 
experiment? We use exactly the same principles but instead 
of a potentiometer (twist based resistance) we use a photo resistor (light based 
resistance). The Arduino cannot directly sense resistance (it senses voltage) so we 
set up a voltage divider (http://ardx.org/VODI). The exact voltage at the sensing 
pin is calculable, but for our purposes (just sensing relative light) we can 
experiment with the values and see what works for us. A low value will occur when 
the sensor is well lit while a high value will occur when it is in darkness. 
Wire 
Photo-Resistor 
x1 
2 Pin Header 
x4 
330 Ohm Resistor 
Orange-Orange-Brown 
x1 
CIRC-09 
Breadboard Sheet 
x1 
Yellow LED 
x1 
10k Ohm Resistor 
Brown-Black-Orange 
x1 
Parts: 
Schematic 
Arduino 
pin 13 
LED 
resistor 
(330ohm) 
gnd 
(ground) (-) 
+5 volts 
photo 
resistor 
Arduino 
analog 
pin 0 
resistor 
(10k ohm) 
The Internet 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS09S 
.:view:. 
assembly video 
http://ardx.org/VIDE09
CODE (no need to type everything in just click) 
Download the Code from ( http://ardx.org/CODE09 ) 
(copy the text and paste it into an empty Arduino Sketch) 
CIRC-09 
/* 
* A simple programme that will change the //output 
* intensity of an LED based on the amount of } 
* light incident on the photo resistor. /* 
* * loop() - this function will start after setup 
*/ * finishes and then repeat 
//PhotoResistor Pin void loop() 
int lightPin = 0; //the analog pin the { 
//photoresistor is int lightLevel = analogRead(lightPin); //Read the 
//connected to // lightlevel 
//the photoresistor is not lightLevel = map(lightLevel, 0, 900, 0, 255); 
//calibrated to any units so //adjust the value 0 to 900 to 0 to 255 
//this is simply a raw sensor lightLevel = constrain(lightLevel, 0, 255); 
//value (relative light) //make sure the value is betwween 0 and 255 
//LED Pin analogWrite(ledPin, lightLevel); //write the value 
int ledPin = 9;//the pin the LED is connected to } 
NOT WORKING? (3 things to try) 
LED Remains Dark 
This is a mistake we continue 
to make time and time again, 
if only they could make an LED 
that worked both ways. Pull it 
MAKING IT BETTER 
MORE, MORE, MORE: 
Still not quite working? 
You may be in a room which is 
either too bright or dark. Try 
turning the lights on or off to 
see if this helps. Or if you have 
a flashlight near by give that a 
try. 
*/ 
It Isn't Responding to 
Changes in Light. 
Given that the spacing of the 
wires on the photo-resistor is 
not standard, it is easy to 
misplace it. Double check its in 
the right place. 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC09 
up and give it a twist. 
Reverse the response: Light controlled servo: 
Perhaps you would like the opposite response. Don't Let's use our newly found light sensing skills to control a 
worry we can easily reverse this response just change: servo (and at the same time engage in a little bit of Arduino 
analogWrite(ledPin, lightLevel); ----> code hacking). Wire up a servo connected to pin 9 (like in 
analogWrite(ledPin, 255 - lightLevel); CIRC-04). Then open the Knob example program (the same 
Upload and watch the response change: one we used in CIRC-08) File > Examples > Servo > 
Night light: Knob. Upload the code to your board and watch as it works 
Rather than controlling the brightness of the LED in unmodified. 
response to light, let's instead turn it on or off based on Using the full range of your servo: 
You'll notice that the servo will only operate over a limited 
a threshold value. Change the loop() code with. 
void loop(){ portion of its range. This is because with the voltage dividing 
int threshold = 300; circuit we use the voltage on analog pin 0 will not range from 
if(analogRead(lightPin) > threshold){ 
digitalWrite(ledPin, HIGH); 0 to 5 volts but instead between two lesser values (these 
}else{ 
digitalWrite(ledPin, LOW); values will change based on your setup). To fix this play with 
} the val = map(val, 0, 1023, 0, 179); line. For hints on what to } 
do visit http://arduino.cc/en/Reference/Map . 
//we are controlling brightness so 
//we use one of the PWM (pulse 
//width modulation pins) 
void setup() 
{ 
pinMode(ledPin, OUTPUT); //sets the led pin to 
25
CIRC-10 .:Temperature:. 
WHAT WE’RE DOING: 
THE CIRCUIT: 
Parts: 
26 
.:TMP36 Precision Temperature Sensor:. 
What's the next phenomena we will measure with our 
Arduino? Temperature. To do this we'll use a rather 
complicated IC (integrated circuit) hidden in a package 
identical to our P2N2222AG transistors. It has three pin's, 
ground, signal and +5 volts, and is easy to use. It outputs 10 
millivolts per degree centigrade on the signal pin (to allow measuring temperatures below 
freezing there is a 500 mV offset eg. 25 ° C = 750 mV, 0 ° C = 500mV 
). To convert this from the 
digital value to degrees, we will use some of the Arduino's math abilities. Then to display it 
we'll use one of the IDE's rather powerful features, the debug window. We'll output the value 
over a serial connection to display on the screen. Let's get to it. 
One extra note, this circuit uses the Arduino IDE's serial monitor. To open this, first upload the 
program then click the button which looks like a square with an antennae. 
The TMP36 Datasheet: 
http://ardx.org/TMP36 
Wire 
TMP36 
Temperature Sensor 
x1 
2 Pin Header 
x4 
CIRC-10 
Breadboard Sheet 
x1 
Schematic 
+5 volts 
TMP36 
(precision 
temperature 
sensor) 
+5v 
signal the chip will have 
gnd 
(ground) (-) 
Arduino 
analog 
pin 0 
gnd 
TMP36 printed 
on it 
The Internet 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS10S 
.:view:. 
assembly video 
http://ardx.org/VIDE10
CODE (no need to type everything in just click) 
Download the Code from ( http://ardx.org/CODE10 ) 
(copy the text and paste it into an empty Arduino Sketch) 
/* --------------------------------------------- void loop() 
* | Arduino Experimentation Kit Example Code | // run over and over again 
* | CIRC-10 .: Temperature :. | { 
* --------------------------------------------- float temperature = getVoltage(temperaturePin); 
* //getting the voltage reading from the 
* A simple program to output the current temperature //temperature sensor 
* to the IDE's debug window 
* For more details on this circuit: 
temperature = (temperature - .5) * 100;//converting from 10 
//TMP36 Pin Variables mv 
int temperaturePin = 0;//the analog pin the TMP36's //per degree wit 500 mV offset to 
//Vout pin is connected to //degrees ((volatge - 500mV) times 
//the resolution is 
//10 mV / degree centigrade 1 0 0 ) 
//(500 mV offset) to make Serial.println(temperature); //printing the result 
//negative temperatures an delay(1000); //waiting a second 
Gibberish is Displayed 
This happens because the serial 
monitor is receiving data at a 
different speed than expected. 
To fix this, click the pull-down 
box that reads "*** baud" and 
change it to "9600 baud". 
CIRC-10 
Temperature Value is 
Serial.println(" degrees centigrade"); voltage we simply display the result of getVoltage(). The change to the first line means when we next output it 
delete the line temperature = (temperature - .5) * 100; w i ll a ppear on the same line, then we add the informative 
Outputting degrees Fahrenheit: text and a new line. 
Again this is a simple change requiring only math. To Changing the serial speed: 
go degrees C ----> degrees F we use the formula: If you ever wish to output a lot of data over the serial line 
( F = C * 1.8) + 32 ) time is of the essence. We are currently transmitting at 9600 
add the line baud but much faster speeds are possible. To change this 
temperature = 
before Serial.println(temperature); Serial.begin(9600); ----> Serial.begin(115200); 
Upload the sketch turn on the serial monitor, then change 
More informative output: the speed from 9600 baud to 115200 baud in the pull down 
Let's add a message to the serial output to make what 
27 
Nothing Seems to Happen 
This 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) 
Outputting voltage: do this first revert to the original code then change: 
This is a simple matter of changing one line. Our Serial.println(temperature); 
----> 
sensor outputs 10mv per degree centigrade so to get Serial.print(temperature); 
(((temperature - .5) * 100)*1.8) + 32; change the line: 
menu. You are now transmitting data 12 times faster. 
is appearing in the Serial Monitor more informative. To 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC10 
Unchanging 
Try pinching the sensor with 
your fingers to heat it up or 
pressing a bag of ice against it 
to cool it down. 
} 
option 
/* 
void setup() * getVoltage() - returns the voltage on the analog input 
{ * defined by pin 
Serial.begin(9600); //Start the serial connection */ 
//with the computer float getVoltage(int pin){ 
//to view the result open the return (analogRead(pin) * .004882814);//converting from a 0 
//serial monitor //to 1024 digital range 
//last button beneath the file // to 0 to 5 volts 
//bar (looks like a box with an //(each 1 reading equals ~ 5 
//antenna) 
} millivolts 
} 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
MORE, MORE, MORE:
CIRC-11 
WHAT WE’RE DOING: 
THE CIRCUIT: 
com NO 
28 
.:Larger Loads:. 
.:Relays:. 
This next circuit is a bit of a test. We combine what we learned 
about using transistors in CIRC03 to control a relay. A relay is 
an electrically controlled mechanical switch. Inside the little 
plastic box is an electromagnet that, when energized, causes a 
switch to trip (often with a very satisfying clicking sound). You can buy relays that vary in size 
from a quarter of the size of the one in this kit up to as big as a fridge, each capable of 
switching a certain amount of current. They are immensely fun because there is an element of 
the physical to them. While all the silicon we've played with to this point is fun sometimes, you 
may just want to wire up a hundred switches to control something magnificent. Relays give you 
the ability to dream it up then control it with your Arduino. Now to using today's technology to 
control the past. (The 1N4001 diode is acting as a flyback diode, for details on why it's there visit: http://ardx.org/4001) 
Parts: 
CIRC-11 
Breadboard Sheet 
x1 
10k Ohm Resistor 
Brown-Black-Orange 
x1 
2 Pin Header 
x4 
330 Ohm Resistor 
Orange-Orange-Brown 
x2 
Relay 
(SPDT) 
x1 
Red LED 
x1 
Schematic 
Arduino 
pin 2 
resistor 
(10kohm) 
Base 
Transistor 
P2N2222AG 
Collector Emitter 
Diode 
(flyback) 
+5 volts 
gnd 
(ground) (-) 
Diode 
(1N4001) 
x1 
the transistor will have 
P2N2222AG printed on it 
(some variations will have 
the pin assignment reversed) 
Transistor 
P2N2222AG (TO92) 
x1 
Yellow LED 
x1 
coil - 
NC 
The Internet 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS11S 
.:view:. 
assembly video 
http://ardx.org/VIDE11
CODE (no need to type everything in just click) 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
CIRC-11 
Not Quite Working 
The 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). 
Watch the Back-EMF Pulse 
Replace the diode with an LED. You’ll see it blink each time it “snubs” the coil voltage spike when it turns off. 
Controlling a Motor 
In CIRC-03 we controlled a motor using a transistor. However if you want to control a larger motor a relay is a good option. 
To do this simply remove the red LED, and connect the motor in its place (remember to bypass the 330 Ohm resistor). 
MORE, MORE, MORE: 
No Clicking Sound 
The transistor or coil portion of 
the circuit isn't quite working. 
Check the transistor is plugged 
in the right way. 
29 
File > Examples > 
1.Basic > Blink 
(example from the great arduino.cc site, check it out for other great ideas) 
/* 
* Blink 
* 
* The basic Arduino example. Turns on an LED on for one second, 
* then off for one second, and so on... We use pin 13 because, 
* depending on your Arduino board, it has either a built-in LED 
* or a built-in resistor so that you need only an LED. 
* 
* http://www.arduino.cc/en/Tutorial/Blink 
*/ 
int ledPin = 2; // *********** CHANGE TO PIN 2 ************ 
void setup() // run once, when the sketch starts 
{ 
pinMode(ledPin, OUTPUT); // sets the digital pin as output 
} 
void loop() // run over and over again 
{ 
digitalWrite(ledPin, HIGH); // sets the LED on 
delay(1000); // waits for a second 
digitalWrite(ledPin, LOW); // sets the LED off 
delay(1000); // waits for a second 
} 
More details, where to buy more parts, where to ask more questions: 
http://ardx.org/CIRC11 
Nothing Happens 
The example code uses pin 13 
and we have the relay 
connected to pin 2. Make sure 
you made this change in the 
code.
CIRC-12 
WHAT WE’RE DOING: 
THE CIRCUIT: 
330 Ohm Resistor 
Orange-Orange-Brown 
x3 
2 Pin Header 
x4 
CIRC-12 
Breadboard Sheet 
x1 
Parts: 
Schematic 
Arduino 
pin 11 pin 10 pin 9 
The Internet 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS12 
30 
.:Colorful Light:. 
.:RGB LEDs:. 
When you first started with CIRC01 you were happy just to 
get a red LED blinking away. But you're past that now, 
right? You want orange, you want teal, you want aubergine! 
Fortunately there's a way to shine multiple colors from a single 
LED without having to stock up on every shade of the rainbow. To do this we use a RGB 
LED. An RGB LED isn't a single LED it's actually three LEDs in one small package: one 
Red, one Green and one Blue. When you turn them on their light mixes together and you 
get other colors. The color you get is a result of the intensity of the individual red, green 
and blue LEDs. We control the intensity with Pulse Width Modulation (PWM) which we've 
used before to control LED brightness and motor speed. 
Wire 
5mm RGB LED 
x1 
resistor 
(330ohm) 
common (gnd) 
red 
green 
blue 
gnd 
blue 
green 
red 
longest 
lead 
flat 
side
CODE (no need to type everything in just click) 
/*Cycles through the colors of a RGB LED*/ 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
MORE, MORE, MORE: 
31 
Download the Code from (http://ardx.org/CODE12S) 
(copy the text and paste it into an empty Arduino Sketch) 
Seeing Red 
The 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_LED_PIN, 
redIntensity); 
to 
analogWrite(RED_LED_PIN, 
redIntensity/3); 
Looking For More? 
More details, where to buy more parts, where to ask more questions: 
http://sparkfun.com/RGB 
(shameless plug) 
If you’re looking to do more 
why not check out all the 
lovely extra bits and bobs 
available from 
http://www.SparkFun.com 
LED Remains Dark or 
Shows Incorrect Color 
With 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. 
// Cycle color from green through to blue 
// LED leads connected to PWM pins for (blueIntensity = 0; 
const int RED_LED_PIN = 9; blueIntensity <= 255; 
const int GREEN_LED_PIN = 10; blueIntensity+=5) { 
const int BLUE_LED_PIN = 11; greenIntensity = 255-blueIntensity; 
// Used to store the current intensity level analogWrite(BLUE_LED_PIN, blueIntensity); 
int redIntensity = 0; analogWrite(GREEN_LED_PIN, greenIntensity); 
int greenIntensity = 0; delay(DISPLAY_TIME); 
int blueIntensity = 0; } 
// Length of time showing each color // Cycle cycle from blue through to red 
const int DISPLAY_TIME = 100; // milliseconds for (redIntensity = 0; 
redIntensity <= 255; 
void setup() { redIntensity+=5) { 
// No setup required. blueIntensity = 255-redIntensity; 
} analogWrite(RED_LED_PIN, redIntensity); 
analogWrite(BLUE_LED_PIN, blueIntensity); 
void loop() { delay(DISPLAY_TIME); 
// Cycle color from red through to green } 
for (greenIntensity = 0; } 
greenIntensity <= 255; 
greenIntensity+=5) { ---------- More Code Online ---------- 
redIntensity = 255-greenIntensity; 
analogWrite(GREEN_LED_PIN, greenIntensity); 
analogWrite(RED_LED_PIN, redIntensity); 
delay(DISPLAY_TIME); 
} 
Using HTML-style color codes A land of diffusion 
If you're familiar with making web pages you One disadvantage of using a RGB LED made up of 
might prefer to specify colors using "hex triplets" three separate LEDs to generate our colors is that 
like you do when you use HTML and CSS. A hex sometimes it's possible to see the color of the 
triplet specifies a color using a series of letters individual lights. One way to workaround this is to find 
and numbers like `#FF0000` for red or a way to make the light more diffuse (or scattered) so 
`#800080` for purple. You can learn more about that the individual colors mix together better. The LED 
how this works on Wikipedia supplied with your kit is diffused rather than clear to 
(http://ardx.org/HEXCOL) and find a list of help improve the effectiveness of the color mixing. If 
colors with their associated hex triplets so you the light still isn't diffuse enough you can try putting 
don't need to work it out yourself. the LED behind some paper or acrylic; or inside a ping 
Download the code from: pong ball or polystyrene ball. 
http://ardx.org/RGBMB 
CIRC-12
10k Ohm Resistor 
Brown-Black-Orange 
x1 
2 Pin Header 
x4 
WHAT WE’RE DOING: 
THE CIRCUIT: 
CIRC-13 
Breadboard Sheet 
x1 
Parts: 
signal 
(white) 
gnd 
(black) 
The Internet 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS13S 
32 
.:Measuring Bends:. 
.:Flex Sensor:. 
In life it's important to be flexible. But what do you do 
if you want to measure how flexible an object is? You 
use a flex sensor. A flex sensor uses carbon on a strip 
of plastic to act like a variable resistor or potentiometer 
(CIRC-08) but instead of changing the resistance by turning a knob you change it 
by flexing (bending) the component. We use a "voltage divider" again (CIRC-08 & 
09) to detect this change in resistance. The 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 flex sensor to 
control the position of a servo. 
Wire 
Flex Sensor 
x1 
CIRC-13 
3 Pin Header 
x1 
Mini Servo 
x1 
Schematic 
+5 volts 
flex 
sensor 
analog 
pin 0 
resistor 
(10k ohm) 
pin 9 
gnd 
(ground) (-) 
+5v 
(red) 
Mini Servo
CODE (no need to type everything in just click) 
CIRC-13 
// Based on } 
// File > Examples > Servo > Knob 
// Controlling a servo position using a void loop() 
// potentiometer (variable resistor) { 
// by Michal Rinott // val = analogRead(potpin);// reads the value 
<http://people.interaction- // of the potentiometer 
NOT WORKING? (3 things to try) 
Servo Not Twisting? 
Even with colored wires it is 
still shockingly easy to plug a 
servo in backwards. This might 
be the case. 
MAKING IT BETTER 
MORE, MORE, MORE: 
33 
Download the Code from (http://ardx.org/CODE13S) 
(copy the text and paste it into an empty Arduino Sketch) 
//(value between 0 and 1023) 
Servo Not Moving As 
Expected 
The sensor is only designed 
to work in one direction. Try 
flexing it the other way. 
(where the striped side faces 
in on a convex curve) 
More details, where to buy more parts, where to ask more questions: 
http://sparkfun.com/ 
Servo Moves Once 
You may need to modify the 
range of values in the call to 
the map() function 
(details in the making it better 
section below) 
ivrea.it/m.rinott> Serial.println(val); 
val = map(val, 50, 300, 0, 179); 
#include <Servo.h> // scale it to use it with the servo 
// (value between 0 and 180) 
Servo myservo; // create servo object to myservo.write(val); // sets the servo 
// control a servo // according to the 
// scaled value 
int potpin = 0; // analog pin used to 
// connect the delay(15); // waits for the servo 
// potentiometer // to get there 
int val; // variable to read the } 
//value from the analog pin 
void setup() 
{ 
Serial.begin(9600); 
myservo.attach(9);// attaches the servo 
// servo on pin 9 to the servo object 
Calibrating the Range 300) value with the fully bent value. 
While the servo is now moving chances are its Applications 
range isn’t quite perfect. To adjust the range we 
With sensors the real fun comes in using them in neat 
need to change the values in the map() function. 
and un-expected ways here are a few of our favorite 
map(value, fromLow, fromHigh, 
toLow, toHigh) flex sensor applications. 
For full details on how it works: One Player Rock Paper Scissors Glove 
http://ardx.org/MAP A glove that lets you play RPS against yourself. 
http://ardx.org/RPS 
To calibrate our sensor we can use the debug 
window (like in CIRC-11). Open the debug Electronic Plant Brace 
window then replace the Monitor if your plant is bending towards light fromLow value (default 
50) with the value displayed when the sensor is and fix it. http://ardg.org/BRACE 
un bent. Then replace the fromHigh (default
2 Pin Header 
x4 
330 Ohm Resistor 
Orange-Orange-Brown 
x3 
WHAT WE’RE DOING: 
THE CIRCUIT: 
CIRC-14 
Breadboard Sheet 
x1 
Parts: 
Schematic 
Arduino 
pin 11 pin 10 pin 9 
The Internet 
.:download:. 
breadboard layout sheet 
http://ardx.org/BBLS14S 
34 
.:Fancy Sensing:. 
.:Soft Potentiometer:. 
A "soft pot" (short for "soft potentiometer") is like a 
regular potentiometer of the style seen in CIRC-08 
except, it's flat, really thin, flexible and doesn't have a 
knob. A potentiometer is also known as a "variable 
resistor" and for a soft pot the resistance it provides is determined by where 
pressure is applied. Pressure can be applied with your finger, a stylus or a hard 
plastic "wiper". By pressing down on various parts of the strip, the resistance 
varies from 100 to 10k Ohms allowing you to calculate the relative position on the 
strip. You can use this to track movement on the softpot or discrete "button style" 
presses. In this circuit we’ll use it to control the color of an RGB LED. 
Wire 
5mm RGB LED 
x1 
CIRC-14 
Soft Potentiometer 
x1 
resistor 
(330ohm) 
gnd 
blue 
green 
red 
longest 
lead 
+5 volts 
Arduino 
analog 
pin 0 
wiper 
V+ 
gnd 
soft-pot
CODE (no need to type everything in just click) 
CIRC-14 
// CIRC-14 Example Code int greenValue = constrain( 
// Will fade an RGB LED from Red-Green-Blue map(sensorValue, 0, 512, 0, 255),0,255)- 
// in relation to the soft pot value constrain( 
NOT WORKING? (3 things to try) 
MAKING IT BETTER 
MORE, MORE, MORE: 
35 
Download the Code from (http://ardx.org/CODE14S) 
(copy the text and paste it into an empty Arduino Sketch) 
Seeing Red 
The 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_LED_PIN, 
redValue); 
to 
analogWrite(RED_LED_PIN, 
redValue/3); 
More details, where to buy more parts, where to ask more questions: 
http://sparkfun.com/ 
Bizarre Results 
The most likely cause of this is 
if you’re pressing the 
potentiometer in more than 
one position. This is normal 
and can actually be used to 
create some neat results. 
LED Remains Dark or 
Shows Incorrect Color 
With 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. 
map(sensorValue, 512, 1023, 0, 255),0,255); 
// LED leads connected to PWM pins //calculate the green value (0-255 
const int RED_LED_PIN = 9; //Red LED Pin //over 0-512 & 255-0 over 512-1023) 
const int GREEN_LED_PIN = 10; //Green LED Pin int blueValue = constrain( 
const int BLUE_LED_PIN = 11; //Blue LED Pin map(sensorValue, 512, 1023, 0, 255),0,255); 
//calculate the blue value 0-255 over 
//512-1023 
void setup() { 
//no need for any code here // Display the requested color 
} analogWrite(RED_LED_PIN, redValue); 
analogWrite(GREEN_LED_PIN, greenValue); 
void loop() { analogWrite(BLUE_LED_PIN, blueValue); 
int sensorValue = analogRead(0); } 
//read the Soft Pot 
int redValue = constrain( 
map(sensorValue, 0, 512, 255, 0),0,255); 
//calculate the red Value (255-0 
//over the range 0-512) 
HSB Color (Hue, Saturation & Brightness) Now when you use the soft pot you’ll notice a much 
Our RGB LED displays colors using RGB color cleaner and fuller fade from red through to violet. 
codes. However this is often not the easiest way Faux Buttons 
of working with colors (as noted by the less than Because of the way the soft pot works it can also be 
reader friendly code above). A much more used to make custom buttons. To do this you define a 
intuitive system is HSB. range of values corresponding to a discrete button. 
Full Details: Use the code snippet below and the debug window to 
http://ardx.org/HSB 
determine desired values. 
To convert from RGB to HSB all that is required is 
some slightly complicated math. For an example if(analogRead(0) > minValue && 
analogRead(0) < maxValue){ program visit: buttonAction() 
http://ardx.org/CODE14MB } 
based on www.kasperkamperman.com's original Then cover the soft pot with a drawn/printed button 
code: http://ardx.org/KASP pattern
NOTE 
36 
notes 
.:Notes:. 
.:Room for a Few Notes:.
NOTE 
37 
notes 
.:Notes:. 
.:Room for a Few Notes:.
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.

More Related Content

PDF
Ardx experimenters-guide-web
PDF
Arduino experimenters guide hq
PPTX
Fun with arduino
PDF
2015-10-21 - Arduino workshop
PDF
Arduino Boot Camp Pitch Slides
PDF
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
PDF
Introduction to Arduino and Circuits
PDF
Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009
Ardx experimenters-guide-web
Arduino experimenters guide hq
Fun with arduino
2015-10-21 - Arduino workshop
Arduino Boot Camp Pitch Slides
Letselectronic.blogspot.com robotic arm based on atmega mcu controlled by win...
Introduction to Arduino and Circuits
Arduino Lecture 4 - Interactive Media CS4062 Semester 2 2009

What's hot (18)

PDF
Getting Started With Arduino_Tutorial
PPT
Client Side Programming with Applet
KEY
Intro to Arduino
PPTX
Microcontroller Programming & Hardware Introduction
PPTX
Applets in java
PPTX
java Unit4 chapter1 applets
PPTX
Arduino slides
PPTX
Introduction to Arduino
PDF
Arduino comic-latest
PPT
Java: Java Applets
PPTX
Arduino as an embedded industrial controller
PDF
Lab2ppt
PPTX
PPTX
Arduino Workshop
PPT
Applet Architecture - Introducing Java Applets
PPTX
PPTX
Arduino Programming
Getting Started With Arduino_Tutorial
Client Side Programming with Applet
Intro to Arduino
Microcontroller Programming & Hardware Introduction
Applets in java
java Unit4 chapter1 applets
Arduino slides
Introduction to Arduino
Arduino comic-latest
Java: Java Applets
Arduino as an embedded industrial controller
Lab2ppt
Arduino Workshop
Applet Architecture - Introducing Java Applets
Arduino Programming
Ad

Similar to arduino (20)

PDF
Ardx eg-spar-web-rev10
PDF
Arduino experimenters guide ARDX
PPTX
Introduction to the Arduino
PPTX
Introduction to Arduino Microcontroller
DOCX
Arduino Full Tutorial
PDF
introductiontoarduino-111120102058-phpapp02.pdf
PPTX
PDF
Arduino comic v0004
PDF
Arduino Comic-Jody Culkin-2011
PDF
Getting startedwitharduino ch04
PPTX
Ch_2_8,9,10.pptx
PDF
Presentation S4A
PDF
Presentation
PPT
Arduino_CSE ece ppt for working and principal of arduino.ppt
PDF
Arduino guide
PPTX
What is Arduino
PPT
Arduino Platform with C programming.
PPT
Intro to Arduino
PPSX
Arduino by yogesh t s'
Ardx eg-spar-web-rev10
Arduino experimenters guide ARDX
Introduction to the Arduino
Introduction to Arduino Microcontroller
Arduino Full Tutorial
introductiontoarduino-111120102058-phpapp02.pdf
Arduino comic v0004
Arduino Comic-Jody Culkin-2011
Getting startedwitharduino ch04
Ch_2_8,9,10.pptx
Presentation S4A
Presentation
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino guide
What is Arduino
Arduino Platform with C programming.
Intro to Arduino
Arduino by yogesh t s'
Ad

Recently uploaded (20)

PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
cuic standard and advanced reporting.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
20250228 LYD VKU AI Blended-Learning.pptx
Spectral efficient network and resource selection model in 5G networks
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The AUB Centre for AI in Media Proposal.docx
MYSQL Presentation for SQL database connectivity
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
Reach Out and Touch Someone: Haptics and Empathic Computing
Programs and apps: productivity, graphics, security and other tools
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Big Data Technologies - Introduction.pptx
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Spectroscopy.pptx food analysis technology
NewMind AI Weekly Chronicles - August'25 Week I
Digital-Transformation-Roadmap-for-Companies.pptx

arduino

  • 2. ABOUT OPEN SOURCE HARDWARE All of the projects at SparkFun and .:oomlout:. are open source. What does this mean? It means everything involved in making this kit, be it this guide, 3D models, or code is available for free download. But it goes further, you're also free to reproduce and modify any of this material, then distribute it for yourself. The catch? Quite simple, it is released under a Creative Commons (By - Share Alike) license. This means you must credit .:oomlout:. in your design and share your developments in a similar manner. Why? We grew up learning and playing with open source software and the experience was good fun, we think it would be lovely if a similar experience was possible with physical things. More details on the Creative Commons CC (By - Share Alike) License can be found at http://ardx.org/CCLI We’re a plucky little design company focusing on producing “delightfully fun open source products” To check out what we are up to http://www.oomlout.com A Few Words ABOUT THIS KIT The overall goal of this kit is fun. Beyond this, the aim is to get you comfortable using a wide range of electronic components through small, simple and easy circuits. The focus is to get each circuit working then giving you the tools to figure out why. If you encounter any problems, want to ask a question, or would like to know more about any part, extra help is only an e-mail away help@oomlout.com. ABOUT .: OOMLOUT :. ABOUT SPARKFUN SparkFun is an energetic young company seeking to make electronics fun, accessible, and approachable to everyone - from kids in elementary school to PhD-toting engineers. http://www.sparkfun.com/ ABOUT PROBLEMS We strive to deliver the highest level of quality in each and every thing we produce. If you ever find an ambiguous instruction, a missing piece, or would just like to ask a question, we’ll try our best to help out. help@oomlout.com (we like hearing about problems it helps us improve future versions) Thanks For Choosing .:oomlout:. and SparkFun
  • 3. TBCN .: WHERE TO FIND EVERYTHING :. table of contents Before We Start {ASEM} Assembling the Pieces 02 {INST} Installing the Software 03 {PROG} A Small Programming Primer 04 {ELEC} A Small Electronics Primer 06 The Circuits {CIRC01} Getting Started - (Blinking LED) 08 {CIRC02} 8 LED Fun - (Multiple LEDs) 10 {CIRC03} Spin Motor Spin - (Transistor and Motor) 12 {CIRC04} A Single Servo - (Servos) 14 {CIRC05} 8 More LEDs - (74HC595 Shift Register) 16 {CIRC06} Music - (Piezo Elements) 18 {CIRC07} Button Pressing - (Pushbuttons) 20 {CIRC08} Twisting - (Potentiometers) 22 {CIRC09} Light - (Photo Resistors) 24 {CIRC10} Temperature - (TMP36 Temperature Sensor) 26 {CIRC11} Larger Loads - (Relays) 28 {CIRC12} Colorful Light - (RGB LED) 30 {CIRC13} Measuring Bends - (Flex Sensor) 32 {CIRC14} Fancy Sensing - (Soft Potentiometer) 34 01
  • 4. 01 ASEM assembling the 02 pieces Breadboard x1 Arduino x1 Arduino Holder x1 .: PUTTING IT TOGETHER :. .: For an introduction to what an Arduino is, visit :. .: http://ardx.org/INTR :.
  • 5. .: INSTALLING THE IDE :. This is the program used to write code for the Arduino. It may seem a little daunting at first but once you have it installed and start playing around, its secrets will reveal themselves. Step 1: Download the software http://arduino.cc/en/Main/Software download the software for your operating system Windows XP Mac OSX Step 2: Unzip the Software Unzip rr(rr- version #) arduino-00 -win.zip Recommended Path c:Program Files Step 3: Shortcut Icon Open rr (rr- version #) c:program filesarduino-00 Right Click Arduino.exe (send to>Desktop (create shortcut)) Step 4: Plug In Your Arduino Plug your Arduino in: Using the included USB cable, plug your Arduino board into a free USB port. Wait for a box to pop up Step 5: Add new Hardware Skip searching the internet (click the next box when prompted to do so) Install from a specific location (click “Install from a list or specific location (Advanced)") Choose the Location Duemilanove Board c:program filesarduino-00 rrdriversFTDI USB Drivers Uno Board c:program filesarduino-00 rrdrivers Finished Vista, Seven Step 5: Add new Hardware Run Device Manager Start > Run > devmgmt.msc Choose the Arduino Other Devices > Arduino Uno (Uno) Update Driver click “Update Driver” Select Driver click “Browse My Computer for Driver Software” c:program filesarduino-00rrdrivers 02 INST installing (software and hardware) Step 2: Open The .dmg Open (mount) arduino-00rr-mac.dmg (rr- version #) Step 3: Copy The Application Go to "Arduino" (in the devices section of finder) Move "Arduino" Application to the "Applications" folder Step 4: Install Drivers .:Duemilanove Boards Only:. Go to "Arduino" device Double Click & Install FTDI Drivers for Intel Macs ( _ _ ).pkg rrrr (FTDI Drivers for PPC Macs ( _ _ ).pkg rrrr Restart Step 5: Plug In Your Arduino Plug your Arduino in: Using the included USB cable, plug your Arduino board into a free USB port. Finished .: NOTE: :. .: Encountering problems? :. .: Would like more details? Using Linux? :. .: http://ardx.org/LINU :. Go to Finished 03
  • 6. ARDUINO PROGRAMMING IN BRIEF // (single line comment) It is often useful to write notes to yourself as you go along about what each line of code does. To do this type two forward slashes and everything until the end of the line will be ignored by your program. { } (curly brackets) Used to define when a block of code starts and ends (used in functions as well as loops). 03 PROG programming 04 primer .:A Small Programming Primer:. The Arduino is programmed in the C language. This is a quick little primer targeted at people who have a little bit of programing experience and just need a briefing on the idiosyncracies of C and the Arduino IDE. If you find the concepts a bit daunting, don't worry, you can start going through the circuits and pick up most of it along the way. For a more in-depth intro, the Arduino.cc website is a great resource. STRUCTURE void setup(){ } All the code between the two curly brackets will be run once when your Arduino program first runs. Each Arduino program (often called a sketch) has two required functions (also called routines). void loop(){ } This function is run after setup has finished. After it has run once it will be run again, and again, until power is removed. SYNTAX ; (semicolon) Each line of code must be ended with a semicolon (a missing semicolon is often the reason for a program refusing to compile). One of the slightly frustrating elements of C is its formatting requirements (this also makes it very powerful). If you remember the following you should be alright. /* */(multi line comment) If you have a lot to say you can span several lines as a comment. Everything between these two symbols will be ignored in your program. A program is nothing more than instructions to move numbers around in an intelligent way. Variables are used to do the moving. long (long) Used when an integer is not large enough. Takes 4 bytes (32 bits) of RAM and has a range between -2,147,483,648 and 2,147,483,647. int (integer) The main workhorse, stores a number in 2 bytes (16 bits). Has no decimal places and will store a value between -32,768 and 32,767. boolean (boolean) A simple True or False variable. Useful because it only uses one bit of RAM. char (character) Stores one character using the ASCII code (ie 'A' = 65). Uses one byte (8 bits) of RAM. The Arduino handles strings as an array of char’s. float (float) Used for floating point math (decimals). Takes 4 bytes (32 bits) of RAM and has a range between -3.4028235E+38 and 3.4028235E+38. VARIABLES
  • 7. 03 PROG programming 05 primer MATH OPERATORS = (assignment) makes something equal to something else (eg. x = 10 * 2 (x now equals 20)) % (modulo) gives the remainder when one number is divided by another (ex. 12 % 10 (gives 2)) + (addition) - (subtraction) * (multiplication) / (division) Operators used for manipulating numbers. (they work like simple math). COMPARISON OPERATORS == (equal to) (eg. 12 == 10 is FALSE or 12 == 12 is TRUE) != (not equal to) (eg. 12 != 10 is TRUE or 12 != 12 is FALSE) < (less than) (eg. 12 < 10 is FALSE or 12 < 12 is FALSE or 12 < 14 is TRUE) > (greater than) (eg. 12 > 10 is TRUE or 12 > 12 is FALSE or 12 > 14 is FALSE) Operators used for logical comparison. CONTROL STRUCTURE if(condition){ } else if( condition ){ } else { } This will execute the code between the curly brackets if the condition is true, and if not it will test the else if condition if that is also false the else code will execute. Programs are reliant on controlling what runs next, here are the basic control elements (there are many more online). for(int i = 0; i < #repeats; i++){ } Used when you would like to repeat a chunk of code a number of times (can count up i++ or down i-- or use any variable) DIGITAL digitalWrite(pin, value); Once a pin is set as an OUTPUT, it can be set either HIGH (pulled to +5 volts) or LOW (pulled to ground). pinMode(pin, mode); Used to set a pin's mode, pin is the pin number you would like to address 0-19 (analog 0- 5 are 14-19). The mode can either be INPUT or OUTPUT. int digitalRead(pin); Once a pin is set as an INPUT you can use this to return whether it is HIGH (pulled to +5 volts) or LOW (pulled to ground). ANALOG int analogWrite(pin, value); Some of the Arduino's pins support pulse width modulation (3, 5, 6, 9, 10, 11). This turns the pin on and off very quickly making it act like an analog output. The value is any number between 0 (0% duty cycle ~0v) and 255 (100% duty cycle ~5 volts). The Arduino is a digital machine but it has the ability to operate in the analog realm (through tricks). Here's how to deal with things that aren't digital. int analogRead(pin); When the analog input pins are set to input you can read their voltage. A value between 0 (for 0 volts) and 1024 (for 5 volts) will be returned. .:For a full programming reference visit:. http://ardx.org/PROG
  • 8. 04 ELEC electronics Diode What it Does: No. of Leads: The electronic equivalent of a one way 2 valve. Allowing current to flow in one Things to watch out for: direction but not the other. - Will only work in one direction (current will Identifying: flow if end with the line is connected to ground) Usually a cylinder with wires extending from More Details: either end. (and an off center line indicating polarity) http://ardx.org/DIOD Hobby Servo What it Does: No. of Leads: Takes a timed pulse and converts it into 3 an angular position of the output shaft. Things to watch out for: Identifying: - The plug is not polarized so make sure A plastic box with 3 wires coming out one it is plugged in the right way. side and a shaft with a plastic horn out More Details: the top. http://ardx.org/SERV DC Motor What it Does: No. of Leads: Spins when a current is passed through it. 2 Identifying: Things to watch out for: This one is easy, it looks like a motor. - Using a transistor or relay that is rated Usually a cylinder with a shaft coming out for the size of motor you're using. of one end. More Details: http://ardx.org/MOTO 06 primer .:A Small Electronics Primer:. ELECTRONICS IN BRIEF No previous electronic experience is required to have fun with this kit. Here are a few details about each component to make identifying, and perhaps understanding them, a bit easier. If at any point you are worried about how a component is used or why it's not working the internet offers a treasure trove of advice, or we can be contacted at help@oomlout.com COMPONENT DETAILS LED (Light Emitting Diode) What it Does: No. of Leads: Emits light when a small current is 2 (one longer, this one connects to positive) passed through it. (only in one direction) Things to watch out for: Identifying: - Will only work in one direction Looks like a mini light bulb. - Requires a current limiting resistor More Details: http://ardx.org/LED Resistors What it Does: No. of Leads: Restricts the amount of current that can 2 flow through a circuit. Things to watch out for: Identifying: - Easy to grab the wrong value (double Cylinder with wires extending from either check the colors before using) end. The value is displayed using a color More Details: coding system (for details see next page) http://ardx.org/RESI Transistor What it Does: No. of Leads: Uses a small current to switch or amplify a 3 (Base, Collector, Emitter) much larger current. Things to watch out for: Identifying: - Plugging in the right way round (also a Comes in many different packages but you current limiting resistor is often needed on the base pin) can read the part number off the package. More Details: (P2N2222AG in this kit and find a datasheet online) http://ardx.org/TRAN
  • 9. 04 ELEC electronics What it Does: No. of Leads: Packages any range of complicated 2 - 100s (in this kit there is one with 3 (TMP36) and electronics inside an easy to use package. one with 16 (74HC595) Identifying: Things to watch out for: The part ID is written on the outside of the - Proper orientation. (look for marks showing pin 1) package. (this sometimes requires a lot of More Details: light or a magnifying glass to read). http://ardx.org/ICIC 07 primer Piezo Element What it Does: A pulse of current will cause it to click. A No. of Leads: stream of pulses will cause it to emit a 2 tone. Things to watch out for: Identifying: - Difficult to misuse. In this kit it comes in a little black barrel, More Details: but sometimes they are just a gold disc. http://ardx.org/PIEZ IC (Integrated Circuit) Pushbutton What it Does: No. of Leads: Completes a circuit when it is pressed. 4 Identifying: Things to watch out for: A little square with leads out the bottom - these are almost square so can be and a button on the top. inserted 90 degrees off angle. Potentiometer More Details: http://ardx.org/BUTT What it Does: No. of Leads: Produces a variable resistance dependant 3 on the angular position of the shaft. Things to watch out for: Identifying: - Accidentally buying logarithmic scale. They can be packaged in many different More Details: form factors, look for a dial to identify. http://ardx.org/POTE Photo Resistor What it Does: No. of Leads: Produces a variable resistance dependant 2 on the amount of incident light. Things to watch out for: Identifying: - Remember it needs to be in a voltage Usually a little disk with a clear top and a divider before it provides a useful input. curvy line underneath. More Details: http://ardx.org/PHOT RESISTOR COLOR CODE LEAD CLIPPING first digit second digit # of zeros tolerance Examples: green-blue-brown - 560 ohms red-red-red - 2 200 ohms (2.2k) brown-black-orange - 10 000 ohms (10k) 0 - Black 5 - Green 20% - none 1 - Brown 6 - Blue 10% - silver 2 - Red 7 - Purple 5% - gold 3 - Orange 8 - Grey 4 - Yellow 9 - White Some components in this kit come with very long wire leads. To make them more compatible with a breadboard a couple of changes are required. LEDs: Clip the leads so the long lead is ~10mm (3/8”) long and the short one is ~7mm (9/32”). Resistors: Bend the leads down so they are 90 degrees to the cylinder. Then snip them so they are ~6mm (1/4”) long. Other Components: Other components may need clipping. Use your discretion when doing so. COMPONENT DETAILS (CONT.)
  • 10. CIRC-01 WHAT WE’RE DOING: THE CIRCUIT: Schematic Arduino pin 13 longer lead LED (light emitting diode) resistor (330ohm) (orange-orange-brown) gnd (ground) (-) 08 .:Getting Started:. .:(Blinking LED):. LEDs (light emitting diodes) are used in all sorts of clever things which is why we have included them in this kit. We will start off with something very simple, turning one on and off, repeatedly, producing a pleasant blinking effect. To get started, grab the parts listed below, pin the layout sheet to your breadboard and then plug everything in. Once the circuit is assembled you'll need to upload the program. To do this plug the Arduino board into your USB port. Then select the proper port in Tools > Serial Port > (the comm port of your Arduino). Next upload the program by going to File > Upload to I/O Board (ctrl+U). Finally, bask in the glory and possibility that controlling lights offers. If you are having trouble uploading, a full trouble shooting guide can be found here: http://ardx.org/TRBL Wire 5mm Yellow LED x1 330 Ohm Resistor Orange-Orange-Brown x1 2 Pin Header x4 CIRC-01 Breadboard Sheet x1 Parts: The Internet .:download:. + breadboard layout sheet http://ardx.org/BBLS01S .:view:. assembly video http://ardx.org/VIDE01
  • 11. CODE (no need to type everything in just click) NOT WORKING? (3 things to try) CIRC-01 09 File > Examples > 1.Basic > Blink (example from the great arduino.cc site, check it out for other ideas) /* Blink * Turns on an LED on for one second, then off for one second, * repeatedly. * Created 1 June 2005 By David Cuartielles * http://arduino.cc/en/Tutorial/Blink * based on an orginal by H. Barragan for the Wiring i/o board */ int ledPin = 13; // LED connected to digital pin 13 // The setup() method runs once, when the sketch starts void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(ledPin, LOW); // set the LED off delay(1000); // wait for a second } LED Not Lighting Up? LEDs will only work in one direction. Try taking it out and More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC01 twisting it 180 degrees. (no need to worry, installing it backwards does no permanent harm). Changing the pin: Control the brightness: The LED is connected to pin 13 but we can use any of Along with digital (on/off) control the Arduino can control the Arduino’s pins. To change it take the wire plugged some pins in an analog (brightness) fashion. (more details on into pin 13 and move it to a pin of your choice (from 0- this in later circuits). To play around with it. 13) (you can also use analog 0-5, analog 0 is 14...) Change the LED to pin 9: (also change the wire) Then in the code change the line: ledPin = 13; -> int ledPin = 9; int ledPin = 13; -> int ledPin = newpin; Replace the code inside the { }'s of loop() with this: Then upload the sketch: (ctrl-u) analogWrite(ledPin, new number); Change the blink time: (new number) = any number between 0 and 255. Unhappy with one second on one second off? 0 = off, 255 = on, in between = different brightness In the code change the lines: Fading: digitalWrite(ledPin, HIGH); We will use another included example program. To open go to delay(time on); //(seconds * 1000) File > Examples > 3.Analog > Fading digitalWrite(ledPin, LOW); delay(time off); //(seconds * 1000) Then upload to your board and watch as the LED fades in and then out. 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. help@oomlout.com Program Not Uploading This happens sometimes, the most likely cause is a confused serial port, you can change this in tools>serial port> MAKING IT BETTER MORE, MORE, MORE:
  • 12. Wire CIRC-02 WHAT WE’RE DOING: THE CIRCUIT: Parts: Schematic pin 2 pin 3 pin 4 pin 5 pin 6 pin 7 pin 8 pin 9 10 .:8 LED Fun:. .:Multiple LEDs:. We have caused one LED to blink, now it's time to up the stakes. Lets connect eight. We'll also have an opportunity to stretch the Arduino a bit by creating various lighting sequences. This circuit is also a nice setup to experiment with writing your own programs and getting a feel for how the Arduino works. Along with controlling the LEDs we start looking into a few simple programming methods to keep your programs small. for() loops - used when you want to run a piece of code several times. arrays[] - used to make managing variables easier (it's a group of variables). 5mm Yellow LED x8 330 Ohm Resistor Orange-Orange-Brown x8 2 Pin Header x4 CIRC-02 Breadboard Sheet x1 gnd gnd The Internet .:download:. LED resistor 330ohm LED resistor 330ohm breadboard layout sheet http://ardx.org/BBLS02S .:view:. assembly video http://ardx.org/VIDE02
  • 13. CODE (no need to type everything in just click) NOT WORKING? (3 things to try) 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. MAKING IT BETTER Switching to loops: Extra animations: In the loop() function there are 4 lines. The last Tired of this animation? Then try the other two three all start with a '//'. This means the line is sample animations. Uncomment their lines and upload treated as a comment (not run). To switch the the program to your board and enjoy the new light program to use loops change the void loop() animations. (delete the slashes in front of row 3 and then 4) code to: Testing out your own animations: //oneAfterAnotherNoLoop(); oneAfterAnotherLoop(); Jump into the included code and start changing //oneOnAtATime(); things. The main point is to turn an LED on use //inAndOut(); Upload the program, and notice that nothing has changed. You can take a look at the two it off use digitalWrite(pinNumber, LOW); . functions, each does the same thing, but use Type away, regardless of what you change you won't different approaches (hint: the second one uses break anything. a for loop). MORE, MORE, MORE: Operating out of sequence With eight wires it's easy to cross a couple. Double check that the first LED is plugged into pin 2 and each pin there after. CIRC-02 digitalWrite(pinNumber, HIGH); then to turn 11 Download the Code from ( http://ardx.org/CODE02 ) (and then copy the text and paste it into an empty Arduino Sketch) More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC02 Starting Afresh Its 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. //LED Pin Variables * will then turn them off int ledPins[] = {2,3,4,5,6,7,8,9}; //An array to hold the void oneAfterAnotherNoLoop(){ //pin each LED is connected to int delayTime = 100; //i.e. LED #0 is connected to pin 2 / / t h e t i m e ( i n m i l l i s e c o n d s ) t o p a u s e //between LEDs void setup() digitalWrite(ledPins[0], HIGH); //Turns on LED #0 { //(connected to pin 2) for(int i = 0; i < 8; i++){ delay(delayTime); //waits delayTime milliseconds //this is a loop and will repeat eight times ... pinMode(ledPins[i],OUTPUT); ... //we use this to set LED pins to output digitalWrite(ledPins[7], HIGH); //Turns on LED #7 } //(connected to pin 9) } delay(delayTime); //waits delayTime milliseconds //Turns Each LED Off void loop() // run over and over again digitalWrite(ledPins[7], LOW); //Turns off LED #7 { delay(delayTime); //waits delayTime milliseconds oneAfterAnotherNoLoop(); ... //this will turn on each LED one by //one then turn each oneoff -----more code in the downloadable version------ //oneAfterAnotherLoop(); //this does the same as onAfterAnotherNoLoop //but with much less typing //oneOnAtATime(); //inAndOut(); } /* * oneAfterAnotherNoLoop() - Will light one then * delay for delayTime then light the next LED it
  • 14. CIRC-03 WHAT WE’RE DOING: THE CIRCUIT: CIRC-03 Breadboard Sheet x1 Transistor P2N2222AG Parts: Collector Emitter 12 .:Spin Motor Spin:. .:Transistor & Motor:. The Arduino's pins are great for directly controlling small electric items like LEDs. However, when dealing with larger items (like a toy motor or washing machine), an external transistor is required. A transistor is incredibly useful. It switches a lot of current using a much smaller current. A transistor has 3 pins. For a negative type (NPN) transistor, you connect your load to collector and the emitter to ground. Then when a small current flows from base to the emitter, a current will flow through the transistor and your motor will spin (this happens when we set our Arduino pin HIGH). There are literally thousands of different types of transistors, allowing every situation to be perfectly matched. We have chosen a P2N2222AG a rather common general purpose transistor. The important factors in our case are that its maximum voltage (40v) and its maximum current (200 milliamp) are both high enough for our toy motor (full details can be found on its datasheet http://ardx.org/2222). (The 1N4001 diode is acting as a flyback diode for details on why its there visit: http://ardx.org/4001) Wire Transistor P2N2222AG (TO92) x1 10k Ohm Resistor Brown-Black-Orange x1 2 Pin Header x4 Toy Motor x1 Arduino pin 9 resistor (10kohm) gnd (ground) (-) Base Motor +5 volts The transistor will have P2N2222AG printed on it (some variations will have different pin assignments!) Diode (1N4001) x1 Diode .:download:. breadboard layout sheet http://ardx.org/BBLS03S .:view:. assembly video http://ardx.org/VIDE03 .:NOTE: if your arduino is resetting you need to install the optional capacitor:. Schematic The Internet
  • 15. CODE (no need to type everything in just click) Download the Code from ( http://ardx.org/CODE03 ) (then simply copy the text and paste it into an empty Arduino Sketch) int motorPin = 9; //pin the motor is connected to void setup() //runs once void motorOnThenOffWithSpeed(){ { int onSpeed = 200;// a number between pinMode(motorPin, OUTPUT); //0 (stopped) and 255 (full speed) } int onTime = 2500; int offSpeed = 50;// a number between void loop() // run over and over again //0 (stopped) and 255 (full speed) { int offTime = 1000; motorOnThenOff(); analogWrite(motorPin, onSpeed); //motorOnThenOffWithSpeed(); // turns the motor On //motorAcceleration(); delay(onTime); // waits for onTime milliseconds } analogWrite(motorPin, offSpeed); // turns the motor Off /* delay(offTime); // waits for offTime milliseconds * motorOnThenOff() - turns motor on then off } * (notice this code is identical to the code we used for void motorAcceleration(){ * the blinking LED) int delayTime = 50; //time between each speed step */ for(int i = 0; i < 256; i++){ void motorOnThenOff(){ //goes through each speed from 0 to 255 int onTime = 2500; //on time analogWrite(motorPin, i); //sets the new speed int offTime = 1000; //off time delay(delayTime);// waits for delayTime milliseconds digitalWrite(motorPin, HIGH); } // turns the motor On for(int i = 255; i >= 0; i--){ delay(onTime); // waits for onTime milliseconds //goes through each speed from 255 to 0 digitalWrite(motorPin, LOW); analogWrite(motorPin, i); //sets the new speed // turns the motor Off delay(delayTime);//waits for delayTime milliseconds delay(offTime);// waits for offTime milliseconds } } } Still No Luck? NOT WORKING? (3 things to try) If you sourced your own motor, double check that it will work with 5 volts and that it does not draw too much power. CIRC-03 Still Not Working? Sometimes the Arduino board computer. Try un-plugging and then re-plugging it into your 13 Motor Not Spinning? If you sourced your own transistor, double check with the data sheet that the pinout will disconnect from the USB port. MAKING IT BETTER More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC03 is compatible with a P2N2222AG (many are reversed). Controlling speed: In the loop() section change it to this We played with the Arduino's ability to control the // motorOnThenOff(); brightness of an LED earlier now we will use the same motorOnThenOffWithSpeed(); feature to control the speed of our motor. The Arduino // motorAcceleration(); Then upload the program. You can change the speeds by does this using something called Pulse Width changing the variables onSpeed and offSpeed. Modulation (PWM). This relies on the Arduino's ability to operate really, really fast. Rather than directly Accelerating and decelerating: controlling the voltage coming from the pin the Arduino Why stop at two speeds, why not accelerate and decelerate will switch the pin on and off very quickly. In the the motor. To do this simply change the loop() code to read computer world this is going from 0 to 5 volts many // motorOnThenOff(); times a second, but in the human world we see it as a // motorOnThenOffWithSpeed(); voltage. For example if the Arduino is PWM'ing at 50% motorAcceleration(); we see the light dimmed 50% because our eyes are not Then upload the program and watch as your motor slowly quick enough to see it flashing on and off. The same accelerates up to full speed then slows down again. If you feature works with transistors. Don't believe me? Try it would like to change the speed of acceleration change the out. variable delayTime (larger means a longer acceleration time). MORE, MORE, MORE:
  • 16. CIRC-04 WHAT WE’RE DOING: THE CIRCUIT: Parts: CIRC-04 Breadboard Sheet x1 Schematic signal (white) gnd (black) The Internet 14 .:A Single Servo:. .:Servos:. Spinning a motor is good fun but when it comes to projects where motion control is required they tend to leave us wanting more. The answer? Hobby servos. They are mass produced, widely available and cost anything from a couple of dollars to hundreds. Inside is a small gearbox (to make the movement more powerful) and some electronics (to make it easier to control). A standard servo is positionable from 0 to 180 degrees. Positioning is controlled through a timed pulse, between 1.25 milliseconds (0 degrees) and 1.75 milliseconds (180 degrees) (1.5 milliseconds for 90 degrees). Timing varies between manufacturer. If the pulse is sent every 25-50 milliseconds the servo will run smoothly. One of the great features of the Arduino is it has a software library that allows you to control two servos (connected to pin 9 or 10) using a single line of code. Wire 3 Pin Header x1 Mini Servo x1 2 Pin Header x4 Arduino pin 9 gnd (ground) (-) +5v (red) Mini Servo +5 volts (5V) .:download:. breadboard layout sheet http://ardx.org/BBLS04S .:view:. assembly video http://ardx.org/VIDE04
  • 17. CODE (no need to type everything in just click) // Sweep // by BARRAGAN <http://barraganstudio.com> #include <Servo.h> Servo myservo; // create servo object to control a servo int pos = 0; // variable to store the servo position myservo.attach(9); // attaches the servo on pin 9 to the servo object Still Not Working A mistake we made a time or two was simply forgetting to connect the power (red and brown wires) to +5 volts and ground. CIRC-04 15 File > Examples > Servo > Sweep (example from the great arduino.cc site, check it out for other great ideas) void setup() { Servo Not Twisting? Even with colored wires it is still shockingly easy to plug a servo in backwards. This might More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC04 be the case. Potentiometer control: void loop() { We have yet to experiment with inputs but if you would like int pulseTime = 2100; //(the number of microseconds to read ahead, there is an example program File > Servo > //to pause for (1500 90 degrees // 900 0 degrees 2100 180 degrees) Knob. This uses a potentiometer (CIRC08) to control the digitalWrite(servoPin, HIGH); delayMicroseconds(pulseTime); servo. You can find instructions online here: digitalWrite(servoPin, LOW); delay(25); http://ardx.org/KNOB } Self timing: Great ideas: While it is easy to control a servo using the Arduino's included Servos can be used to do all sorts of great things, here are a few of library sometimes it is fun to figure out how to program our favorites. something yourself. Try it. We're controlling the pulse directly Xmas Hit Counter so you could use this method to control servos on any of the http://ardx.org/XMAS Arduino's 20 available pins (you need to highly optimize this code before doing that). Open Source Robotic Arm (uses a servo controller as well as the Arduino) http://ardx.org/RARM int servoPin = 9; void setup(){ Servo Walker pinMode(servoPin,OUTPUT); } http://ardx.org/SEWA } void loop() { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } Fits and Starts If the servo begins moving then twitches, and there's a flashing light on your Arduino board, the power supply you are using is not quite up to the challenge. Using a fresh battery instead of USB should solve this problem. NOT WORKING? (3 things to try) MAKING IT BETTER MORE, MORE, MORE:
  • 18. There is a half moon cutout, this goes at the top CIRC-05 .:8 More LEDs:. WHAT WE’RE DOING: THE CIRCUIT: Parts: Schematic +5 volts 74HC595 +5V The Internet 16 .:74HC595 Shift Register:. Time to start playing with chips, or integrated circuits (ICs) as they like to be called. The external packaging of a chip can be very deceptive. For example, the chip on the Arduino board (a microcontroller) and the one we will use in this circuit (a shift register) look very similar but are in fact rather different. The price of the ATMega chip on the Arduino board is a few dollars while the 74HC595 is a couple dozen cents. It's a good introductory chip, and once you're comfortable playing around with it and its datasheet (available online http://ardx.org/74HC595 ) the world of chips will be your oyster. The shift register (also called a serial to parallel converter), will give you an additional 8 outputs (to control LEDs and the like) using only three Arduino pins. They can also be linked together to give you a nearly unlimited number of outputs using the same four pins. To use it you “clock in” the data and then lock it in (latch it). To do this you set the data pin to either HIGH or LOW, pulse the clock, then set the data pin again and pulse the clock repeating until you have shifted out 8 bits of data. Then you pulse the latch and the 8 bits are transferred to the shift registers pins. It sounds complicated but is really simple once you get the hang of it. (for a more in depth look at how a shift register works visit: http://ardx.org/SHIF) Wire Shift Register 74HC595 x1 2 Pin Header x4 330 Ohm Resistor Orange-Orange-Brown x8 CIRC-05 Breadboard Sheet x1 Red LED x8 LED resistor (330ohm) gnd (ground) (-) pin 4 pin 3 pin 2 0 1 2 3 4 5 6 7 data clock latch gnd .:download:. breadboard layout sheet http://ardx.org/BBLS05S .:view:. assembly video http://ardx.org/VIDE05
  • 19. CODE (no need to type everything in just click) Not Quite Working Sorry to sound like a broken record but it is probably something as simple as a crossed wire. CIRC-05 17 The Arduino’s power More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC05 LED goes out This happened to us a couple of times, it happens when the chip is inserted backwards. If you fix it quickly nothing will break. Doing it the hard way: //between LED updates An Arduino makes rather complex actions very easy, shifting out data is for(int i = 0; i < 8; i++){ changeLED(i,ON); one of these cases. However one of the nice features of an Arduino is delay(delayTime); you can make things as easy or difficult as you like. Let's try an } for(int i = 0; i < 8; i++){ example of this. In your loop switch the line: changeLED(i,OFF); updateLEDs(i) -> updateLEDsLong(i); delay(delayTime); Upload the program and notice nothing has changed. If you look at the } Uploading this will cause the lights to light up one after another and then off code you can see how we are communicating with the chip one bit at a in a similar manner. Check the code and wikipedia to see how it works, or time. (for more details http://ardx.org/SPI ). shoot us an e-mail if you have questions. Controlling individual LEDs: Time to start controlling the LEDs in a similar method as we did in More animations: Now things get more interesting. If you look back to the code from CIRC02 (8 CIRC02. As the eight LED states are stored in one byte (an 8 bit value) LED Fun) you see we change the LEDs using digitalWrite(led, state), this is for details on how this works try http://ardx.org/BINA. An Arduino is the same format as the routine we wrote changeLED(led, state). You can use very good at manipulating bits and there are an entire set of operators the animations you wrote for CIRC02 by copying the code into this sketch and that help us out. Details on bitwise maths ( http://ardx.org/BITW ). changing all the digitalWrite()'s to changeLED()'s. Powerful? Very. (you'll also Our implementation. need to change a few other things but follow the compile errors and it works Replace the loop() code with itself out). int delayTime = 100; //the number of milliseconds //to delay Frustration? Shoot 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. help@oomlout.com Download the Code from ( http://ardx.org/CODE05 ) (copy the text and paste it into an empty Arduino Sketch) //Pin Definitions //The 74HC595 uses a serial communication //link which has three pins int data = 2; digitalWrite(latch, LOW); int clock = 3; int latch = 4; //Pulls the chips latch low shiftOut(data, clock, MSBFIRST, value); void setup() //runs once //Shifts out 8 bits to the shift register { pinMode(data, OUTPUT); pinMode(clock, OUTPUT); digitalWrite(latch, HIGH); pinMode(latch, OUTPUT); } //Pulls the latch high displaying the data } void loop() // run over and over again { ---------- More Code Online ---------- int delayTime = 100; //delay between LED updates for(int i = 0; i < 256; i++){ updateLEDs(i); delay(delayTime); } } /* * updateLEDs() - sends the LED states set * in value to the 74HC595 sequence */ void updateLEDs(int value){ NOT WORKING? (3 things to try) MAKING IT BETTER MORE, MORE, MORE:
  • 20. CIRC-06 WHAT WE’RE DOING: THE CIRCUIT: Parts: Schematic Arduino pin 9 Piezo Element gnd (ground) (-) The Internet 18 .:Music:. .:Piezo Elements:. To this point we have controlled light, motion, and electrons. Let's tackle sound next. But sound is an analog phenomena, how will our digital Arduino cope? We will once again rely on its incredible speed which will let it mimic analog behavior. To do this, we will attach a piezo element to one of the Arduino's digital pins. A piezo element makes a clicking sound each time it is pulsed with current. If we pulse it at the right frequency (for example 440 times a second to make the note middle A) these clicks will run together to produce notes. Let's get to experimenting with it and get your Arduino playing "Twinkle Twinkle Little Star". Wire Piezo Element x1 2 Pin Header x4 CIRC-06 Breadboard Sheet x1 .:download:. breadboard layout sheet http://ardx.org/BBLS06S .:view:. assembly video http://ardx.org/VIDE06
  • 21. CODE (no need to type everything in just click) Download the Code from ( http://ardx.org/CODE06 ) (copy the text and paste it into an empty Arduino Sketch) /* Melody * (cleft) 2005 D. Cuartielles for K3 * digitalWrite(speakerPin, * This example uses a piezo speaker to play melodies. It sends LOW); * a square wave of the appropriate frequency to the piezo, delayMicroseconds(tone); * generating the corresponding tone. } * } * The calculation of the tones is made following the * mathematical operation: void playNote(char note, int duration) { * char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; * timeHigh = period / 2 = 1 / (2 * toneFrequency) int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 * * where the different tones are described as in the table: }; * // play the tone corresponding to the note name * note frequency period timeHigh for (int i = 0; i < 8; i++) { * c 261 Hz 3830 1915 if (names[i] == note) { * d 294 Hz 3400 1700 playTone(tones[i], duration); * e 329 Hz 3038 1519 } * f 349 Hz 2864 1432 } * g 392 Hz 2550 1275 } * a 440 Hz 2272 1136 * b 493 Hz 2028 1014 void setup() { * C 523 Hz 1912 956 pinMode(speakerPin, OUTPUT); * } * http://www.arduino.cc/en/Tutorial/Melody */ void loop() { int speakerPin = 9; if (notes[i] == ' ') { int length = 15; // the number of notes delay(beats[i] * tempo); // rest char notes[] = "ccggaagffeeddc "; // a space represents a rest } else { int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; playNote(notes[i], beats[i] * tempo); int tempo = 300; } void playTone(int tone, int duration) { delay(tempo / 2); } NOT WORKING? (3 things to try) MAKING IT BETTER MORE, MORE, MORE: CIRC-06 for (int i = 0; i < length; i++) { // pause between notes Tired of Twinkle Twinkle The code is written so you can easily add your own songs, check out the code below to 19 Can't Think While the Melody is Playing? Just pull up the piezo element whilst you think, upload your program then plug it back in. Little Star? get started. More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC06 No Sound Given the size and shape of the piezo element it is easy to miss the right holes on the breadboard. Try double checking its placement. Playing with the speed: char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', The timing for each note is calculated based on 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, variables, as such we can tweak the sound of each note 1014, 956 }; or the timing. To change the speed of the melody you Composing your own melodies: need to change only one line. The program is pre-set to play 'Twinkle Twinkle Little Star' int tempo = 300; ---> int tempo = (new #) however the way it is programmed makes changing the song Change it to a larger number to slow the melody down, easy. Each song is defined in one int and two arrays, the int or a smaller number to speed it up. Tuning the notes: length defines the number of notes, the first array If you are worried about the notes being a little out of notes[] defines each note, and the second beats[] tune this can be fixed as well. The notes have been defines how long each note is played. Some Examples: calculated based on a formula in the comment block at Twinkle Twinkle Little Star int length = 15; the top of the program. But to tune individual notes just char notes[] = {"ccggaagffeeddc "}; int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, adjust their values in the tones[] array up or down 1, 1, 2, 4 }; until they sound right. (each note is matched by its Happy Birthday (first line) int length = 13; name in the names[] (array ie. c = 1915 ) char notes[] = {"ccdcfeccdcgf "}; int beats[] = {1,1,1,1,1,2,1,1,1,1,1,2,4}; for (long i = 0; i < duration * 1000L; i += tone * 2) { } digitalWrite(speakerPin, HIGH); delayMicroseconds(tone);
  • 22. CIRC-07 WHAT WE’RE DOING: THE CIRCUIT: Parts: Schematic The Internet 20 .:Button Pressing:. .:Pushbuttons:. Up to this point we have focused entirely on outputs, time to get our Arduino to listen, watch and feel. We'll start with a simple pushbutton. Wiring up the pushbutton is simple. There is one component, the pull up resistor, that might seem out of place. This is included because an Arduino doesn't sense the same way we do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or LOW. The button is set up to pull the Arduino's pin LOW when it is pressed, however, when the button is unpressed the voltage of the pin will float (causing occasional errors). To get the Arduino to reliably read the pin as HIGH when the button is unpressed, we add the pull up resistor. (note: the first example program uses only one of the two buttons) Wire Pushbutton x2 2 Pin Header x4 330 Ohm Resistor Orange-Orange-Brown x1 CIRC-07 Breadboard Sheet x1 Red LED x1 10k Ohm Resistor Brown-Black-Orange x2 Arduino pin 13 LED resistor (330 ohm) gnd (ground) (-) Arduino pin 2 pin 3 +5 volts resistor (pull-up) (10k ohm) pushbutton .:download:. breadboard layout sheet http://ardx.org/BBLS07S .:view:. assembly video http://ardx.org/VIDE07
  • 23. CODE (no need to type everything in just click) NOT WORKING? (3 things to try) Light Not Turning On The pushbutton is square and because of this it is easy to put it in the wrong way. Give it a 90 degree twist and MAKING IT BETTER MORE, MORE, MORE: Light Not Fading A bit of a silly mistake we constantly made, when you switch from simple on off to fading remember to move the LED wire from pin 13 to pin 9. CIRC-07 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. 21 File > Examples > 2.Digital > Button (example from the great arduino.cc site, check it out for other great ideas) /* * Button * by DojoDave <http://www.0j0.org> * * Turns on and off a light emitting diode(LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. * http://www.arduino.cc/en/Tutorial/Button */ int ledPin = 13; // choose the pin for the LED int inputPin = 2; // choose the input pin (for a pushbutton) int val = 0; // variable for reading the pin status void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare pushbutton as input } void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, LOW); // turn LED OFF } else { digitalWrite(ledPin, HIGH); // turn LED ON } } Underwhelmed? More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC07 see if it starts working. On button off button: Fading up and down: The initial example may be a little underwhelming (ie. I Lets use the buttons to control an analog signal. To do this don't really need an Arduino to do this), let’s make it a you will need to change the wire connecting the LED from pin little more complicated. One button will turn the LED on 13 to pin 9, also change this in code. the other will turn the LED off. Change the code to: int ledPin = 13; ----> int ledPin = 9; int ledPin = 13; // choose the pin for the LED Next change the loop() code to read. int inputPin1 = 3; // button 1 int value = 0; int inputPin2 = 2; // button 2 void loop(){ if (digitalRead(inputPin1) == LOW) { value--; } void setup() { else if (digitalRead(inputPin2) == LOW) { value++; } pinMode(ledPin, OUTPUT); // declare LED as output value = constrain(value, 0, 255); pinMode(inputPin1, INPUT); // make button 1 an input analogWrite(ledPin, value); pinMode(inputPin2, INPUT); // make button 2 an input delay(10); } } void loop(){ if (digitalRead(inputPin1) == LOW) { Changing fade speed: digitalWrite(ledPin, LOW); // turn LED OFF If you would like the LED to fade faster or slower, there is only } else if (digitalRead(inputPin2) == LOW) { digitalWrite(ledPin, HIGH); // turn LED ON one line of code that needs changing; } } delay(10); ----> delay(new #); Upload the program to your board, and start toggling the To fade faster make the number smaller, slower requires a LED on and off. larger number.
  • 24. CIRC-08 WHAT WE’RE DOING: THE CIRCUIT: Schematic Potentiometer Parts: The Internet .:Twisting:. .:Potentiometers:. Along with the digital pins, the Arduino also has 6 pins which can be used for analog input. These inputs take a voltage (from 0 to 5 volts) and convert it to a digital number between 0 (0 volts) and 1024 (5 volts) (10 bits of resolution). A very useful device that exploits these inputs is a potentiometer (also called a variable resistor). When it is connected with 5 volts across its outer pins the middle pin will read some value between 0 and 5 volts dependent on the angle to which it is turned (ie. 2.5 volts in the middle). We can then use the returned values as a variable in our program. Potentiometer Wire 10k ohm x1 2 Pin Header x4 330 Ohm Resistor Orange-Orange-Brown x1 CIRC-08 Breadboard Sheet x1 Yellow LED x1 Arduino pin 13 LED (light emitting diode) resistor (330ohm) (orange-orange-brown) gnd (ground) (-) +5 volts Arduino analog pin 0 .:download:. breadboard layout sheet http://ardx.org/BBLS08S .:view:. assembly video http://ardx.org/VIDE08 22
  • 25. CIRC-08 CODE (no need to type everything in just click) File > Examples > 3.Analog > AnalogInput (example from the great arduino.cc site, check it out for other great ideas) /* Analog Input * Demonstrates analog input by reading an analog sensor on analog * pin 0 and turning on and off a light emitting diode(LED) connected to digital pin 13. * The amount of time the LED will be on and off depends on the value obtained by * analogRead(). * Created by David Cuartielles * Modified 16 Jun 2009 * By Tom Igoe * http://arduino.cc/en/Tutorial/AnalogInput */ int sensorPin = 0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor void setup() { pinMode(ledPin, OUTPUT); //declare the ledPin as an OUTPUT: Not Working Make 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) } void loop() { sensorValue = analogRead(sensorPin);// read the value from the sensor: digitalWrite(ledPin, HIGH); // turn the ledPin on delay(sensorValue); // stop the program for <sensorValue> milliseconds: digitalWrite(ledPin, LOW); // turn the ledPin off: delay(sensorValue); // stop the program for for <sensorValue> milliseconds: } Sporadically Working This is most likely due to a slightly dodgy connection with the potentiometer's pins. This can usually be conquered by taping the potentiometer down. Threshold switching: Then change the loop code to. Sometimes you will want to switch an output when a value void loop() { int value = analogRead(potPin) / 4; exceeds a certain threshold. To do this with a analogWrite(ledPin, value); potentiometer change the } loop() code to. Upload the code and watch as your LED fades in relation to void loop() { int threshold = 512; your potentiometer spinning. (Note: the reason we divide the if(analogRead(sensorPin) > threshold){ value by 4 is the analogRead() function returns a value from 0 digitalWrite(ledPin, HIGH);} else{ digitalWrite(ledPin, LOW);} to 1024 (10 bits), and analogWrite() takes a value from 0 to } 255 (8 bits) ) This will cause the LED to turn on when the value is above Controlling a servo: 512 (about halfway), you can adjust the sensitivity by This is a really neat example and brings a couple of circuits changing the threshold value. together. Wire up the servo like you did in CIRC-04, then open Fading: the example program Knob (File > Examples > Servo > Let’s control the brightness of an LED directly from the Knob ), then change one line of code. potentiometer. To do this we need to first change the pin int potpin = 0; ----> int potpin = 2; the LED is connected to. Move the wire from pin 13 to pin Upload to your Arduino and then watch as the servo shaft turns 9 and change one line in the code. as you turn the potentiometer. int ledPin = 13; ----> int ledPin = 9; More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC08 Still Backward You can try operating the circuit upside down. Sometimes this helps. 23 NOT WORKING? (3 things to try) MAKING IT BETTER MORE, MORE, MORE:
  • 26. CIRC-09 WHAT WE’RE DOING: THE CIRCUIT: 24 .:Light:. .:Photo Resistors:. Whilst getting input from a potentiometer can be useful for human controlled experiments, what do we use when we want an environmentally controlled experiment? We use exactly the same principles but instead of a potentiometer (twist based resistance) we use a photo resistor (light based resistance). The Arduino cannot directly sense resistance (it senses voltage) so we set up a voltage divider (http://ardx.org/VODI). The exact voltage at the sensing pin is calculable, but for our purposes (just sensing relative light) we can experiment with the values and see what works for us. A low value will occur when the sensor is well lit while a high value will occur when it is in darkness. Wire Photo-Resistor x1 2 Pin Header x4 330 Ohm Resistor Orange-Orange-Brown x1 CIRC-09 Breadboard Sheet x1 Yellow LED x1 10k Ohm Resistor Brown-Black-Orange x1 Parts: Schematic Arduino pin 13 LED resistor (330ohm) gnd (ground) (-) +5 volts photo resistor Arduino analog pin 0 resistor (10k ohm) The Internet .:download:. breadboard layout sheet http://ardx.org/BBLS09S .:view:. assembly video http://ardx.org/VIDE09
  • 27. CODE (no need to type everything in just click) Download the Code from ( http://ardx.org/CODE09 ) (copy the text and paste it into an empty Arduino Sketch) CIRC-09 /* * A simple programme that will change the //output * intensity of an LED based on the amount of } * light incident on the photo resistor. /* * * loop() - this function will start after setup */ * finishes and then repeat //PhotoResistor Pin void loop() int lightPin = 0; //the analog pin the { //photoresistor is int lightLevel = analogRead(lightPin); //Read the //connected to // lightlevel //the photoresistor is not lightLevel = map(lightLevel, 0, 900, 0, 255); //calibrated to any units so //adjust the value 0 to 900 to 0 to 255 //this is simply a raw sensor lightLevel = constrain(lightLevel, 0, 255); //value (relative light) //make sure the value is betwween 0 and 255 //LED Pin analogWrite(ledPin, lightLevel); //write the value int ledPin = 9;//the pin the LED is connected to } NOT WORKING? (3 things to try) LED Remains Dark This is a mistake we continue to make time and time again, if only they could make an LED that worked both ways. Pull it MAKING IT BETTER MORE, MORE, MORE: Still not quite working? You may be in a room which is either too bright or dark. Try turning the lights on or off to see if this helps. Or if you have a flashlight near by give that a try. */ It Isn't Responding to Changes in Light. Given that the spacing of the wires on the photo-resistor is not standard, it is easy to misplace it. Double check its in the right place. More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC09 up and give it a twist. Reverse the response: Light controlled servo: Perhaps you would like the opposite response. Don't Let's use our newly found light sensing skills to control a worry we can easily reverse this response just change: servo (and at the same time engage in a little bit of Arduino analogWrite(ledPin, lightLevel); ----> code hacking). Wire up a servo connected to pin 9 (like in analogWrite(ledPin, 255 - lightLevel); CIRC-04). Then open the Knob example program (the same Upload and watch the response change: one we used in CIRC-08) File > Examples > Servo > Night light: Knob. Upload the code to your board and watch as it works Rather than controlling the brightness of the LED in unmodified. response to light, let's instead turn it on or off based on Using the full range of your servo: You'll notice that the servo will only operate over a limited a threshold value. Change the loop() code with. void loop(){ portion of its range. This is because with the voltage dividing int threshold = 300; circuit we use the voltage on analog pin 0 will not range from if(analogRead(lightPin) > threshold){ digitalWrite(ledPin, HIGH); 0 to 5 volts but instead between two lesser values (these }else{ digitalWrite(ledPin, LOW); values will change based on your setup). To fix this play with } the val = map(val, 0, 1023, 0, 179); line. For hints on what to } do visit http://arduino.cc/en/Reference/Map . //we are controlling brightness so //we use one of the PWM (pulse //width modulation pins) void setup() { pinMode(ledPin, OUTPUT); //sets the led pin to 25
  • 28. CIRC-10 .:Temperature:. WHAT WE’RE DOING: THE CIRCUIT: Parts: 26 .:TMP36 Precision Temperature Sensor:. What's the next phenomena we will measure with our Arduino? Temperature. To do this we'll use a rather complicated IC (integrated circuit) hidden in a package identical to our P2N2222AG transistors. It has three pin's, ground, signal and +5 volts, and is easy to use. It outputs 10 millivolts per degree centigrade on the signal pin (to allow measuring temperatures below freezing there is a 500 mV offset eg. 25 ° C = 750 mV, 0 ° C = 500mV ). To convert this from the digital value to degrees, we will use some of the Arduino's math abilities. Then to display it we'll use one of the IDE's rather powerful features, the debug window. We'll output the value over a serial connection to display on the screen. Let's get to it. One extra note, this circuit uses the Arduino IDE's serial monitor. To open this, first upload the program then click the button which looks like a square with an antennae. The TMP36 Datasheet: http://ardx.org/TMP36 Wire TMP36 Temperature Sensor x1 2 Pin Header x4 CIRC-10 Breadboard Sheet x1 Schematic +5 volts TMP36 (precision temperature sensor) +5v signal the chip will have gnd (ground) (-) Arduino analog pin 0 gnd TMP36 printed on it The Internet .:download:. breadboard layout sheet http://ardx.org/BBLS10S .:view:. assembly video http://ardx.org/VIDE10
  • 29. CODE (no need to type everything in just click) Download the Code from ( http://ardx.org/CODE10 ) (copy the text and paste it into an empty Arduino Sketch) /* --------------------------------------------- void loop() * | Arduino Experimentation Kit Example Code | // run over and over again * | CIRC-10 .: Temperature :. | { * --------------------------------------------- float temperature = getVoltage(temperaturePin); * //getting the voltage reading from the * A simple program to output the current temperature //temperature sensor * to the IDE's debug window * For more details on this circuit: temperature = (temperature - .5) * 100;//converting from 10 //TMP36 Pin Variables mv int temperaturePin = 0;//the analog pin the TMP36's //per degree wit 500 mV offset to //Vout pin is connected to //degrees ((volatge - 500mV) times //the resolution is //10 mV / degree centigrade 1 0 0 ) //(500 mV offset) to make Serial.println(temperature); //printing the result //negative temperatures an delay(1000); //waiting a second Gibberish is Displayed This happens because the serial monitor is receiving data at a different speed than expected. To fix this, click the pull-down box that reads "*** baud" and change it to "9600 baud". CIRC-10 Temperature Value is Serial.println(" degrees centigrade"); voltage we simply display the result of getVoltage(). The change to the first line means when we next output it delete the line temperature = (temperature - .5) * 100; w i ll a ppear on the same line, then we add the informative Outputting degrees Fahrenheit: text and a new line. Again this is a simple change requiring only math. To Changing the serial speed: go degrees C ----> degrees F we use the formula: If you ever wish to output a lot of data over the serial line ( F = C * 1.8) + 32 ) time is of the essence. We are currently transmitting at 9600 add the line baud but much faster speeds are possible. To change this temperature = before Serial.println(temperature); Serial.begin(9600); ----> Serial.begin(115200); Upload the sketch turn on the serial monitor, then change More informative output: the speed from 9600 baud to 115200 baud in the pull down Let's add a message to the serial output to make what 27 Nothing Seems to Happen This 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) Outputting voltage: do this first revert to the original code then change: This is a simple matter of changing one line. Our Serial.println(temperature); ----> sensor outputs 10mv per degree centigrade so to get Serial.print(temperature); (((temperature - .5) * 100)*1.8) + 32; change the line: menu. You are now transmitting data 12 times faster. is appearing in the Serial Monitor more informative. To More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC10 Unchanging Try pinching the sensor with your fingers to heat it up or pressing a bag of ice against it to cool it down. } option /* void setup() * getVoltage() - returns the voltage on the analog input { * defined by pin Serial.begin(9600); //Start the serial connection */ //with the computer float getVoltage(int pin){ //to view the result open the return (analogRead(pin) * .004882814);//converting from a 0 //serial monitor //to 1024 digital range //last button beneath the file // to 0 to 5 volts //bar (looks like a box with an //(each 1 reading equals ~ 5 //antenna) } millivolts } NOT WORKING? (3 things to try) MAKING IT BETTER MORE, MORE, MORE:
  • 30. CIRC-11 WHAT WE’RE DOING: THE CIRCUIT: com NO 28 .:Larger Loads:. .:Relays:. This next circuit is a bit of a test. We combine what we learned about using transistors in CIRC03 to control a relay. A relay is an electrically controlled mechanical switch. Inside the little plastic box is an electromagnet that, when energized, causes a switch to trip (often with a very satisfying clicking sound). You can buy relays that vary in size from a quarter of the size of the one in this kit up to as big as a fridge, each capable of switching a certain amount of current. They are immensely fun because there is an element of the physical to them. While all the silicon we've played with to this point is fun sometimes, you may just want to wire up a hundred switches to control something magnificent. Relays give you the ability to dream it up then control it with your Arduino. Now to using today's technology to control the past. (The 1N4001 diode is acting as a flyback diode, for details on why it's there visit: http://ardx.org/4001) Parts: CIRC-11 Breadboard Sheet x1 10k Ohm Resistor Brown-Black-Orange x1 2 Pin Header x4 330 Ohm Resistor Orange-Orange-Brown x2 Relay (SPDT) x1 Red LED x1 Schematic Arduino pin 2 resistor (10kohm) Base Transistor P2N2222AG Collector Emitter Diode (flyback) +5 volts gnd (ground) (-) Diode (1N4001) x1 the transistor will have P2N2222AG printed on it (some variations will have the pin assignment reversed) Transistor P2N2222AG (TO92) x1 Yellow LED x1 coil - NC The Internet .:download:. breadboard layout sheet http://ardx.org/BBLS11S .:view:. assembly video http://ardx.org/VIDE11
  • 31. CODE (no need to type everything in just click) NOT WORKING? (3 things to try) MAKING IT BETTER CIRC-11 Not Quite Working The 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). Watch the Back-EMF Pulse Replace the diode with an LED. You’ll see it blink each time it “snubs” the coil voltage spike when it turns off. Controlling a Motor In CIRC-03 we controlled a motor using a transistor. However if you want to control a larger motor a relay is a good option. To do this simply remove the red LED, and connect the motor in its place (remember to bypass the 330 Ohm resistor). MORE, MORE, MORE: No Clicking Sound The transistor or coil portion of the circuit isn't quite working. Check the transistor is plugged in the right way. 29 File > Examples > 1.Basic > Blink (example from the great arduino.cc site, check it out for other great ideas) /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We use pin 13 because, * depending on your Arduino board, it has either a built-in LED * or a built-in resistor so that you need only an LED. * * http://www.arduino.cc/en/Tutorial/Blink */ int ledPin = 2; // *********** CHANGE TO PIN 2 ************ void setup() // run once, when the sketch starts { pinMode(ledPin, OUTPUT); // sets the digital pin as output } void loop() // run over and over again { digitalWrite(ledPin, HIGH); // sets the LED on delay(1000); // waits for a second digitalWrite(ledPin, LOW); // sets the LED off delay(1000); // waits for a second } More details, where to buy more parts, where to ask more questions: http://ardx.org/CIRC11 Nothing Happens The example code uses pin 13 and we have the relay connected to pin 2. Make sure you made this change in the code.
  • 32. CIRC-12 WHAT WE’RE DOING: THE CIRCUIT: 330 Ohm Resistor Orange-Orange-Brown x3 2 Pin Header x4 CIRC-12 Breadboard Sheet x1 Parts: Schematic Arduino pin 11 pin 10 pin 9 The Internet .:download:. breadboard layout sheet http://ardx.org/BBLS12 30 .:Colorful Light:. .:RGB LEDs:. When you first started with CIRC01 you were happy just to get a red LED blinking away. But you're past that now, right? You want orange, you want teal, you want aubergine! Fortunately there's a way to shine multiple colors from a single LED without having to stock up on every shade of the rainbow. To do this we use a RGB LED. An RGB LED isn't a single LED it's actually three LEDs in one small package: one Red, one Green and one Blue. When you turn them on their light mixes together and you get other colors. The color you get is a result of the intensity of the individual red, green and blue LEDs. We control the intensity with Pulse Width Modulation (PWM) which we've used before to control LED brightness and motor speed. Wire 5mm RGB LED x1 resistor (330ohm) common (gnd) red green blue gnd blue green red longest lead flat side
  • 33. CODE (no need to type everything in just click) /*Cycles through the colors of a RGB LED*/ NOT WORKING? (3 things to try) MAKING IT BETTER MORE, MORE, MORE: 31 Download the Code from (http://ardx.org/CODE12S) (copy the text and paste it into an empty Arduino Sketch) Seeing Red The 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_LED_PIN, redIntensity); to analogWrite(RED_LED_PIN, redIntensity/3); Looking For More? More details, where to buy more parts, where to ask more questions: http://sparkfun.com/RGB (shameless plug) If you’re looking to do more why not check out all the lovely extra bits and bobs available from http://www.SparkFun.com LED Remains Dark or Shows Incorrect Color With 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. // Cycle color from green through to blue // LED leads connected to PWM pins for (blueIntensity = 0; const int RED_LED_PIN = 9; blueIntensity <= 255; const int GREEN_LED_PIN = 10; blueIntensity+=5) { const int BLUE_LED_PIN = 11; greenIntensity = 255-blueIntensity; // Used to store the current intensity level analogWrite(BLUE_LED_PIN, blueIntensity); int redIntensity = 0; analogWrite(GREEN_LED_PIN, greenIntensity); int greenIntensity = 0; delay(DISPLAY_TIME); int blueIntensity = 0; } // Length of time showing each color // Cycle cycle from blue through to red const int DISPLAY_TIME = 100; // milliseconds for (redIntensity = 0; redIntensity <= 255; void setup() { redIntensity+=5) { // No setup required. blueIntensity = 255-redIntensity; } analogWrite(RED_LED_PIN, redIntensity); analogWrite(BLUE_LED_PIN, blueIntensity); void loop() { delay(DISPLAY_TIME); // Cycle color from red through to green } for (greenIntensity = 0; } greenIntensity <= 255; greenIntensity+=5) { ---------- More Code Online ---------- redIntensity = 255-greenIntensity; analogWrite(GREEN_LED_PIN, greenIntensity); analogWrite(RED_LED_PIN, redIntensity); delay(DISPLAY_TIME); } Using HTML-style color codes A land of diffusion If you're familiar with making web pages you One disadvantage of using a RGB LED made up of might prefer to specify colors using "hex triplets" three separate LEDs to generate our colors is that like you do when you use HTML and CSS. A hex sometimes it's possible to see the color of the triplet specifies a color using a series of letters individual lights. One way to workaround this is to find and numbers like `#FF0000` for red or a way to make the light more diffuse (or scattered) so `#800080` for purple. You can learn more about that the individual colors mix together better. The LED how this works on Wikipedia supplied with your kit is diffused rather than clear to (http://ardx.org/HEXCOL) and find a list of help improve the effectiveness of the color mixing. If colors with their associated hex triplets so you the light still isn't diffuse enough you can try putting don't need to work it out yourself. the LED behind some paper or acrylic; or inside a ping Download the code from: pong ball or polystyrene ball. http://ardx.org/RGBMB CIRC-12
  • 34. 10k Ohm Resistor Brown-Black-Orange x1 2 Pin Header x4 WHAT WE’RE DOING: THE CIRCUIT: CIRC-13 Breadboard Sheet x1 Parts: signal (white) gnd (black) The Internet .:download:. breadboard layout sheet http://ardx.org/BBLS13S 32 .:Measuring Bends:. .:Flex Sensor:. In life it's important to be flexible. But what do you do if you want to measure how flexible an object is? You use a flex sensor. A flex sensor uses carbon on a strip of plastic to act like a variable resistor or potentiometer (CIRC-08) but instead of changing the resistance by turning a knob you change it by flexing (bending) the component. We use a "voltage divider" again (CIRC-08 & 09) to detect this change in resistance. The 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 flex sensor to control the position of a servo. Wire Flex Sensor x1 CIRC-13 3 Pin Header x1 Mini Servo x1 Schematic +5 volts flex sensor analog pin 0 resistor (10k ohm) pin 9 gnd (ground) (-) +5v (red) Mini Servo
  • 35. CODE (no need to type everything in just click) CIRC-13 // Based on } // File > Examples > Servo > Knob // Controlling a servo position using a void loop() // potentiometer (variable resistor) { // by Michal Rinott // val = analogRead(potpin);// reads the value <http://people.interaction- // of the potentiometer NOT WORKING? (3 things to try) Servo Not Twisting? Even with colored wires it is still shockingly easy to plug a servo in backwards. This might be the case. MAKING IT BETTER MORE, MORE, MORE: 33 Download the Code from (http://ardx.org/CODE13S) (copy the text and paste it into an empty Arduino Sketch) //(value between 0 and 1023) Servo Not Moving As Expected The sensor is only designed to work in one direction. Try flexing it the other way. (where the striped side faces in on a convex curve) More details, where to buy more parts, where to ask more questions: http://sparkfun.com/ Servo Moves Once You may need to modify the range of values in the call to the map() function (details in the making it better section below) ivrea.it/m.rinott> Serial.println(val); val = map(val, 50, 300, 0, 179); #include <Servo.h> // scale it to use it with the servo // (value between 0 and 180) Servo myservo; // create servo object to myservo.write(val); // sets the servo // control a servo // according to the // scaled value int potpin = 0; // analog pin used to // connect the delay(15); // waits for the servo // potentiometer // to get there int val; // variable to read the } //value from the analog pin void setup() { Serial.begin(9600); myservo.attach(9);// attaches the servo // servo on pin 9 to the servo object Calibrating the Range 300) value with the fully bent value. While the servo is now moving chances are its Applications range isn’t quite perfect. To adjust the range we With sensors the real fun comes in using them in neat need to change the values in the map() function. and un-expected ways here are a few of our favorite map(value, fromLow, fromHigh, toLow, toHigh) flex sensor applications. For full details on how it works: One Player Rock Paper Scissors Glove http://ardx.org/MAP A glove that lets you play RPS against yourself. http://ardx.org/RPS To calibrate our sensor we can use the debug window (like in CIRC-11). Open the debug Electronic Plant Brace window then replace the Monitor if your plant is bending towards light fromLow value (default 50) with the value displayed when the sensor is and fix it. http://ardg.org/BRACE un bent. Then replace the fromHigh (default
  • 36. 2 Pin Header x4 330 Ohm Resistor Orange-Orange-Brown x3 WHAT WE’RE DOING: THE CIRCUIT: CIRC-14 Breadboard Sheet x1 Parts: Schematic Arduino pin 11 pin 10 pin 9 The Internet .:download:. breadboard layout sheet http://ardx.org/BBLS14S 34 .:Fancy Sensing:. .:Soft Potentiometer:. A "soft pot" (short for "soft potentiometer") is like a regular potentiometer of the style seen in CIRC-08 except, it's flat, really thin, flexible and doesn't have a knob. A potentiometer is also known as a "variable resistor" and for a soft pot the resistance it provides is determined by where pressure is applied. Pressure can be applied with your finger, a stylus or a hard plastic "wiper". By pressing down on various parts of the strip, the resistance varies from 100 to 10k Ohms allowing you to calculate the relative position on the strip. You can use this to track movement on the softpot or discrete "button style" presses. In this circuit we’ll use it to control the color of an RGB LED. Wire 5mm RGB LED x1 CIRC-14 Soft Potentiometer x1 resistor (330ohm) gnd blue green red longest lead +5 volts Arduino analog pin 0 wiper V+ gnd soft-pot
  • 37. CODE (no need to type everything in just click) CIRC-14 // CIRC-14 Example Code int greenValue = constrain( // Will fade an RGB LED from Red-Green-Blue map(sensorValue, 0, 512, 0, 255),0,255)- // in relation to the soft pot value constrain( NOT WORKING? (3 things to try) MAKING IT BETTER MORE, MORE, MORE: 35 Download the Code from (http://ardx.org/CODE14S) (copy the text and paste it into an empty Arduino Sketch) Seeing Red The 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_LED_PIN, redValue); to analogWrite(RED_LED_PIN, redValue/3); More details, where to buy more parts, where to ask more questions: http://sparkfun.com/ Bizarre Results The most likely cause of this is if you’re pressing the potentiometer in more than one position. This is normal and can actually be used to create some neat results. LED Remains Dark or Shows Incorrect Color With 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. map(sensorValue, 512, 1023, 0, 255),0,255); // LED leads connected to PWM pins //calculate the green value (0-255 const int RED_LED_PIN = 9; //Red LED Pin //over 0-512 & 255-0 over 512-1023) const int GREEN_LED_PIN = 10; //Green LED Pin int blueValue = constrain( const int BLUE_LED_PIN = 11; //Blue LED Pin map(sensorValue, 512, 1023, 0, 255),0,255); //calculate the blue value 0-255 over //512-1023 void setup() { //no need for any code here // Display the requested color } analogWrite(RED_LED_PIN, redValue); analogWrite(GREEN_LED_PIN, greenValue); void loop() { analogWrite(BLUE_LED_PIN, blueValue); int sensorValue = analogRead(0); } //read the Soft Pot int redValue = constrain( map(sensorValue, 0, 512, 255, 0),0,255); //calculate the red Value (255-0 //over the range 0-512) HSB Color (Hue, Saturation & Brightness) Now when you use the soft pot you’ll notice a much Our RGB LED displays colors using RGB color cleaner and fuller fade from red through to violet. codes. However this is often not the easiest way Faux Buttons of working with colors (as noted by the less than Because of the way the soft pot works it can also be reader friendly code above). A much more used to make custom buttons. To do this you define a intuitive system is HSB. range of values corresponding to a discrete button. Full Details: Use the code snippet below and the debug window to http://ardx.org/HSB determine desired values. To convert from RGB to HSB all that is required is some slightly complicated math. For an example if(analogRead(0) > minValue && analogRead(0) < maxValue){ program visit: buttonAction() http://ardx.org/CODE14MB } based on www.kasperkamperman.com's original Then cover the soft pot with a drawn/printed button code: http://ardx.org/KASP pattern
  • 38. NOTE 36 notes .:Notes:. .:Room for a Few Notes:.
  • 39. NOTE 37 notes .:Notes:. .:Room for a Few Notes:.
  • 40. 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.