Ardx Eg Adaf Web
Ardx Eg Adaf Web
Ardx Eg Adaf Web
Experimenters
Guide
for Arduino
(ARDX)
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 :.
Were 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
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, well try our best to help out.
help@oomlout.com
(we like hearing about problems it helps us improve future versions)
TBCN
table of contents
Before We Start
{ASEM}
02
{INST}
03
{PROG}
04
{ELEC}
06
The Circuits
{CIRC01}
08
{CIRC02}
10
{CIRC03}
12
{CIRC04}
14
{CIRC05}
16
{CIRC06}
18
{CIRC07}
20
{CIRC08}
Twisting - (Potentiometers)
22
{CIRC09}
24
{CIRC10}
26
{CIRC11}
28
{CIRC12}
30
{CIRC13}
32
01
01 ASEM
assembling the
pieces
02
.: PUTTING IT TOGETHER :.
Arduino Holder
x1
Breadboard
x1
4-40 nut
x4
Arduino
x1
Bumper
x4
02 INST
installing
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.
Windows XP
Mac OSX
Unzip
-windows.zip (r
- version #)
arduino-1.r
Recommended Path
c:\Program Files\
Open (mount)
arduino-1.r
-macosx.zip (r
- version #)
Right Click
Arduino.exe (send to>Desktop (create shortcut))
Finished
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
.: NOTE: :.
.: Encountering problems? :.
.: Would like more details? Using Linux? :.
.: http://ardx.org/LINU :.
Finished
03
03 PROG
programming
primer
STRUCTURE
Each Arduino program
(often called a sketch) has
two required functions
(also called routines).
void setup(){
void loop(){
SYNTAX
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.
{ } (curly brackets)
Used to define when a block
of code starts and ends (used
in functions as well as loops).
; (semicolon)
Each line of code must be
ended with a semicolon (a
missing semicolon is often
the reason for a program
refusing to compile).
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.
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.
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.
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 chars.
VARIABLES
A program is nothing more
than instructions to move
numbers around in an
intelligent way. Variables are
used to do the moving.
boolean (boolean)
A simple True or False
variable. Useful
because it only
uses one bit of
RAM.
04
03 PROG
programming
primer
MATHS OPERATORS
Operators used for
manipulating numbers.
(they work like simple
maths).
COMPARISON OPERATORS
Operators used for
logical comparison.
==
!=
<
>
CONTROL STRUCTURE
Programs are reliant on
controlling what runs
next, here are the basic
control elements (there
are many more online).
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.
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
pinMode(pin, mode);
digitalWrite(pin, value);
int digitalRead(pin);
int analogWrite(pin,
value);
int analogRead(pin);
ANALOG
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.
05
04 ELEC
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)
Diode
Resistors
Transistor
What it Does:
Emits light when a small current is
passed through it. (only in one direction)
Identifying:
Looks like a mini light bulb.
No. of Leads:
2 (one longer, this one connects to positive)
Things to watch out for:
- Will only work in one direction
- Requires a current limiting resistor
More Details:
http://ardx.org/LED
What it Does:
The electronic equivalent of a one way
valve. Allowing current to flow in one
direction but not the other.
Identifying:
Usually a cylinder with wires extending from
either end. (and an off center line indicating polarity)
No. of Leads:
2
Things to watch out for:
- Will only work in one direction
What it Does:
Restricts the amount of current that can
flow through a circuit.
Identifying:
Cylinder with wires extending from either
end. The value is displayed using a color
coding system (for details see next page)
No. of Leads:
2
Things to watch out for:
- Easy to grab the wrong value
What it Does:
Uses a small current to switch or amplify a
much larger current.
Identifying:
Comes in many different packages but you
can read the part number off the package.
No. of Leads:
3 (Base, Collector, Emitter)
Things to watch out for:
- Plugging in the right way round (also a
Hobby Servo
DC Motor
06
What it Does:
Takes a timed pulse and converts it into
an angular position of the output shaft.
Identifying:
A plastic box with 3 wires coming out one
side and a shaft with a plastic horn out
the top.
What it Does:
Spins when a current is passed through it.
Identifying:
This one is easy, it looks like a motor.
Usually a cylinder with a shaft coming out
of one end.
(current will
flow if end with the line is connected to ground)
More Details:
http://ardx.org/DIOD
(double
More Details:
http://ardx.org/RESI
More Details:
http://ardx.org/TRAN
No. of Leads:
3
Things to watch out for:
- The plug is not polarized so make sure
it is plugged in the right way.
More Details:
http://ardx.org/SERV
No. of Leads:
2
Things to watch out for:
- Using a transistor or relay that is rated
for the size of motor you're using.
More Details:
http://ardx.org/MOTO
04 ELEC
electronics
primer
No. of Leads:
2
Things to watch out for:
- Difficult to misuse.
More Details:
http://ardx.org/PIEZ
IC (Integrated Circuit)
What it Does:
Packages any range of complicated
electronics inside an easy to use package.
Identifying:
The part ID is written on the outside of the
package. (this sometimes requires a lot of
light or a magnifying glass to read).
Pushbutton
What it Does:
Completes a circuit when it is pressed.
Identifying:
A little square with leads out the bottom
and a button on the top.
No. of Leads:
2 - 100s (in this kit there is one with 3 (TMP36) and
one with 16 (74HC595)
No. of Leads:
4
Things to watch out for:
- these are almost square so can be
inserted 90 degrees off angle.
More Details:
http://ardx.org/BUTT
Potentiometer
What it Does:
Produces a variable resistance dependant
on the angular position of the shaft.
Identifying:
They can be packaged in many different
form factors, look for a dial to identify.
No. of Leads:
3
Things to watch out for:
- Accidentally buying logarithmic scale.
More Details:
http://ardx.org/POTE
Photo Resistor
What it Does:
Produces a variable resistance dependant
on the amount of incident light.
Identifying:
Usually a little disk with a clear top and a
curvy line underneath.
0
1
2
3
4
Black
Brown
Red
Orange
Yellow
5
6
7
8
9
Green
Blue
Purple
Grey
White
first digit
second digit
# of zeros
tolerance
20% - none
10% - silver
5% - gold
No. of Leads:
2
Things to watch out for:
- Remember it needs to be in a voltage
divider before it provides a useful input.
More Details:
http://ardx.org/PHOT
LEAD CLIPPING
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.
07
CIRC-01
.:Getting Started:.
.:(Blinking LED):.
THE CIRCUIT:
Parts:
CIRC-01
Breadboard Sheet
x1
10mm LED
x1
Wire
.:note:.
Schematic
Arduino
pin 13
longer lead
+
LED
(light emitting diode)
resistor
(560ohm)
(green-blue-brown)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS01
.:view:.
assembly video
http://ardx.org/VIDE01
.:note:.
The 5v (red) wire is not used,
it's included for uniformity
with other circuits.
08
CODE
CIRC-01
13;
NOT WORKING?
(3 things to try)
Still No Success?
harm).
tools>serial port>
MAKING IT BETTER
Changing the pin:
The LED is connected to pin 13 but we can use any of
Fading:
We will use another included example program. To open go to
File > Examples > 3.Analog > Fading
Then upload to your board and watch as the LED fades in and
then out.
http://ardx.org/CIRC01
09
CIRC-02
THE CIRCUIT:
Parts:
CIRC-02
Breadboard Sheet
x1
560 Ohm Resistor
Green-Blue-Brown
x8
Schematic
pin 2 pin 3 pin 4 pin 5
LED
resistor
560ohm
gnd
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS02
.:view:.
assembly video
http://ardx.org/VIDE02
10
2 Pin Header
x4
Wire
CODE
CIRC-02
/*
* oneAfterAnotherNoLoop() - Will light one then
* delay for delayTime then light the next LED it
NOT WORKING?
(3 things to try)
Starting Afresh
MAKING IT BETTER
Switching to loops:
In the loop() function there are 4 lines. The last
three all start with a '//'. This means the line is
treated as a comment (not run). To switch the
program to use loops change the void loop()
code to:
//oneAfterAnotherNoLoop();
oneAfterAnotherLoop();
//oneOnAtATime();
//inAndOut();
Extra animations:
Tired of this animation? Then try the other two
sample animations. Uncomment their lines and upload
the program to your board and enjoy the new light
animations. (delete the slashes in front of row 3 and then 4)
Testing out your own animations:
Jump into the included code and start changing
things. The main point is to turn an LED on use
digitalWrite(pinNumber, HIGH); then to turn
it off use digitalWrite(pinNumber, LOW); .
Type away, regardless of what you change you won't
break anything.
http://ardx.org/CIRC02
11
CIRC-03
THE CIRCUIT:
Parts:
CIRC-03
Breadboard Sheet
x1
2 Pin Header
x4
Transistor
P2N2222AG (TO92)
x1
Toy Motor
x1
Diode
(1N4001)
x1
Schematic
Arduino
pin 9
Wire
resistor
(2.2kohm)
Base
Collector
Transistor
P2N2222AG
Emitter
Diode
Motor
gnd
(ground) (-)
+5 volts
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS03
.:view:.
assembly video
http://ardx.org/VIDE03
12
.:NOTE: if your arduino is resetting you need to install the optional capacitor:.
CODE
CIRC-03
NOT WORKING?
void motorOnThenOffWithSpeed(){
int onSpeed = 200;// a number between
//0 (stopped) and 255
int onTime = 2500;
int offSpeed = 50;// a number between
//0 (stopped) and 255
int offTime = 1000;
analogWrite(motorPin, onSpeed);
// turns the motor On
delay(onTime);
// waits for onTime
analogWrite(motorPin, offSpeed);
// turns the motor Off
delay(offTime);
// waits for offTime
}
(full speed)
(full speed)
milliseconds
milliseconds
void motorAcceleration(){
int delayTime = 50; //time between each speed step
for(int i = 0; i < 256; i++){
//goes through each speed from 0 to 255
analogWrite(motorPin, i);
//sets the new speed
delay(delayTime);// waits for delayTime milliseconds
}
for(int i = 255; i >= 0; i--){
//goes through each speed from 255 to 0
analogWrite(motorPin, i);
//sets the new speed
delay(delayTime);//waits for delayTime milliseconds
}
}
(3 things to try)
Still No Luck?
power.
USB port.
MAKING IT BETTER
Controlling speed:
We played with the Arduino's ability to control the
brightness of an LED earlier now we will use the same
feature to control the speed of our motor. The Arduino
does this using something called Pulse Width
Modulation (PWM). This relies on the Arduino's ability to
operate really, really fast. Rather than directly
controlling the voltage coming from the pin the Arduino
will switch the pin on and off very quickly. In the
computer world this is going from 0 to 5 volts many
times a second, but in the human world we see it as a
voltage. For example if the Arduino is PWM'ing at 50%
we see the light dimmed 50% because our eyes are not
quick enough to see it flashing on and off. The same
feature works with transistors. Don't believe me? Try it
out.
http://ardx.org/CIRC03
13
CIRC-04
THE CIRCUIT:
Parts:
CIRC-04
Breadboard Sheet
x1
Mini Servo
x1
Schematic
Arduino
pin 9
Mini Servo
signal
(orange)
+5v
(red)
gnd
(black/
brown)
gnd
(ground) (-)
+5 volts
(5V)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS04
.:view:.
assembly video
http://ardx.org/VIDE04
14
2 Pin Header
x4
3 Pin Header
x1
Wire
CODE
CIRC-04
// 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
void setup() {
myservo.attach(9);
}
void loop() {
for(pos = 0; pos < 180; pos +=
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}
NOT WORKING?
1)
(3 things to try)
MAKING IT BETTER
Potentiometer control:
We have yet to experiment with inputs but if you would like
to read ahead, there is an example program File > Servo >
Knob. This uses a potentiometer (CIRC08) to control the
servo. You can find instructions online here:
void loop() {
int pulseTime = 2100; //(the number of microseconds
//to pause for (1500 90 degrees
// 900 0 degrees 2100 180 degrees)
digitalWrite(servoPin, HIGH);
delayMicroseconds(pulseTime);
digitalWrite(servoPin, LOW);
delay(25);
http://ardx.org/KNOB
Self timing:
While it is easy to control a servo using the Arduino's included
Great ideas:
Servos can be used to do all sorts of great things, here are a few of
our favorites.
Open Source Robotic Arm (uses a servo controller as well as the Arduino)
http://ardx.org/RARM
int servoPin = 9;
void setup(){
pinMode(servoPin,OUTPUT);
}
Servo Walker
http://ardx.org/SEWA
http://ardx.org/CIRC04
15
CIRC-05
THE CIRCUIT:
Parts:
CIRC-05
Breadboard Sheet
x1
2 Pin Header
x4
Red LED
x8
Shift Register
74HC595
x1
Wire
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS05
.:view:.
assembly video
http://ardx.org/VIDE05
16
(ground) (-)
+5V 0
1
2
data 3
clock 4
latch
5
6
gnd 7
gnd
74HC595
(560ohm)
LED
resistor
Schematic
CODE
CIRC-05
digitalWrite(latch, LOW);
//Pulls the chips latch low
shiftOut(data, clock, MSBFIRST, value);
//Shifts out 8 bits to the shift register
digitalWrite(latch, HIGH);
//Pulls the latch high displaying the data
}
void loop()
// run over and over again
{
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)
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
MAKING IT BETTER
Doing it the hard way:
An Arduino makes rather complex actions very easy, shifting out data is
one of these cases. However one of the nice features of an Arduino is
you can make things as easy or difficult as you like. Let's try an
example of this. In your loop switch the line:
updateLEDs(i) -> updateLEDsLong(i);
Upload the program and notice nothing has changed. If you look at the
code you can see how we are communicating with the chip one bit at a
time. (for more details http://ardx.org/SPI ).
Controlling individual LEDs:
Time to start controlling the LEDs in a similar method as we did in
CIRC02. As the eight LED states are stored in one byte (an 8 bit value)
for details on how this works try http://ardx.org/BINA. An Arduino is
very good at manipulating bits and there are an entire set of operators
that help us out. Details on bitwise maths ( http://ardx.org/BITW ).
Our implementation.
Replace the loop() code with
int delayTime = 100; //the number of milliseconds
//to delay
http://ardx.org/CIRC05
17
CIRC-06
.:Music:.
.:Piezo Elements:.
THE CIRCUIT:
Parts:
CIRC-06
Breadboard Sheet
x1
Schematic
Arduino
pin 9
Piezo
Element
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS06
.:view:.
assembly video
http://ardx.org/VIDE06
18
2 Pin Header
x4
Piezo Element
x1
Wire
CODE
CIRC-06
NOT WORKING?
digitalWrite(speakerPin,
LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956
};
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
(3 things to try)
No Sound
Given the size and shape of
the piezo element it is easy to
miss the right holes on the
breadboard. Try double
MAKING IT BETTER
Playing with the speed:
The timing for each note is calculated based on
variables, as such we can tweak the sound of each note
or the timing. To change the speed of the melody you
need to change only one line.
int tempo = 300; ---> int tempo = (new #)
Change it to a larger number to slow the melody down,
or a smaller number to speed it up.
Tuning the notes:
If you are worried about the notes being a little out of
tune this can be fixed as well. The notes have been
calculated based on a formula in the comment block at
the top of the program. But to tune individual notes just
adjust their values in the tones[] array up or down
until they sound right. (each note is matched by its
name in the names[] (array ie. c = 1915 )
http://ardx.org/CIRC06
19
.:Button Pressing:.
CIRC-07
.:Pushbuttons:.
THE CIRCUIT:
Parts:
CIRC-07
Breadboard Sheet
x1
10k Ohm Resistor
Brown-Black-Orange
x2
Schematic
Arduino
pin 2
Arduino
pin 13
resistor
LED
resistor
(560ohm)
(10k ohm)
pushbutton
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS07
.:view:.
assembly video
http://ardx.org/VIDE07
20
pin 3
+5 volts
2 Pin Header
x4
560 Ohm Resistor
Green-Blue-Brown
x1
Pushbutton
x2
Wire
Red LED
x1
CODE
CIRC-07
(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);
pinMode(inputPin, INPUT);
}
void loop(){
val = digitalRead(inputPin);
if (val == HIGH) {
digitalWrite(ledPin, LOW);
} else {
digitalWrite(ledPin, HIGH);
}
}
NOT WORKING?
(3 things to try)
Underwhelmed?
MAKING IT BETTER
On button off button:
The initial example may be a little underwhelming (ie. I
you will need to change the wire connecting the LED from pin
the other will turn the LED off. Change the code to:
int ledPin = 13;
int inputPin1 = 3;
int inputPin2 = 2;
void setup() {
pinMode(ledPin, OUTPUT);
// declare LED as output
pinMode(inputPin1, INPUT); // make button 1 an input
pinMode(inputPin2, INPUT); // make button 2 an input
}
void loop(){
if (digitalRead(inputPin1) == LOW) {
digitalWrite(ledPin, LOW); // turn LED OFF
} else if (digitalRead(inputPin2) == LOW) {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
int value = 0;
void loop(){
if (digitalRead(inputPin1) == LOW) { value--; }
else if (digitalRead(inputPin2) == LOW) { value++; }
value = constrain(value, 0, 255);
analogWrite(ledPin, value);
delay(10);
}
larger number.
http://ardx.org/CIRC07
21
.:Twisting:.
CIRC-08
.:Potentiometers:.
THE CIRCUIT:
Parts:
CIRC-08
Breadboard Sheet
x1
2 Pin Header
x4
Green LED
x1
Schematic
Arduino
+5 volts
pin 13
Arduino
Potentiometer
LED
(light
emitting
diode)
resistor
(560ohm)
(green-blue-brown)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS08
.:view:.
assembly video
http://ardx.org/VIDE08
22
analog
pin 0
Potentiometer
10k ohm
x1
Wire
CODE
CIRC-08
(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;
int ledPin = 13;
int sensorValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);//
digitalWrite(ledPin, HIGH);
//
delay(sensorValue);
//
digitalWrite(ledPin, LOW);
//
delay(sensorValue);
//
}
NOT WORKING?
read
turn
stop
turn
stop
the
the
the
the
the
(3 things to try)
Sporadically Working
Not Working
Still Backward
You can try operating the
circuit upside down.
Sometimes this helps.
power pins)
MAKING IT BETTER
Threshold switching:
Sometimes you will want to switch an output when a value
exceeds a certain threshold. To do this with a
potentiometer change the loop() code to.
void loop() {
int threshold = 512;
if(analogRead(sensorPin) > threshold){
digitalWrite(ledPin, HIGH);}
else{ digitalWrite(ledPin, LOW);}
}
This will cause the LED to turn on when the value is above
512 (about halfway), you can adjust the sensitivity by
changing the threshold value.
Fading:
Lets control the brightness of an LED directly from the
potentiometer. To do this we need to first change the pin
the LED is connected to. Move the wire from pin 13 to pin
9 and change one line in the code.
http://ardx.org/CIRC08
23
CIRC-09
.:Light:.
.:Photo Resistors:.
THE CIRCUIT:
Parts:
CIRC-09
Breadboard Sheet
x1
10k Ohm Resistor
Brown-Black-Orange
x1
Schematic
Arduino
pin 13
+5 volts
resistor
(10k ohm)
LED
Arduino
analog
pin 0
resistor
(560ohm)
photo
resistor
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS09
.:view:.
assembly video
http://ardx.org/VIDE09
24
2 Pin Header
x4
560 Ohm Resistor
Green-Blue-Brown
x1
Photo-Resistor
x1
Green LED
x1
Wire
CODE
CIRC-09
//output
}
/*
* loop() - this function will start after setup
* finishes and then repeat
*/
void loop()
{
int lightLevel = analogRead(lightPin); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to 0 to 255
lightLevel = constrain(lightLevel, 0, 255);
//make sure the value is between 0 and 255
analogWrite(ledPin, lightLevel); //write the value
}
void setup()
{
pinMode(ledPin, OUTPUT); //sets the led pin to
NOT WORKING?
(3 things to try)
It Isn't Responding to
Changes in Light.
try.
MAKING IT BETTER
Reverse the response:
Perhaps you would like the opposite response. Don't
worry we can easily reverse this response just change:
analogWrite(ledPin, lightLevel); ---->
analogWrite(ledPin, 255 - lightLevel);
http://ardx.org/CIRC09
25
.:Temperature:.
CIRC-10
THE CIRCUIT:
Parts:
CIRC-10
Breadboard Sheet
x1
TMP36
Temperature Sensor
x1
2 Pin Header
x4
Wire
Schematic
Arduino
analog
pin 0
+5 volts
+5v
signal
gnd
TMP36
(precision
temperature
sensor)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS10
.:view:.
assembly video
http://ardx.org/VIDE10
26
CODE
CIRC-10
NOT WORKING?
void loop()
// run over and over again
{
float temperature = getVoltage(temperaturePin);
//getting the voltage reading from the
//temperature sensor
(3 things to try)
Gibberish is Displayed
This happens because the serial
Temperature Value is
Unchanging
to cool it down.
MAKING IT BETTER
Outputting voltage:
This is a simple matter of changing one line. Our
sensor outputs 10mv per degree centigrade so to get
voltage we simply display the result of getVoltage().
delete the line
the speed from 9600 baud to 115200 baud in the pull down
menu. You are now transmitting data 12 times faster.
http://ardx.org/CIRC10
27
CIRC-11
.:Larger Loads:.
.:Relays:.
THE CIRCUIT:
Parts:
CIRC-11
Breadboard Sheet
x1
Diode
(1N4001)
x1
Transistor
P2N2222AG (TO92)
x1
Green LED
x1
Relay
(DPDT)
x1
Red LED
x1
2 Pin Header
x4
Schematic
Arduino
pin 2
resistor
(2.2kohm)
Transistor
Base P2N2222AG
Collector
NC com
coil
NO
Emitter
Diode
(flyback)
+5 volts
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS11
.:view:.
assembly video
http://ardx.org/VIDE11
28
CODE
CIRC-11
// ***********
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
NOT WORKING?
//
//
//
//
(3 things to try)
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.
No Clicking Sound
MAKING IT BETTER
Watch the Back-EMF Pulse
Replace the diode with an LED. Youll see it blink each time it snubs the coil voltage spike when it
Arduino
pin 2
resistor
(2.2kohm)
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
Transistor
P2N2222AG
relay is a good option. To do this simply remove the red LED, and connect the motor in its place
Base
Collector
a DC motor we must be able to reverse the direction of current flow through it.
To do this manually we reverse the leads. To do it electrically we require
something called an h-bridge. This can be done using a DPDT relay to control
the motor's direction, wire up the following circuit. It looks complicated but can
be accomplished using only a few extra wires. Give it a try.
NO
NC com
Emitter
Diode
coil
(flyback)
+5 volts
gnd
(ground) (-)
http://ardx.org/CIRC11
29
.:Colorful Light:.
CIRC-12
.:RGB LEDs:.
ma
e
blu
ma
w gree
n
w gree
n
blu
ge
nta
analog
red
llo
ye
ge
nta
digital
llo
ye
cyan
cyan
THE CIRCUIT:
Parts:
CIRC-12
Breadboard Sheet
x1
2 Pin Header
x4
pin 10
pin 9
green
red
pin 11
resistor
green
blue
common (+5v)
red
Schematic
Arduino
(560ohm)
blue
longest
lead
+5v
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS12A
30
flat
side
Wire
CODE
CIRC-12
void setup(){
for(int i = 0; i < 3; i++){
pinMode(ledDigitalOne[i], OUTPUT);
//Set the three LED pins as outputs
}
}
void loop(){
setColor(ledDigitalOne, CYAN);
//Set the color of the LED
//randomColor()
//Predefined Colors
const boolean RED[] = {ON, OFF, OFF};
const boolean GREEN[] = {OFF, ON, OFF};
const boolean BLUE[] = {OFF, OFF, ON};
const boolean YELLOW[] = {ON, ON, OFF};
const boolean CYAN[] = {OFF, ON, ON};
const boolean MAGENTA[] = {ON, OFF, ON};
const boolean WHITE[] = {ON, ON, ON};
const boolean BLACK[] = {OFF, OFF, OFF};
}
void randomColor(){
int rand = random(0, sizeof(COLORS) / 2);
//get a random number within the range of
//colors
setColor(ledDigitalOne, COLORS[rand]);
//Set the color of led one to a random color
delay(1000);
}
NOT WORKING?
(3 things to try)
Seeing Red
MAKING IT BETTER
More Colors
I imagine you are less than impressed by the
between them. Sadly the code for this is a bit too long
for the section above, for an example program (with
lots of comments).
http://adafruit.com
31
.:Squeezing:.
CIRC-13
THE CIRCUIT:
Parts:
CIRC-13
Breadboard Sheet
x1
Green Led
x1
Schematic
Arduino
pin 9
+5 volts
Arduino
FSR
analog
pin 2
LED
(light
emitting
diode)
resistor
resistor
(560ohm)
(10kohm)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS13A
32
2 Pin Header
x4
~FSR Resistance
infinite
30 k ohm
6 k ohm
1 k ohm
250 ohm
CODE
CIRC-13
/*
* Force Sensitive Resistor Test Code
*
* The intensity of the LED will vary with the amount of pressure on the sensor
*/
int sensePin = 2;
int ledPin = 9;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
int value = analogRead(sensePin) / 4; //the voltage on the pin divded by 4 (to
//scale from 10 bits (0-1024) to 8 (0-255)
analogWrite(ledPin, value);
//sets the LEDs intensity proportional to
//the pressure on the sensor
Serial.println(value);
//print the value to the debug window
}
NOT WORKING?
(3 things to try)
Fading to Fast/Slow
harm).
available from
http://www.Adafruit.com
MAKING IT BETTER
Then replace the fromHigh value with the
toHigh = 255)
Applications
With sensors the real fun comes in using them in neat
and un-expected ways. So get thinking about how
and where sensing squeeze could enhance your life.
http://adafruit.com
33
(ARDX)
experimentation kit for arduino
www.oomlout.com