Arduino Experimenters Kit
Arduino Experimenters Kit
arduino
experimentation kit
Arduino
Experimenter’s Kit
SketchBoard Edition
ARDX
Open-Source Arduino
Instruction Guide
Document Revision: Nov 18 2015
A Few Words
More details on the Creative Commons CC (By - Share Alike) License can be found at
http://ardx.org/CCLI
ABOUT .: OOMLOUT :.
We’re a plucky little design company focusing on producing
“delightfully fun open source products”
To check out what we are up to
www.oomlout.com
ABOUT SOLARBOTICS
We started out doing BEAM robot kits over 22 years ago (really!), and now we
also supply clever electronic bits & kits
http://www.solarbotics.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.
[email protected] / [email protected]
(we like hearing about problems it helps us improve future versions)
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 Lights - (RGB LEDs) 30
01
01 ASEM
assembling the .: PUTTING IT TOGETHER :.
pieces
#4 x 1/16”
4-40 x 3/8” bolt 4-40 nut
Spacer
x3 x3
x3
Use at least 2
bolt / spacer / nut
assemblies to
secure the Arduino
Step 3:
Plug in your Arduino Step 3: Copy The Application
Follow steps for installation, when prompted Go to
for drivers browse to "Arduino" (in the devices section of finder)
C:\Program Files (x86)\Arduino\drivers"
and use the drivers in this folder
Move
"Arduino" Application to the
"Applications" folder
Step 4:
Run Arduino software
by clicking the Start button
type in Arduino and press enter
Step 4: Plug In Your Arduino
Plug your Arduino in:
Step 5: Using the included USB cable, plug your Arduino
board into a free USB port.
click "Tools" > "Ports" > select the port Finished
assigned to your Arduino
This confirms your Arduino installed
correctly.
.: NOTE: :.
.: Encountering problems? :.
.: Would like more details? Using Linux? :.
.: http://ardx.org/LINU :.
03
03 PROG
programming .: A Small Programming Primer:.
primer
STRUCTURE
Each Arduino program void setup(){ } void loop(){ }
(often called a sketch) has All the code between the two This function is run after setup
two required functions curly brackets will be run once has finished. After it has run
when your Arduino program once it will be run again, and
(also called routines).
first runs. again, until power is removed.
SYNTAX
One of the slightly // (single line comment) /* */(multi line comment)
frustrating elements of C is It is often useful to write notes If you have a lot to say you can
its formatting requirements to yourself as you go along span several lines as a
(this also makes it very about what each line of code comment. Everything between
does. To do this type two these two symbols will be
powerful). If you remember
forward slashes and everything ignored in your program.
the following you should be until the end of the line will be
alright. ignored by your program.
; (semicolon)
Each line of code must be
{ } (curly brackets) ended with a semicolon (a
Used to define when a block missing semicolon is often
of code starts and ends (used the reason for a program
in functions as well as loops). refusing to compile).
VARIABLES
A program is nothing more int (integer) long (long)
The main workhorse, stores a Used when an integer is not
than instructions to move
number in 2 bytes (16 bits). large enough. Takes 4 bytes (32
numbers around in an Has no decimal places and will bits) of RAM and has a range
intelligent way. Variables are store a value between -32,768 between -2,147,483,648 and
used to do the moving. and 32,767. 2,147,483,647.
04
.:For a full programming reference visit:. 03 PROG
http://ardx.org/PROG programming
primer
MATHS OPERATORS
= (assignment) makes something equal to something else (eg. x
Operators used for = 10 * 2 (x now equals 20))
manipulating numbers. % (modulo) gives the remainder when one number is divided by
(they work like simple another (ex. 12 % 10 (gives 2))
+ (addition)
maths). - (subtraction)
* (multiplication)
/ (division)
COMPARISON OPERATORS
Operators used for == (equal to) (eg. 12 == 10 is FALSE or 12 == 12 is TRUE)
logical comparison. != (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)
CONTROL STRUCTURE
if(condition){ } for(int i = 0; i <
Programs are reliant on else if( condition ){ } #repeats; i++){ }
else { }
controlling what runs Used when you would like to
This will execute the code between
next, here are the basic repeat a chunk of code a number
the curly brackets if the condition
of times (can count up i++ or
control elements (there is true, and if not it will test the
down i-- or use any variable)
else if condition if that is also
are many more online). false the else code will execute.
DIGITAL
pinMode(pin, mode); digitalWrite(pin, value); int digitalRead(pin);
Used to set a pin's mode, pin Once a pin is set as an OUTPUT, Once a pin is set as an INPUT
is the pin number you would it can be set either HIGH (pulled you can use this to return
like to address 0-19 (analog 0- to +5 volts) or LOW (pulled to whether it is HIGH (pulled to
5 are 14-19). The mode can ground). +5 volts) or LOW (pulled to
either be INPUT or OUTPUT. ground).
ANALOG
int analogWrite(pin, int analogRead(pin);
The Arduino is a digital value);
machine but it has the ability Some of the Arduino's pins support When the analog input pins are set
to operate in the analog pulse width modulation (3, 5, 6, 9, 10, to input you can read their voltage.
11). This turns the pin on and off very A value between 0 (for 0
realm (through tricks). quickly making it act like an analog volts) and 1023 (for
Here's how to deal with output. The value is any number 5 volts) will be
things that aren't digital. between 0 (0% duty cycle ~0v) and returned.
255 (100% duty cycle ~5 volts).
05
04 ELEC
electronics .: A Small Electronics Primer:.
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 [email protected]
COMPONENT DETAILS
LED What it Does: No. of Leads:
(Light Emitting Diode) 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
Diode What it Does: No. of Leads:
The electronic equivalent of a one way 2 (stripe connects to negative)
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)
can read the part number off the package. More Details:
(P2N2222AG in this kit and find a datasheet online) http://ardx.org/TRAN
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)
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
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
If you are having trouble uploading, a full troubleshooting guide can be found here: http://ardx.org/TRBL
THE CIRCUIT:
Parts:
CIRC-01 10mm LED
2 Pin Header
Breadboard Sheet x1 Wire
x4
x1
560 Ohm Resistor
Green-Blue-Brown
x1
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
08
CODE (no need to type everything in, just click)
CIRC-01
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 original by H. Barragan for the Wiring i/o board
*/
int ledPin = 13; // LED connected to digital pin 13
MAKING IT BETTER
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)
ledPin = 13; -> int ledPin = 9;
Then in the code change the line:
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.
http://ardx.org/CIRC01
09
.:8 LED Fun:.
CIRC-02
.:Multiple LED’s:.
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).
THE CIRCUIT:
Parts:
CIRC-02 5mm Green LED
2 Pin Header
Breadboard Sheet x8 Wire
x4
x1
560 Ohm Resistor
Green-Blue-Brown
x8
Schematic
LED
resistor
560ohm
gnd
pin 6 pin 7 pin 8 pin 9
LED
resistor
560ohm
gnd
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS02
.:view:.
assembly video
http://ardx.org/VIDE02
10
CODE (no need to type everything in, just click)
CIRC-02
Download the Code from ( http://ardx.org/CODE02 )
(and then copy the text and paste it into an empty Arduino Sketch)
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:
//oneAfterAnotherNoLoop(); Testing out your own animations:
oneAfterAnotherLoop(); Jump into the included code and start changing
//oneOnAtATime();
//inAndOut(); things. The main point is to turn an LED on use
digitalWrite(pinNumber, HIGH); then to turn
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).
http://ardx.org/CIRC02
11
.:Spin Motor Spin:.
CIRC-03
.:Transistor & Motor:.
THE CIRCUIT:
Parts:
CIRC-03 Transistor
2 Pin Header P2N2222AG (TO92) Wire
Breadboard Sheet
x4 x1
x1
Diode 2.2k Ohm Resistor
Toy Motor (1N4001) Red-Red-Red
x1 x1 x1
gnd
(ground) (-)
+5 volts
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS03
.:view:.
assembly video
http://ardx.org/VIDE03
.:NOTE: if your Arduino is resetting you need to install the optional capacitor:.
12
CODE (no need to type everything in, just click)
CIRC-03
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 }
} }
MAKING IT BETTER
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();
motorAcceleration();
voltage. For example if the Arduino is PWM'ing at 50%
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).
http://ardx.org/CIRC03
13
.:A Single Servo:.
CIRC-04
.:Servos:.
THE CIRCUIT:
Parts:
CIRC-04 3 Pin Header
2 Pin Header
Breadboard Sheet x1 Wire
x4
x1
Mini Servo
x1
Schematic
Arduino
pin 9
Mini Servo
signal
(orange)
+5v
gnd (red)
(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
CODE (no need to type everything in, just click)
CIRC-04
File > Examples > Servo > Sweep
(example from the great arduino.cc site check it out for other great ideas)
// 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); // attaches the servo on pin 9 to the servo object
}
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
}
}
MAKING IT BETTER
Potentiometer control:
void loop() {
We have yet to experiment with inputs but if you would like int pulseTime = 2100; //(the number of microseconds
//to pause for (1500 90 degrees
to read ahead, there is an example program File > // 900 0 degrees 2100 180 degrees)
Examples > Servo > Knob. This uses a potentiometer digitalWrite(servoPin, HIGH);
delayMicroseconds(pulseTime);
(CIRC08) to control the servo. You can find instructions online digitalWrite(servoPin, LOW);
delay(25);
here: http://ardx.org/KNOB }
http://ardx.org/CIRC04
15
CIRC-05 .:8 More LEDs:.
.:74HC595 Shift Register:.
THE CIRCUIT:
Parts:
CIRC-05 Shift Register
2 Pin Header
Breadboard Sheet 74HC595 Wire
x4
x1 x1
560 Ohm Resistor
Red LED Green-Blue-Brown
x8 x8
Schematic
pin pin pin
(560ohm)
4 3 2
resistor
74HC595
LED
(ground) (-)
+5V 0
gnd
1
2
data 3
clock 4
latch
5 There is a half moon
6 cutout, this goes at the top
gnd 7
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS05
.:view:.
assembly video
http://ardx.org/VIDE05
16
CODE (no need to type everything in, just click)
CIRC-05
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 protocol called SPI
//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){
MAKING IT BETTER
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
http://ardx.org/CIRC05
17
.:Music:.
CIRC-06
.:Piezo Elements:.
THE CIRCUIT:
Parts:
CIRC-06
2 Pin Header Piezo Element
Breadboard Sheet Wire
x4 x1
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
CODE (no need to type everything in, just click)
CIRC-06
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() {
for (int i = 0; i < length; i++) {
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; }
// pause between notes
void playTone(int tone, int duration) { delay(tempo / 2); }
for (long i = 0; i < duration * 1000L; i += tone * 2) { }
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
MAKING IT BETTER
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};
http://ardx.org/CIRC06
19
.:Button Pressing:.
CIRC-07
.:Pushbuttons:.
THE CIRCUIT:
Parts:
CIRC-07 Pushbutton
2 Pin Header
Breadboard Sheet x2 Wire
x4
x1
10k Ohm Resistor 560 Ohm Resistor
Brown-Black-Orange Green-Blue-Brown Red LED
x2 x1 x1
Schematic
Arduino
pin 2 pin 3
Arduino +5 volts
pin 13
resistor
LED (10k ohm)
resistor pushbutton
(560ohm)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS07
.:view:.
assembly video
http://ardx.org/VIDE07
20
CODE (no need to type everything in, just click) CIRC-07
File > Examples > 2.Digital > Button
(example from the great arduino.cc site, check it out for other great ideas)
/*
* Button
* by DojoDave
*
* 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
}
}
MAKING IT BETTER
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.
int ledPin = 13; ----> int ledPin = 9;
the other will turn the LED off. Change the code to:
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
} else if (digitalRead(inputPin2) == LOW) { If you would like the LED to fade faster or slower, there is only
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.
http://ardx.org/CIRC07
21
.:Twisting:.
CIRC-08
.:Potentiometers:.
THE CIRCUIT:
Parts:
CIRC-08 Potentiometer Wire
2 Pin Header
Breadboard Sheet 10k ohm
x4
x1 x1
560 Ohm Resistor
Green LED Green-Blue-Brown
x1 x1
Schematic
Arduino +5 volts
pin 13
Arduino
analog
Potentiometer pin 2
LED
(light
emitting
diode)
resistor (560ohm)
(blue-green-brown)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS08
.:view:.
assembly video
http://ardx.org/VIDE08
22
CODE (no need to type everything in, just click) CIRC-08
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
}
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 <sensorValue> milliseconds
}
MAKING IT BETTER
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 1023 (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;
http://ardx.org/CIRC08
23
.:Light:.
CIRC-09
.:Photo Resistors:.
THE CIRCUIT:
Parts:
CIRC-09 Photo-Resistor
2 Pin Header
Breadboard Sheet x1 Wire
x4
x1
10k Ohm Resistor 560 Ohm Resistor
Brown-Black-Orange Green-Blue-Brown Green LED
x1 x1 x1
Schematic
Arduino
+5 volts
pin 13
photo
resistor
LED Arduino
analog
pin 0
resistor resistor
(560ohm) (10k ohm)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS09
.:view:.
assembly video
http://ardx.org/VIDE09
24
CODE (no need to type everything in, just click) CIRC-09
Download the Code from ( http://ardx.org/CODE09 )
(copy the text and paste it into an empty Arduino Sketch)
/*
* A simple program 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-900 to 0-255
//this is simply a raw sensor lightLevel = constrain(lightLevel, 0, 255);
value (relative light) //make sure the value is between 0 and 255
//LED Pin analogWrite(ledPin, lightLevel); //write the value
int ledPin = 9;//the pin the LED is connected to }
//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
MAKING IT BETTER
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 >
Knob. Upload the code to your board and watch as it works
Night light:
Rather than controlling the brightness of the LED in unmodified.
Using the full range of your servo:
response to light, let's instead turn it on or off based on
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 .
http://ardx.org/CIRC09
25
CIRC-10 .:Temperature:.
.:TMP36 Precision Temperature Sensor:.
THE CIRCUIT:
Parts:
CIRC-10
2 Pin Header
Breadboard Sheet
x4
x1
TMP36
Temperature Sensor Wire
x1
Schematic
Arduino
analog +5 volts
pin 0
+5v
TMP36
(precision the chip will have
signal TMP36 printed on it
gnd temperature
sensor)
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS10
.:view:.
assembly video
http://ardx.org/VIDE10
26
CODE (no need to type everything in, just click) CIRC-10
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
MAKING IT BETTER
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);
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;
will appear 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 maths. 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 =
(((temperature - .5) * 100)*1.8) + 32; change the line:
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
menu. You are now transmitting data 12 times faster.
is appearing in the Serial Monitor more informative. To
http://ardx.org/CIRC10
27
.:Larger Loads:.
CIRC-11
.:Relays:.
THE CIRCUIT:
Parts:
CIRC-11 Diode Transistor Relay
Breadboard Sheet (1N4001) P2N2222AG (TO92) (DPDT)
x1 x1 x1 x1
2.2k Ohm Resistor 560 Ohm Resistor
Red-Red-Red Green-Blue-Brown Green LED Red LED
x1 x2 x1 x1
2 Pin Header
x4
Schematic
Arduino
pin 2
resistor
(2.2kohm)
Transistor
Base P2N2222AG
Collector Emitter
the transistor will have
Diode P2N2222AG printed on it
coil
NO NC com
(flyback) (some variations will have
the pin assignment reversed)
+5 volts
gnd
(ground) (-)
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/BBLS11
.:view:.
assembly video
http://ardx.org/VIDE11
28
CODE (no need to type everything in, just click) CIRC-11
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
}
MAKING IT BETTER
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. Arduino
pin 2
Controlling a Motor resistor
In CIRC-03 we controlled a motor using a transistor. However if you want to control a larger motor a (2.2kohm)
relay is a good option. To do this simply remove the red LED, and connect the motor in its place
Transistor
(remember to bypass the 560 Ohm resistor) Base P2N2222AG
Collector Emitter
Controlling Motor Direction
A bit of a complicated improvement to finish. To control the direction of spin of
a DC motor we must be able to reverse the direction of current flow through it. Diode
coil
NO NC com
(flyback)
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 +5 volts
the motor's direction, wire up the following circuit. It looks complicated but can gnd
be accomplished using only a few extra wires. Give it a try. (ground) (-)
http://ardx.org/CIRC11
29
.:Colorful Light:.
CIRC-12
.:RGB LEDs:.
The RGB LED actually has 3 LED elements inside it - Red, Green &
Blue. Let’s turn these on in combinations to get some brilliant colours out of a single LED!
So you can see what is going on, this circuit has an LED tied into each channel so you can
see exactly what the different signals are doing to the RGB LED.
THE CIRCUIT:
Parts:
CIRC-RGB 5mm RGB LED
2 Pin Header
Breadboard Sheet x1 Wire
x4
x1
560 Ohm Resistor Red LED
Green-Blue-Brown x3
x6
Schematic
RGB LED resistors
(560ohm)
common (gnd)
pin 11
blue
Arduino
gnd green
pin 10
green
blue
red
red
pin 9
blue
indicator
gnd
green
flat
indicator side
gnd
red
indicator
gnd
The Internet
.:download:.
breadboard layout sheet
http://ardx.org/src/circ/
CIRC12-sheet-SOLA.png
30
CODE (no need to type everything in, just click)
CIRC-12
Download the Code from (http://ardx.org/CODE12R)
(copy the text and paste it into an empty Arduino Sketch)
MAKING IT BETTER
More Colors Analog Color Control
I imagine you are less than impressed by the While switching between colors is good fun RGB LEDs
cyan glowing LED before you. To display a really come into their own when mixed with analog
different color change the color in the code to control. Using PWM (pulse width modulation) it’s
one of the others. possible to produce nearly any color and fade
setColor(ledAnalogOne, CYAN); ----> between them. Sadly the code for this is a bit too long
setColor(ledAnalogOne, **NEW COLOR**);
for the section above, for an example program (with
Display a Random Color lots of comments).
Of course we can do more than display a
constant color, to see how we cycle through Download the code from:
random colors change the loop() code to. http://bit.ly/PxDkmH
void loop(){
//setColor(ledAnalogOne, CYAN);
randomColor()
}
http://www.solarbotics.com
31
.:Notes:.
32
33
ARDX
Other products from Solarbotics: arduino
experimentation kit
vo
Ser
0
92
28
352
38
D W
KAR
www.solarbotics.com
www.oomlout.com
This work is licenced under the Creative Commons Attribution-Share Alike 3.0 Unported License. To view a copy of this
licence, 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.