www.arduino.cc (it's free!). is software, known as the Arduino. IDE, will allow you ..... This is an illustration of how
0
~3
1 RX
7
2 TX
~6 ~5 4
8
)
SC SC L AR A E GN F D 13 1 ~1 2 ~1 1 0 ~9 8
9
~
V
~
N
D 7 IG 6 IT ~ AL ~5 (P 4 W 3 M ~
O
Version 3.2
EF
10
.
K
C
~
11
A
H
~
12
.
P
ET
T
13
R
TX
A
A
H
L SC A SD EF AR ND G 13
RX
E
L
.
N
S
E
R
IS
ES
SE
N
ER VI
IOR
RE
W D
IN
V
V
3V
T 3.
SE
GN
PO D
AN
A5
G
OR
M
ON
15
15
F RE
RE
GN
A1
A4
AL O A3
1 3for the SparkFun RedBoard Your guide to the SparkFun Inventor’s Kit
RX TX
TF
LA
EN
PM
LO
VE
DE
GE
UA
NG
LA
INO
DU
DIG
I TA
L( PW M~ )
AR
TP
7-
7-
IO
5V
A0
A2
T
SIK GUIDE SE
RE
Table of Contents Welcome to the SparkFun Inventor’s Guide The SparkFun Inventor's Guide is your map for navigating the waters of beginning embedded electronics. This booklet contains all the information you will need to explore the 16 circuits of the SparkFun Inventor's Kit for Educators. At the center of this manual is one core philosophy - that anyone can (and should) play around with electronics. When you're done with this guide, you'll have the know-how to start creating your own projects and experiments. Now enough talking - let's get inventing!
www.sparkfun.com
Section 1:
Getting Started What is the RedBoard platform?
2
Download Arduino Software (IDE)
4
Install Drivers
5
Select your board: Arduino Uno
8
Download “SIK Guide Code”
9
Section 2:
Getting Started with Circuits The World Runs on Circuits
10
Inventory of Parts
12
RedBoard
14
Breadboard
16
Circuit #1 - Your First Circuit: Blinking a LED
18
Circuit #2 - Potentiometer
25
Circuit #3 - RGB LED
29
Circuit #4 - Multiple LEDs
33
Circuit #5 - Push Buttons
37
Circuit #6 - Photo Resistor
41
Circuit #7 - Temperature Sensor
45
Circuit #8 - A Single Servo
49
Circuit #9 - Flex Sensor
53
Circuit #10 - Soft Potentiometer
57
Circuit #11 - Piezo Buzzer
61
Circuit #12 - Spinning a Motor
65
Circuit #13 - Relay
69
Circuit #14 - Shift Register
73
Circuit #15 - LCD
77
Circuit #16 - Simon Says
81 Page 1
What is the RedBoard platform? The DIY Revolution We live in a unique time where we have access to resources that allow us to create our own solutions and inventions. The DIY revolution is composed of hobbyists, tinkerers and inventors who would rather craft their own projects than let someone do it for them.
www.sparkfun.com
A Computer for the Physical World The RedBoard in your hand (or on your desk) is your development platform. At its roots, the RedBoard is essentially a small portable computer. It is capable of taking inputs (such as the push of a button or a reading from a light sensor) and interpreting that information to control various outputs (like a blinking LED light or an electric motor). That's where the term "physical computing" is born this board is capable of taking the world of electronics and relating it to the physical world in a real and tangible way. Trust us - this will all make more sense soon.
RedBoard
7-15V
IOREF
FPO FPO
13 TX RX
The SparkFun RedBoard is one of a multitude of development boards based on the ATmega328. It has 14 digital input/output pins (6 of which can be PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ISP header, and a reset button. Don’t worry, you’ll learn about all these later.
RESET
// SparkFun RedBoard
RESET 5V GND VIN
POWER
GND
A0 A1 A3
Page 2
ISP
A5
ON
A4
ANALOG IN
A2
DIGITAL (PWM~)
LEARN. SHARE. HACK.
3.3V
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8 7 ~6 ~5 4 ~3 2 TX 1 RX 0
c b
a
d
h
RedBoard
e
g
f a
Bug Zapper Counter
d
Camera Time-lapse operation
g
Auto-Coffee Maker
b
Old Toy Email Notifer
e
Auto-Plant Watering
h
Quadcopter
c
Power-Lacing High Tops
f
Re-Programmed Traffic Light Page 3
Download the Arduino IDE (Integrated Development Environment) Access the Internet
NW
N
In order to get your RedBoard up and running, you'll need to download the newest version of the Arduino software first from www.arduino.cc (it's free!). This software, known as the Arduino IDE, will allow you to program the board to do exactly what you want. It’s like a word processor for writing programs. With an internet-capable computer, open up your favorite browser and type in the following URL into the address bar:
NE
W
E SE
S
SW
arduino.cc/en/main/software
1
Click on your appropriate computer
operating system next to the “ + ” sign. Windows
Download
Windows Mac OS X
Mac OS X Linux: 32 bit, 64 bit
Linux:source 32 bit, 64 bit
user •••••••••
Choose the appropriate Operating System installation package for your computer. Page 4
// Connect your RedBoard to your Computer Use the USB cable provided in the SIK kit to connect the RedBoard to one of your computer’s USB inputs.
715 V
IO F
RE
RE
3V
T
3.
SE
5V
T
N
ER
VI
W
D
SE
GN
PO
D
RE
GN
A0 A1
13
N .
.
10
K
~
C
11
A
~
H
12
.
E
R
A
H
S
IN
A5
G
L SC A SD EF AR ND G 13
TX
E
L
RX
R
A
O
A4
AL
A3
AN
A2
~ 9
P
8
O N TX RX
D 7 IG 6 TI A ~ 5 L ~ 4 (P W 3 M ~ ~ 2 ) 1
IS
0
3
2
// Install Arduino Drivers Depending on your computer’s operating system, you will need to follow specific instructions. Please go to www.sparkfun.com/FTDI for specific instructions on how to install the FTDI drivers onto your RedBoard.
Page 5
// Open the Arduino IDE: Open the Arduino IDE software on your computer. Poke around and get to know the interface. We aren’t going to code right away, this is just an introduction. This step is to set your IDE to identify your RedBoard.
1
2
7
8
9
Page 6
3
4
5
6
GUI (Graphical User Interface) 1 Verify: Compiles and approves your code. It will catch errors in syntax (like missing semi-colons or parenthesis).
// See Diagram Below
2 Upload: Sends your code to the RedBoard. When you click it, you should see the lights on your board blink rapidly. // See Diagram Below 3 New: This buttons opens up a new code window tab. 4 Open: This button will let you open up an existing sketch. // See Diagram Below 5 Save: This saves the currently active sketch. 6 Serial Monitor: This will open a window that displays any serial information your RedBoard is transmitting. It is very useful for debugging. 7 Sketch Name: This shows the name of the sketch you are currently working on. 8 Code Area: This is the area where you compose the code for your sketch. 9 Message Area: This is where the IDE tells you if there were any errors in your code.
// The three most important commands for this guide are seen below:
Open
Verify
Upload
Page 7
4
// Select your board: Arduino Uno
Note:
// Select your Serial Device
Your SparkFun RedBoard and the Arduino UNO are interchangeable but you won’t find the RedBoard listed in the Arduino Software. Select “Arduino UNO” instead.
Select the serial device of the RedBoard from the Tools | Serial Port menu. This is likely to be com3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your RedBoard and re-open the menu; the entry that disappears should be the RedBoard. Reconnect the board and select that serial port.
Select the serial device of the RedBoard from the Tools > Serial Port menu. On the Mac, this should be something with /dev/tty.usbmodem or /dev/tty.usbserial in it.
http://www.arduino.cc/playground/Learning/Linux
Page 8
Download Arduino Code (For use with the circuits in this guide)
5 Type in the following URL to download the code:
// Copy “SIK Guide Code” into “Examples” library in Arduino folder
sparkfun.com/sikcode
Start
Unzip the file “SIK Guide Code”. It should be located in your browser’s “Downloads” folder. Right click the zipped folder and choose “unzip”.
Programs
arduino
examples
Copy the “SIK Guide Code” folder into Arduino’s folder named “examples”.
Contents Resources
Unzip the file “SIK Guide Code”. It should be loacted in your browser’s “Downloads” folder. Right click the zipped folder and choose “unzip”.
Find “Arduino” in your applications folder. Right click(ctrl + click) on “Arduino”. Select “Show Package Contents”.
Java examples Copy the “SIK Guide Code” folder into Arduino’s folder named “examples”.
http://www.arduino.cc/playground/Learning/Linux
WHAT’S NEXT? Read on to learn more about getting started with circuits. Then you can start on your first circuit on page 17! Page 9
Getting Started with Circuits What is an Electrical Circuit? A circuit is basically an electrical loop with a starting point and an ending point - with any number of components in between. Circuits can include resistors, diodes, inductors, sensors of all sizes and shapes, motors, and any other handful of hundreds of thousands of components. Circuits are usually divided into three categories - analog circuits, digital circuits, or mixed-signal circuits. In this guide, you will explore all three sets of circuits.
The World Runs on Circuits: Everywhere you look, you'll find circuits. The cell phone in your pocket, the computer that controls your car's emissions system, your video game console - all these things are chock full of circuits. In this guide, you'll experiment with some simple circuits and learn the gist of the world of embedded electronics.
// Simple and Complex Circuits In this guide, you will be primarily exploring simple circuits - but that doesn't mean you can't do amazing things with simple tools! When you've finished the SIK, your knowledge of circuits will enable you to explore amazing projects and unleash the power of your imagination.
Page 10
a b c d e 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
f g h i
f g h i
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 11
Inventory of Parts LED (5mm)
Jumper Wire
x30
x5
330Ω Resistor
x25
x5
x5
x5
* ACTUAL SIZE
x1
* ACTUAL SIZE
x25
Diode
(1N4148)
x1
* ACTUAL SIZE
x2
Piezo Buzzer
Photo Resistor
x1
x1
Transistor
(TMP36)
P2N2 222A A18
Temp. Sensor
(P2N2222AG)
FRONT
x1
DC Motor
FRONT
BACK
x2
Push Button
x1
+
10KΩ Resistor
Potentiometer
Page 12
-
(Light Emitting Diode)
Various Colors
x4
BACK
Flex Sensor
SparkFun RedBoard
RESET
x1
7-15V
Soft Potentiometer 13 TX RX
IOREF RESET 5V GND VIN
POWER
GND
A0 A1 A2 A3
ISP
A5
7 ~6 ~5 4 ~3 2 TX 1 RX 0
ON
A4
ANALOG IN
x1
DIGITAL (PWM~)
LEARN. SHARE. HACK.
3.3V
Servo
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
x1
Breadboard
Standard Solderless (Color may vary)
x1 a b c d e
Integrated Circuit
Relay
(IC)
x1
x1
LCD
x1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
f g h i
f g h i
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
x1 Page 13
RESET
2 1
8
7-15V
13 TX RX
IOREF RESET 5V
10
VIN
POWER
GND
A0 A1 A3
Page 14
ISP
A5
7 9
ON
A4
ANALOG IN
A2
DIGITAL (PWM~)
LEARN. SHARE. HACK.
3.3V GND
SCL SDA AREF 3 4 5 GND 13 12 ~11 ~10 ~9 8 7 ~6 ~5 4 ~3 2 TX 1 RX 0
6
SparkFun RedBoard 1 Power In (Barrel Jack) - Can be used with either a 9V or 12V wall-wart or battery. 2 Power In (USB Port) - Provides power and communicates with your board when plugged into your computer via USB. LED (RX: Receiving) - This shows when the FTDI chip is receiving bottom" resistor. Please note the fixed resistor isn't necessarily the bottom resistor. We do that with the photodiode only so that more light = more voltage, but it could be flipped and we'd get the opposite response.
A voltage divider consists of two resistors. The "top" resistor is the sensor you'll be using. The "bottom" one is a normal, fixed resistor. When you connect the top resistor to 5 volts, and the bottom resistor to ground. The voltage at the middle will be proportional to the bottom resistor relative to the total resistance (top resistor + bottom resistor). When one of the resistors changes (as it will when your sensor senses things), the output voltage will change as well!
5 volts
The RedBoard's analog input pins measure voltage, not resistance. But we can easily use resistive sensors with the RedBoard by including them as part of a "voltage divider".
Many of the sensors you'll use (potentiometers, photoresistors, etc.) are resistors in disguise. Their resistance changes in proportion to whatever they're sensing (light level, temperature, sound, etc.).
Measuring resistive sensors:
6
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 6
Code to Note: lightLevel = map(lightLevel, 0, 1023, 0, 255);
When we read an analog signal using analogRead(), it will be a number from 0 to 1023. But when we want to drive a PWM pin using analogWrite(), it wants a number from 0 to 255. We can "squeeze" the larger range into the smaller range using the map() function.
Parameters map(value, fromLow, fromHigh, toLow, toHigh) value: the number to map fromLow: the lower bound of the value's current range fromHigh: the upper bound of the value's current range toLow: the lower bound of the value's target range toHigh: the upper bound of the value's target range
See http://arduino.cc/en/reference/map for more info.
lightLevel = constrain(lightLevel, 0, 255);
Because map() could still return numbers outside the "to" range, we'll also use a function called constrain() that will "clip" numbers into a range. If the number is outside the range, it will make it the largest or smallest number. If it is within the range, it will stay the same.
Parameters constrain(x, a, b) x: the number to constrain, all data types a: the lower end of the range, all data types b: the upper end of the range, all data types
See http://arduino.cc/en/reference/constrain for more info.
What You Should See: V 15 7-
F RE IO SET RE V 3 3.
N VI
RE SE T
P 5V D O W GN D E R GN
A0 A1
AR LE
N
.S
H
IN .H
AC
K.
IS P
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ 5 A L ~ 4 (P W 3 M ~ ~ 2 ) 1
E AR
A5
TX
L SC A SD EF AR ND G 13
A4
RX
13 R1X3 T TX RX X
A3
AN AL O G
A2
You should see the LED grow brighter or dimmer in accordance with how much light your photoresistor is reading. If it isn't working, make sure you have assembled the circuit correctly and verified and uploaded the code to your board or see the troubleshooting tips below.
0
Troubleshooting:
Real World Application:
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 up and give it a twist.
A street lamp uses a light sensor to detect when to turn the lights on at night.
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 it’s in the right place. 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. Page 44
Circuit 2 CIRCUIT #7
Temperature Sensor 5 volt
A temperature sensor is exactly what it sounds like – a sensor used to measure ambient temperature. This particular sensor has three pins – a positive, a ground, and a signal. This is a linear temperature sensor. A change in temperature of one degree centigrade is equal to a change of 10 millivolts at the sensor output. The TMP36 sensor has a nominal 750 mV at 25°C (about room temperature). In this circuit, you’ll learn how to integrate the temperature sensor with your RedBoard, and use the Arduino IDE’s serial monitor to display the temperature.
TMP36 (Precision Temperature Sensor) 1
VCC VOUT 2
3
PIN A0
GND
RedBoard
PARTS:
GND (Ground)
Temp. Sensor
7
When you’re building the circuit be careful not to mix up the transistor and the temperature sensor, they’re almost identical. Look for “TMP” on the body of the temperature sensor.
Wire X
1
X
5
GND
SIGNAL +V
FRONT +V
SIGNAL GND
BACK
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
RE
GN GN
A0 A1
L SC A SD EF AR ND G 13
RX
A
TX
E
L
13
.
~
.
10
K
11
C
A
~
H
12
.
E
R
A
H
S
IN
A5
G
N
R
A4
AL O
A3
AN
A2
~ 9
P
8
D 7 I 6 IG T ~ AL ~5 4 W 3 M ~ ~ 2
IS
(P
O N
) TX
1
RX
0
p.44 Page 45
Page 46
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 7: Temperature Sensor
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 47
GND
A0
Jumper Wire
GND SIGNAL +V
5V
Image Reference:
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Temperature Sensor
Component:
-
j7
j5
+
j6
+
V OUT +
f5 f6 f7
1
3
2
This circuit uses the Arduino IDE's serial monitor. To open this, first upload the program then click the button which looks like a magnifying glass in a square. In order for the serial monitor to operate correctly it must be set to the same baud rate (speed in bits per second) as the code you're running. This code runs at 9600 baud; if the baud rate setting is not 9600, change it to 9600.
Opening your serial monitor:
7
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 7
Code to Note: Before using the serial monitor, you must call Serial.begin() to initialize it. 9600 is the "baud rate", or communications speed. When two devices are communicating with each other, both must be set to the same speed.
Serial.begin(9600);
The Serial.print() command is very smart. It can print out almost anything you can throw at it, including variables of all types, quoted text (AKA "strings"), etc.
Serial.print(degreesC);
See http://arduino.cc/en/serial/print for more info.
Serial.print() will print everything on the same line. Serial.println() will move to the next line. By using both of these commands together, you can create easy-to-read printouts of text and data.
Serial.println(degreesF);
What You Should See:
V 15 7-
F RE IO SET RE V 3 3.
L SC A SD EF AR ND G 13
LE
AR
N
.S
H
AR
H
AC
K.
IS P
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
E.
IN
A5
X
13 R1X3 T TX RX X
A4
RX
RE SE T
A0
A1
AN AL O G
A3
deg C: 23.24 deg F: 73.84 deg C: 23.24 deg F: 73.84 deg C: 23.24 deg F: 73.84 deg C: 22.75 deg F: 72.96 deg C: 23.24 deg F: 73.84 deg C: 23.24 deg F: 73.84 deg C: 23.24 deg F: 73.84 deg C: 22.75 deg F: 72.96 deg C: 23.24 deg F: 73.84 deg C: 22.75 deg F: 72.96 deg C: 22.75 deg F: 72.96 deg C: 23.24 deg F: 73.84 deg C: 22.75 deg F: 72.96 deg C: 23.24 T deg F: 73.84
P 5V D O W GN D E R GN N VI
voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 voltage: 0.73 A2
You should be able to read the temperature your temperature sensor is detecting on the serial monitor in the Arduino IDE. If it isn't working, make sure you have assembled the circuit correctly and verified and uploaded the code to your board or see the troubleshooting tips below.
0
Troubleshooting:
Real World Application:
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).
Building climate control systems use a temperature sensor to monitor and maintain their settings.
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". Temperature Value is Unchanging Try pinching the sensor with your fingers to heat it up or pressing a bag of ice against it to cool it down. Page 48
Circuit 2 CIRCUIT #8
A Single Servo
Servos are ideal for embedded electronics applications because they do one thing very well that motors cannot – they can move to a position accurately. By varying the pulse width of the output voltage to a servo, you can move a servo to a specific position. For example, a pulse of 1.5 milliseconds will move the servo 90 degrees. In this circuit, you’ll learn how to use PWM (pulse width modulation) to control and rotate a servo.
No junction dot means no connection 5 volt
W
SIG
PIN 9
R
V+
RedBoard
B
GND
8
PARTS:
GND (Ground)
Servo
Wire X
1
X
8
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
RE
GN GN
A0 A1
L SC A SD EF AR ND G 13
RX
A
TX
E
L
13
.
~
.
10
K
11
C
A
~
H
12
.
E
R
A
H
S
IN
A5
G
N
R
A4
AL O
A3
AN
A2
~ 9
P
8
D 7 I 6 IG T ~ AL ~5 4 W 3 M ~ ~ 2
IS
(P
O N
) TX
1
RX
0
Page 49
Page 50
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 8: A Single Servo
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 51
5V GND
Jumper Wire
a6
Jumper Wire
Jumper Wire
b5
Jumper Wire
+
+
e7
Jumper Wire Pin 9
e6
Jumper Wire
Jumper Wire
e5
Jumper Wire
a7
e5 e6 e7
Image Reference:
Servo
Component:
Tools
Help
Show Sketch Folder Add File... Import Library
Verify / Compile
Edit Sketch
EEPROM Ethernet Firmata LiquidCrystal SD Servo SoftwareSerial SPI Stepper Wire
After importing the library into your code, you will have access to a number of pre-written commands and functions. More information on how to use the standard library functions can be accessed at: http://arduino.cc/en/Reference/Libraries.
File
To use a library in a sketch, select it from Sketch > Import Library.
But anyone can create a library, and if you want to use a new sensor or output device, chances are that someone out there has already written one that interfaces that device to the RedBoard. Many of SparkFun's products come with Arduino libraries, and you can find even more using Google and the Arduino Playground at http://arduino.cc/playground/. When YOU get the RedBoard working with a new device, consider making a library for it and sharing it with the world!
See http: //arduino.cc/en/reference/libraries for a list of the standard libraries and information on using them.
Libraries are collections of new commands that have been packaged together to make it easy to include them in your sketches. Arduino comes with a handful of useful libraries, such as the servo library used in this example, that can be used to interface to more advanced devices (LCD displays, stepper motors, ethernet ports, etc.)
The Arduino development environment gives you a very useful set of built-in commands for doing basic input and output, making decisions using logic, solving math problems, etc. But the real power of Arduino is the huge community using it and their willingness to share their work.
Expand your horizons using Libraries:
8
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 8
Code to Note: #include is a special "preprocessor" command that inserts a library (or any other file) into your sketch. You can type this command yourself, or choose an installed library from the "sketch / import library" menu.
#include
Servo servo1; servo1.attach(9);
The servo library adds new commands that let you control a servo. To prepare the Arduino to control a servo, you must first create a Servo "object" for each servo (here we've named it "servo1"), and then "attach" it to a digital pin (here we're using pin 9). V 15 7-
3V 3.
F RE IO SET RE
The servos in this kit don't spin all the way around, but they can be commanded to move to a specific position. We use the servo library's write() command to move a servo to a specified number of degrees(0 to 180). Remember that the servo requires time to move, so give it a short delay() if necessary. RE SE T
P 5V D O W GN D E R GN N VI
servo1.write(180);
A0
A1
LE
N
.S
IN
AR H
AC H
K.
IS P
O N TX
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ AL ~5 P( W 4 3 M ~ ~ 2 ) 1
E.
A5
AR
A4
RX
L SC A SD EF AR ND G 13
A3
0
You should see your servo motor move to various locations at several speeds. If the motor doesn't move, check your connections and make sure you have verified and uploaded the code, or see the troubleshooting tips below.
13 R1X3 T TX RX X
AN AL O G
A2
What You Should See:
Troubleshooting:
Real World Application:
Servo Not Twisting Even with colored wires it is still shockingly easy to plug a servo in backward. This might be the case.
Robotic arms you might see in an assembly line or sci-fi movie probably have servos in them.
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. Fits and Starts If the servo begins moving then twitches, and there's a flashing light on your RedBoard, the power supply you are using is not quite up to the challenge. Using a wall adapter instead of USB should solve this problem. Page 52
CIRCUIT #9 Flex Sensor
In this circuit, we will use a flex sensor to measure, well, flex! A flex sensor uses carbon on a strip of plastic to act like a variable resistor, but instead of changing the resistance by turning a knob, you change it by flexing (bending) the component. We use a "voltage divider" again 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.
5 volt
Resistor (10K ohm) (Brown-Black-Orange) W
SIG
PIN 9
R
V+
PIN A0
RedBoard
B
GND
Flex Sensor
PARTS:
GND (Ground)
Flex IC Sensor Potentiometer
Servo LED
330Ω10KΩ Resistor Resistor
11
X X
X
8
X
1
9
Wire X
8 X1
X
11
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
GN
RE
GN
A0 A1
L SC A SD EF AR ND G 13
13
. A
H
S
IN
A5
G
N
R
A
TX
E
RX
L
A4
AL O
A3
AN
A2
R
~
.
10
K
11
C
A
~
H
12
E .
~ 9
P
8
O N TX RX
D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
IS
0
Page 53
Page 54
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 9: Flex Sensor
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 55
e3
h19 h20 i20 i24 i19
Jumper Wire
Flex Sensor
10KΩ Resistor
Jumper Wire
GND
+
5V
Jumper Wire
Jumper Wire
a3
a2 + h24
Jumper Wire Pin 9
b1 + h24
Jumper Wire
Jumper Wire
h24 +
Jumper Wire
j20
e2
Jumper Wire
A0
e1
Jumper Wire
Jumper Wire
e1 e2 e3
Image Reference:
Servo
Component:
for (x = 1 ; x < 9 ; x++) { Serial.print(x); }
12345678
And if you run the code again, you'll see the output you wanted:
01234567
Let's say you wanted a for() loop from 1 to 8, but your code just doesn't seem to be working right. Just add Serial.begin(9600); to your setup() function, and add a Serial.print() or println() to your loop:
You wanted 1 to 8, but the loop is actually giving you 0 to 7. Whoops! Now you just need to fix the loop.
for (x = 0; x < 8; x++) { Serial.print(x); }
The key to visibility into a microcontroller is output. This can be almost anything, including LEDs and buzzers, but one of the most useful tools is the serial monitor. Using Serial.print() and println(), you can easily output human-readable text and data from the RedBoard to a window back on the host computer. This is great for your sketch's final output, but it's also incredibly useful for debugging.
It happens to everyone - you write a sketch which successfully compiles and uploads, but you can't figure out why it's not doing what you want it to. Larger computers have screens, keyboards, and mice that you can use to debug your code, but tiny computers like the RedBoard have no such things.
Debugging your sketches using the Serial Monitor:
9
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 9
Code to Note:
servoposition = map(flexposition, 600, 900, 0, 180); map(value, fromLow, fromHigh, toLow, toHigh)
Because the flex sensor / resistor combination won't give us a full 0 to 5 volt range, we're using the map() function as a handy way to reduce that range. Here we've told it to only expect values from 600 to 900, rather than 0 to 1023.
servoposition = constrain(servoposition, 0, 180); constrain(x, a, b)
Because map() could still return numbers outside the "to" range, we'll also use a function called constrain() that will "clip" numbers into a range. If the number is outside the range, it will make it the largest or smallest number. If it is within the range, it will stay the same.
What You Should See: V 15 7-
F RE IO SET RE V 3 3.
N VI
RE SE T
P 5V D O W GN D E R GN
A0 A1
LE
.S N
IN
H
AR
K. AC
IS P
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
H E.
A5
AR
A4
TX
L SC A SD EF AR ND G 13
A3
RX
13 R1X3 T TX RX X
AN AL O G
A2
You should see the servo motor move in accordance with how much you are flexing the flex sensor. If it isn't working, make sure you have assembled the circuit correctly and verified and uploaded the code to your board or see the troubleshooting tips below.
0
Troubleshooting:
Real World Application:
Servo Not Twisting Even with colored wires it is still shockingly easy to plug a servo in backwards. This might be the case.
Controller accessories for video game consoles like Nintendo’s “Power Glove” use flex-sensing technology. It was the first video game controller attempting to mimic hand movement on a screen in real time.
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 out on a convex curve). Servo Doesn’t Move very Far You need to modify the range of values in the call to the map() function.
Page 56
Circuit 2 CIRCUIT #5 #10
Soft Potentiometer RedBoard
In this circuit, we are going to use yet another kind of variable resistor – this time, a soft potentiometer (or soft pot). This is a thin and flexible strip that can detect where pressure is being applied. By pressing down on various parts of the strip, you can vary the resistance from 100 to 10K ohms. You can use this ability to track movement on the soft pot, or simply as a button. In this circuit, we’ll get the soft pot up and running to control an RGB LED.
PIN 11 PIN 10
5 volt RedBoard
PIN 9 Resistors (330 ohm) (Orange-Orange-Brown)
Soft Pot
PIN A0
RED
GREEN
BLUE
Resistor (10K ohm) (Brown-Black-Orange)
10
LED (Light-Emitting Diode)
PARTS:
GND (Ground)
LED IC
330Ω LED Soft Potentiometer Resistor X
1
X
1
X
330Ω Resistor
Wire 330Ω Resistor 318
9
X
X
10KΩ Resistor
8
X
3
X
1
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
GN
RE
GN
A0 A1
L SC A SD EF AR ND G 13
13
. A
H
S
IN
A5
G
N
R
A
TX
E
RX
L
A4
AL O
A3
AN
A2
R
~
.
10
K
11
C
A
~
H
12
E .
~ 9
P
8
O N TX RX
D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
IS
0
Page 57
Page 58
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 10: Soft Potentiometer
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 59
Jumper Wire
Jumper Wire A0
Pin 11
Jumper Wire
Jumper Wire
Pin 10
Jumper Wire
Jumper Wire
Jumper Wire
j20
j19
j18
h7
h6
e5
h4
+
g71 e7 e1
5V
330Ω Resistor
Pin 9
e6 e1 g61 e7
5V
330Ω Resistor
i19
e4 e1 g41 e7
5V
330Ω Resistor
10KΩ Resistor
h18 h19 h20
a4 a5 a6 a7
5V
Image Reference:
Soft Potentiometer
RGB LED (5mm)
Component:
Jumper Wire
Jumper Wire
Component:
Image Reference:
GND
5V
+
10
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 10
Code to Note: These big, scary functions take a single Value (RGBposition) and calculate the three RGB values necessary to create a rainbow of color. The functions create three "peaks" for the red, green, and blue values, which overlap to mix and create new colors. See the code for more information! Even if you're not 100% clear how it works, you can copy and paste this (or any) function into your own code and use it yourself. If you want to know more about creating your own functions - take a look at circuit #11.
redValue = constrain(map(RGBposition, 0, 341, 255, 0), 0, 255) + constrain(map(RGBposition, 682, 1023, 0, 255), 0, 255);
greenValue = constrain(map(RGBposition, 0, 341, 0, 255), 0, 255) - constrain(map(RGBposition, 341, 682, 0,255), 0, 255);
blueValue = constrain(map(RGBposition, 341, 682, 0, 255), 0, 255) - constrain(map(RGBposition, 682, 1023, 0, 255), 0, 255);
What You Should See:
V 15 7-
You should see the RGB LED change colors in accordance with how you interact with the soft potentiometer. If it isn't working, make sure you have assembled the circuit correctly and verified and uploaded the code to your board, or see the troubleshooting tips below.
3V 3.
F RE IO SET RE
RE SE T
PO 5V W D GN D E R GN N VI A0 A1
LE
.S N
IN
H
A5
AR
A4
L SC A SD EF AR ND G 13
A3
AR
E.
H
K AC
.
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ AL ~5 (P 4 W 3 M ~ ~ 2 ) 1
IS P
TX
13 R1X3 T TX RX X
AN AL O G
A2
RX
0
Troubleshooting:
Real World Application:
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.
The knobs found on many objects, like a radio for instance, are using similar concepts to the one you just completed for this circuit.
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.
Page 60
Circuit 2 CIRCUIT #11
Piezo Buzzer
11
In this circuit, we'll again bridge the gap between the digital world and the analog world. We'll be using a buzzer that makes a small "click" when you apply voltage to it (try it!). By itself that isn't terribly exciting, but if you turn the voltage on and off hundreds of times a second, the buzzer will produce a tone. And if you string a bunch of tones together, you've got music! This circuit and sketch will play a classic tune. We'll never let you down!
Piezo Buzzer RedBoard PIN 9
PARTS:
GND (Ground)
Piezo Buzzer
Wire X
1
3
X
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
GN
RE
GN
A0 A1
13
R N . A
H
S
IN
A5
G
L SC A SD EF AR ND G 13
A
LO
A4
TX
E
RX
L
A
A3
AN
A2
R
~
.
10
K
11
C
A
~
H
12
E .
~ 9
P
8
O N TX RX
D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
IS
0
If the buzzer doesn't easily fit into the holes on the breadboard, try rotating it slightly.
Page 61
Page 62
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 11: Piezo Buzzer
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 63
Pin 9 GND
Jumper Wire
Jumper Wire
i7
Jumper Wire
+
f9
Image Reference:
Piezo Buzzer
Component:
j9
-
f7
return(x);
When you write your own functions, you make your code neater and easier to re-use. See http://arduino.cc/en/reference/functiondeclaration for more information about functions.
If your function is returning a value from your function, put the type of the return value in front of the function name. Then in your function, when you're ready to return the value, put in a return(value) statement. If you won't be returning a value, put "void" in front of the function name (similar to the declaration for the setup() and loop() functions).
If you'll be passing parameters to your function, put them (and their types) in the parentheses after the function name. If your function is not using any parameters, just use an empty parenthesis () after the name.
Your functions can take in values ("parameters"), and return a value, as this one does.
}
x = parameter1 + parameter2;
int add(int parameter1, int parameter2) { int x;
Arduino contains a wealth of built-in functions that are useful for all kinds of things. (See http://arduino.cc/en/reference for a list). But you can also easily create your own functions. First, we need to declare a function. Here's a simple example named "add," which adds two numbers together and returns the result. Let's break it down.
Creating your own functions:
11
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 11
Code to Note:
Up until now we've been working solely with numerical data, but the Arduino can also work with text. Characters (single, printable, letters, numbers and other symbols) have their own type, called "char". When you have an array of characters, it can be defined between double-quotes (also called a "string"), OR as a list of single-quoted characters.
char notes[] = "cdfda ag cdfdg gf "; char names[] = {'c','d','e','f','g','a','b','C'};
One of Arduino's many useful built-in commands is the tone() function. This function drives an output pin at a certain frequency, making it perfect for driving buzzers and speakers. If you give it a duration (in milliseconds), it will play the tone then stop. If you don't give it a duration, it will keep playing the tone forever (but you can stop it with another function, noTone() ).
tone(pin, frequency, duration);
What You Should See: V 15 7-
F RE IO SET RE V 3 3.
5V
PO
RE SE T
D W GN D E R GN
N VI A0 A1
LE
H .S
IN .H
AC
K.
IS P
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
E AR
A5
N AR
A4
TX
L SC A SD EF AR ND G 13
A3
RX
13 R1X3 T TX RX X
AN AL O G
A2
You should see - well, nothing! But you should be able to hear a song. If it isn't working, make sure you have assembled the circuit correctly and verified and uploaded the code to your board or see the troubleshooting tips below.
0
Troubleshooting:
Real World Application:
No Sound Given the size and shape of the piezo buzzer it is easy to miss the right holes on the breadboard. Try double checking its placement.
Many modern megaphones have settings that use a loud amplified buzzer. They are usually very loud and quite good at getting people’s attention.
Can't Think While the Melody is Playing Just pull up the piezo buzzer whilst you think, upload your program then plug it back in. Feeling Let Down and Deserted The code is written so you can easily add your own songs.
Page 64
12
Circuit 2 CIRCUIT #12
Spinning a Motor 5 volt
Diode (1N4148)
Remember before when you played around with a servo motor? Now we are going to tackle spinning a motor. This requires the use of a transistor, which can switch a larger amount of current than the RedBoard can. When using a transistor, you just need to make sure its maximum specs are high enough for your use. The transistor we are using for this circuit is rated at 40V max and 200 milliamps max – perfect for our toy motor! When the motor is spinning and suddenly turned off, the magnetic field inside it collapses, generating a voltage spike. This can damage the transistor. To prevent this, we use a "flyback diode", which diverts the voltage spike around the transistor.
DC Motor
RedBoard PIN 9
Transistor (P2N2222AG)
Resistor (330 ohm) (Orange-Orange-Brown)
Transistor
Diode
P2N2222AG
X
When you’re building the circuit be careful not to mix up the transistor and the temperature sensor, they’re almost identical. Look for “P2N2222A” on the body of the transistor.
DC Motor
1N4148
1
X
1
Wire X
1
330Ω Resistor X
6
X
1
EMITTER BASE
P2N2 222A
PARTS:
GND (Ground)
COLLECTOR
FRONT COLLECTOR BASE EMITTER
BACK 715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
RE
GN GN
A0 A1
L SC A SD EF AR ND G 13
RX
A
TX
E
L
13
.
~
.
10
K
11
A C
~
H
12
.
E
R
A
H
S
IN
A5
G
N
R
A4
AL O
A3
AN
A2
~ 9
P
8
D 7 I 6 IG T ~ AL ~5 (P 4 W 3 M ~ ~ 2
IS O N
) TX
1
RX
0
Page 65
Page 66
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 12 : Spinning a Motor
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 67
Image Reference:
5V
b11 a3
C
5V GND
Jumper Wire
Jumper Wire
+
e3 d11
+
Jumper Wire
j2 a7
Pin 9
e1
e2 e1 g21 e7
Jumper Wire
Jumper Wire
Jumper Wire
330Ω Resistor
e7 e11
B
DC Motor
E
a1 a2 a3
GND b7
EMITTER BASE COLLECTOR
Diode 1N4148
Transistor P2N2222AG
Component:
P2N2 222A
Finally, when you create something really cool, consider sharing it with the world so that others can learn from your genius. Be sure to let us know on https://www.sparkfun.com/project_calls so we can put it on our home page!
If you need help, there are internet forums where you can ask questions. Try Arduino's forum at arduino.cc/forum, and SparkFun's at forum.sparkfun.com. When you're ready to move to more advanced topics, take a look at Arduino's tutorials page at arduino.cc/en/tutorial. Many of SparkFun's more advanced products were programmed with Arduino, (allowing you to easily modify them), or have Arduino examples for them. See our product pages for info.
It's usually pretty easy to pull pieces of different sketches together, just open them in two windows, and copy and paste between them. This is one of the reasons we've been promoting "good programming habits". Things like using constants for pin numbers, and breaking your sketch into functions, make it much easier to re-use your code in new sketches. For example, if you pull in two pieces of code that use the same pin, you can easily change one of the constants to a new pin. (Don't forget that not all of the pins support analogWrite(); the compatible pins are marked on your board.)
We've already shown you how to use a bunch of different input sensors and output devices (and we still have a few more to go). Feel free to make use of the examples in your own sketches - this is the whole idea behind the "Open Source" movement.
1. Perform some sort of input 2. Make some calculations or decisions 3. Perform some sort of output 4. Repeat! (Or not!)
Most of the sketches you write will be a loop with some or all of these steps:
At this point you're probably starting to get your own ideas for circuits that do fun things, or help solve a real problem. Excellent! Here are some tips on programming in general.
Putting it all together:
12
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 12
Code to Note:
while (Serial.available() > 0)
The RedBoard's serial port can be used to receive as well as send data. Because data could arrive at any time, the RedBoard stores, or "buffers" data coming into the port until you're ready to use it. The Serial.available() command returns the number of characters that the port has received, but haven't been used by your sketch yet. Zero means no data has arrived.
speed = Serial.parseInt();
If the port has data waiting for you, there are a number of ways for you to use it. Since we're typing numbers into the port, we can use the handy Serial.parseInt() command to extract, or "parse" integer numbers from the characters it's received. If you type "1" "0" "0" to the port, this function will return the number 100. V 15 7-
5V
3V 3.
F RE IO SET RE
What You Should See:
PO
RE SE T
D W GN D E R GN
N VI A0
L SC A SD EF AR ND G 13
A LE
RN
H .S
AC H E. AR
K.
O N TX
12 11 ~ 0 1 ~ 9 ~ 8 D 7 I 6 IG T ~ AL ~5 (P 4 W 3 M ~ ~ 2 ) 1
IS P
RX
13 R1X3 T TX RX X
IN
A5
0
Page 68
A4
Still Not Working Sometimes the RedBoard will disconnect from the computer. Try un-plugging and then re-plugging it into your USB port.
A
Still No Luck If you sourced your own motor, double check that it will work with 5 volts and that it does not draw too much power.
AN AL O 3 G
Motor Not Spinning If you sourced your own transistor, double check with the data sheet that the pinout is compatible with a P2N2222AG (many are reversed).
A2
Troubleshooting:
A1
The DC Motor should spin if you have assembled the circuit’s components correctly, and also verified/uploaded the correct code. If your circuit is not working check the troubleshooting section below.
Real World Application: Radio Controlled(RC) cars use Direct Current(DC) motors to turn the wheels for propulsion.
13 5
Circuit 2 CIRCUIT #5 #13
Relays
In this circuit, we are going to use some of the lessons we learned in circuit 12 to control a relay. A relay is basically an electrically controlled mechanical switch. Inside that harmless looking plastic box is an electromagnet that, when it gets a jolt of energy, causes a switch to trip. In this circuit, you’ll learn how to control a relay like a pro – giving your RedBoard even more powerful abilities!
5 volt Resistor (330 ohm) (Orange-Orange-Brown)
Diode (1N4148) SPDT Relay Transistor (P2N2222AG)
RedBoard
PIN 2
LEDs (Light-Emitting Diodes)
Resistor (330 ohm) (Orange-Orange-Brown)
When the relay is off, the COM (common) pin will be connected to the NC (Normally Closed) pin. When the relay is on, the COM (common) pin will be connected to the NO (Normally Open) pin.
PARTS:
GND (Ground)
IC Relay
Transistor LED
Diode
P2N2222AG X
1
X
330Ω Resistor
1N4148
8
X
1
X
1
LED X
2
Wire X
2
14
X
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
RE
GN GN
A0 A1
L SC A SD EF AR ND G 13
RX
A
TX
E
L
13
.
~
.
10
K
11
A C
~
H
12
.
E
R
A
H
S
IN
A5
G
N
R
A4
AL O
A3
AN
A2
~ 9
P
8
D 7 I 6 IG T ~ AL ~5 (P 4 W 3 M ~ ~ 2
IS O N
) TX
1
RX
0
Page 69
Page 70
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 13: Relays
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 71
Image Reference:
i13 e22 i13
Jumper Wire
i15 e19
+
h9
Jumper Wire
Jumper Wire
j9
j7 j5
j3
Jumper Wire
Jumper Wire Pin 2
Jumper Wire
e2
Jumper Wire
Jumper Wire
b14 +1 e7 e1
5V
330Ω Resistor
Jumper Wire
f7 a3
GND e7
Jumper Wire
Jumper Wire
-
Jumper Wire
+
-
Jumper Wire
c22 c23
+
C
c19 c20
B
e3 e1 g31 e7
+
+
E
a2 a3 a4
Jumper Wire
5V
-
-
EMITTER BASE COLLECTOR
e14 e9 f15 f13 f9
Component:
330Ω Resistor
Diode 1N4148
LED (5mm)
LED (5mm)
Transistor P2N2222AG
Relay
Component:
P2N2 222A
Image Reference:
GND
5V
+
e4 b9
a7 a9
a20 e19 + b14
a23 + e19 + e15
+ e19 + e15
f7 f5 f6e19 e15
13
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 13
Code to Note:
digitalWrite(relayPin, HIGH);
When we turn on the transistor, which in turn energizes the relay's coil, the relay's switch contacts are closed. This connects the relay's COM pin to the NO (Normally Open) pin. Whatever you've connected using these pins will turn on. (Here we're using LEDs, but this could be almost anything.)
digitalWrite(relayPin, LOW);
The relay has an additional contact called NC (Normally Closed). The NC pin is connected to the COM pin when the relay is OFF. You can use either pin depending on whether something should be normally on or normally off. You can also use both pins to alternate power to two devices, much like railroad crossing warning lights.
What You Should See: V 15 7-
F RE IO SET RE V 3 3.
RE SE T
P 5 V DD OW GGNN DD E R GGNN NN VVI AA00 AA1
LE
N
.S
H
IINN E.
H
AC
K.
IISS PP
OO NN
2 112 1 111 ~~ 100 1 ~~ 99 ~~ 88 DD I 77 I GG 66 IITT ~~ 5 AA 5 ~ ~ L L ((PP 44 WW 33 MM ~~ ~~ 22 )) 11
AR
AA55
AR
AA44
CLL SC A DA SSD EEFF R AAR NNDD GG 1133
AA3
TTX X RX
1133 RRX1X3 TT T X R XXX
AN NA ALL OO GG
AA2
You should be able to hear the relay contacts click, and see the two LEDs alternate illuminating at 1-second intervals. If you don't, double-check that you have assembled the circuit correctly, and uploaded the correct sketch to the board. Also, see the troubleshooting tips below.
00
Troubleshooting:
Real World Application:
LEDs Not Lighting Double-check that you've plugged them in correctly. The longer lead (and non-flat edge of the plastic flange) is the positive lead.
Garage door openers use relays to operate. You might be able to hear the clicking if you listen closely.
No Clicking Sound The transistor or coil portion of the circuit isn't quite working. Check the transistor is plugged in the right way. 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). When you’re building the circuit be careful not to mix up the temperature sensor and the transistor, they’re almost identical. Page 72
14
Circuit 2 CIRCUIT #14
Shift Register
Now we are going to step into the world of ICs (integrated circuits). In this circuit, you’ll learn all about using a shift register (also called a serial-to-parallel converter). The shift register will give your RedBoard an additional eight outputs, using only three pins on your board. For this circuit, you’ll practice by using the shift register to control eight LEDs.
RedBoard
5 volt
PIN 2
14
PIN 3
11
PIN 4
SER
VCC
SCK
QB
SCL
QC
16 15
QA
10
1 2 3
QD
12
RCK
4
QE
5
QF
13 8
OE
QG
GND
QH QH*
Resistors (330 ohm) (Orange-Orange-Brown)
6 7 9
LEDs (Light-Emitting Diodes)
74HC595
PARTS:
GND (Ground)
*Do not connect pin 9
IC
LED X
330Ω Resistor
1
X
Wire
8
X
8
19
X
VCC
QB
1
16
QC
2
15
QA
QD
3
14
SER
QE
4
13
OE
QF
5
12
RCLK
QG
6
11
SRCLK
QH
7
10
SRCLR
GND
8
9
QH’
Align notch on top, inbetween “e5” and “f5” on the breadboard. The notch indicates where pin 1 is.
Bend legs to 90° angle.
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
RE
GN GN
A0 A1
RX 13
IN
A5
G
L SC A SD EF AR ND G 13
TX
A4
AL O
A3
AN
A2
12
~ ~
11
10 ~ 9
P
8
D 7 I 6 IG T ~ AL ~5 (P 4 W 3 M ~ ~ 2
IS O N
) TX
1
RX
0
Page 73
Page 74
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 14: Shift Register
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 75
-
Jumper Wire Jumper Wire Jumper Wire Jumper Wire
c24 a3 c24 a3 c24 a3 c24 a3
+ a3
GND j15 GND j18 GND j21 GND j24
+ GND
330Ω Resistor
330Ω Resistor
330Ω Resistor
330Ω Resistor
Jumper Wire
Jumper Wire
a24 a3
GND c23
330Ω Resistor
Jumper Wire
Jumper Wire
-
Jumper Wire
Jumper Wire
Jumper Wire
a21 a3
+
h23 h24
+
h20 h21
+
h17 h18
+
Jumper Wire
GND c23
+
-
h14 h15
+
Jumper Wire
330Ω Resistor
-
LED (5mm)
+
-
c23 c24
+
Jumper Wire
Jumper Wire
-
LED (5mm)
+
-
c20 c21
+
Jumper Wire
Jumper Wire
a18 a3
-
LED (5mm)
+
-
c17 c18
+
c14 c15
f9 f10 f11 f12
GND c23
-
LED (5mm)
+
f8
330Ω Resistor
-
LED (5mm)
+
f7
Jumper Wire
-
LED (5mm)
+
f6
a15 a3
-
LED (5mm)
+
f5
e5 e6 e7 e8 e9 e10 e11 e12
Component:
330Ω Resistor
-
Image Reference:
LED (5mm)
IC
Component:
Image Reference:
GND
5V
Pin 3
Pin 4
Pin 2
a14 a3
+ a3
+ a3
a1 4 a3
a9 a3 a10 a3 a11 a3 a7 a3 a6 a3 a5 a3
f17 GND f20 GND f23 GND a23 GND a20 GND a17 GND
+ j10
a8 a3
+4 a1 a3 f14 GND
j11 GND
j10
j9
j8 GND
j7
j6 GND
j5 GND
GND +
14
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 14
Code to Note:
You'll communicate with the shift register (and a lot of other parts) using an interface called SPI, or Serial Peripheral Interface. This interface uses a data line and a separate clock line that work together to move data in or out of
shiftOut(datapin, clockpin, MSBFIRST, data);
the RedBoard at high speed. The MSBFIRST parameter specifies the order in which to send the individual bits, in this case we're sending the Most Significant Bit first.
Bits are the smallest possible piece of memory in a computer; each one can store either a "1" or a "0". Larger numbers are stored as arrays of bits. Sometimes we want to manipulate these bits directly, for example now when
bitWrite(byteVar, desiredBit, desiredState);
we're sending eight bits to the shift register and we want to make them 1 or 0 to turn the LEDs on or off. The RedBoard has several commands, such as bitWrite(), that make this easy to do.
F RE IO SET RE V 3 3.
RE SE T
P 5V D O W GN D E R GN N VI A0 A1
LE
N
H .S
IN AR
H
AC
K.
IS P
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ AL ~5 (P 4 W 3 M ~ ~ 2 ) 1
E.
A5
AR
A4
TX
L SC A SD EF AR ND G 13
A3
RX
13 R1X3 T TX RX X
AN AL O G
A2
You should see the LEDs light up similarly to circuit 4 (but this time, you're using a shift register). If they aren't, make sure you have assembled the circuit correctly and verified and uploaded the code to your board. See the troubleshooting tips below.
V 15 7-
What You Should See:
0
Troubleshooting:
Real World Application:
The RedBoard's power LED goes out This happened to us a couple of times, it happens when the chip is inserted backward. If you fix it quickly nothing will break.
Similar to circuit #4, a scrolling marquee display delivers a message with multiple LEDs. Essentially the same task the shift register achieves here in Circuit #14.
Not Quite Working Sorry to sound like a broken record but it is probably something as simple as a crossed wire. 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:
[email protected] Page 76
Circuit 2 CIRCUIT #5 #15
LCD
In this circuit, you’ll learn about how to use an LCD. An LCD, or liquid crystal display, is a simple screen that can display commands, bits of information, or readings from your sensor - all depending on how you program your board. In this circuit, you’ll learn the basics of incorporating an LCD into your project. PIN 2 PIN 3 PIN 4 PIN 5 PIN 11 PIN 12
GND (Ground)
PARTS:
RedBoard
K
A
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
E
R/W
RS
VO
VDD
VSS
16x2 LCD 5 volt
15
IC LCD
LED Potentiometer 330Ω Wire Resistor XX
1
X
8
X
1
Wire 16 8
19
X
X
V 15 7-
F RE IO SET RE V 3 3.
RE SE T
P 5V D O W GN D E R GN N VI A0 A1
L SC A SD EF AR ND G 13
.
N
.
K
C
A
H
IS P O N TX
X RRX
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
.
E
R
A
H
S
IN
A5
1 13 RX 3 T TX R XX
A4
A R
E
L
A3
AN AL O G
A2
0
Page 77
Circuit 15: LCD
Page 78
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 79
Jumper Wire
j16
a3 a3 + a3
e8 GND f15 GND f16 GND
Jumper Wire
Jumper Wire
Jumper Wire
f17 j10
f28 a3
e7 GND
+ j10
b6
b7
b8
j15
Jumper Wire
Pin 2
Jumper Wire
Jumper Wire
Jumper Wire
j17
+ a3
Jumper Wire
Pin 5
Jumper Wire
Pin 12
Pin 11
Pin 4
Jumper Wire
Jumper Wire
Pin 3
Image Reference:
Jumper Wire
Component:
j18
j19
j20
j21
j22
j23
j24
j25
j26
j27
j28
e6 GND
GND
Jumper Wire
j30 j29
Jumper Wire
5V
Image Reference:
Jumper Wire
Potentiometer
LCD
Component:
a3
+ a3 f30 a3
f29 GND
f27
f26 GND
f25
f20
f19
f18
15
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit # 15
Code to Note:
#include
This bit of code tells your Arduino IDE to include the library for a simple LCD display. Without it, none of the commands will work, so make sure you include it!
lcd.print(“hello, world!”);
This is the first time you’ll fire something up on your screen. You may need to adjust the contrast to make it visible. Twist the potentiometer until you can clearly see the text!
F RE IO SET RE V 3 3.
RE SE T
P 5V D O W GN D E R GN N VI A0 A1
LE
.S
IN
H
K. AC
IS P
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
H E. AR
A5
N AR
A4
TX
L SC A SD EF AR ND G 13
A3
RX
13 R1X3 T TX RX X
AN AL O G
A2
0
Initially, you should see the words “hello, world!” pop up on your LCD. Remember you can adjust the contrast using the potentiometer if you can’t make out the words clearly. If you have any issues, make sure your code is correct and double-check your connections.
V 15 7-
What you Should See:
Troubleshooting:
Real World Application:
The Screen is Blank or Completely Lit? Fiddle with the contrast by twisting the potentiometer. If it’s incorrectly adjusted, you won’t be able to read the text.
LCDs are everywhere! From advanced LCDs like your television, to simple notification screens, this is a very common and useful display!
Not Working At All? Double check the code, specifically that you include the LCD library. Screen Is Flickering Double check your connections to your breadboard and Arduino.
Page 80
16
Circuit 2 CIRCUIT #5 #16
Simon Says Piezo Buzzer
Now that we've learned all the basics behind the components in the SIK, let's put them together and have some fun. This circuit will show you how to create your own Simon Says game. Using some LEDs, some buttons, a buzzer and some resistors, you can create this and other exciting games with your SIK.
RedBoard PIN 7 PIN 4
Resistors (330 ohm) (Orange-Orange-Brown)
LEDs (Light-Emitting Diodes)
PIN 5
Yellow
PIN 13
Blue
PIN 3
Green
PIN 10
Buttons
Red
PIN 6 PIN 12
GND (Ground)
PIN 2 PIN 9
PARTS:
GND (Ground)
IC 330Ω Resistor
Push Button 330Ω Wire Resistor
LED X
14
8
X
X
4
X
419
8
Wire
Piezo Element X X
X
1
16
X
715 V
IO F
RE
RE
5V
3V
T
3.
SE
SE T
W
N
ER
VI
D
PO
D
GN
RE
GN
A0 A1
13
N . E
R
A
H
S
IN
A5
G
L SC A SD EF AR ND G 13
A
TX
E
RX
L R
O
A4
AL
A3
AN
A2
~
.
10
K
11
C
A
~
H
12
.
~ 9
P
8
O N TX RX
D 7 IG 6 IT ~ 5 A L ~ 4 (P W 3 M ~ ~ 2 ) 1
IS
0
Page 81
Page 82
A5
13 TX RX
A4
A3
POWER
A2
A1
A0
VIN
GND
GND
5V
3.3V
RESET
IOREF
7-15V
Circuit 16: Simon Says
SCL SDA AREF GND 13 12 ~11 ~10 ~9 8
RESET 7 ~6 ~5 4 ~3 2 TX 1 RX 0
DIGITAL (PWM~)
ON
LEARN. SHARE. HACK.
ISP
ANALOG IN
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
a b c d e
a b c d e
f g h i
f g h i 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Page 83
-
Piezo Element
Push Button
Push Button
Push Button
Push Button
LED (5mm)
LED (5mm)
LED (5mm)
-
-
-
+
+
+
e20 GND
330Ω Resistor
g20 a3
g18 a3
g13 a3
g11 a3
-
+
-
a16 a14
d29 g29
d27 g27
d24 g24
d22 g22
d9 g9
d7 g7
d4 g4
d2 g2
+
j20 c3
+
j18 c3
+
j12 c3
+
j11 c3
e18 GND
330Ω Resistor
+
e12 GND
330Ω Resistor
LED (5mm)
e11 GND
Image Reference:
330Ω Resistor
Component:
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
GND
Pin 9
Pin 2
Pin 10
Pin 3
Pin 13
Pin 5
GND
a3
c29 j10
c27 a3
c24 j10
c22 a3
d20 j10
d18 j10
d12 j10
d11 j10
d12 j10
Pin 13
Jumper Wire Jumper Wire
d11 j10
Pin 5
Jumper Wire
c7 a3
c4 j10
c9 j10
Pin 6
c2 a3
Pin 12
Image Reference:
Jumper Wire
Jumper Wire
Jumper Wire
Jumper Wire
Component:
16
Arduino Code:
Circuit 2
Open Arduino IDE // File > Examples > SIK Guide > Circuit #16
Code to Note:
#define
byte
The #define statement is used to create constants in your code. Constants are variables that will likely only have one value during the lifespan of your code. Thus, you can assign constants a value, and then use them throughout your code wherever you need them. Then, if you need to change that value, you only have to change one line instead of going through all the code to find every instance of that variable.
Bytes are another variable type. In the world of computing, a byte is a chunk of space that contains 8 bits, and a bit is a single binary value. Binary is another way of counting and uses only 1's and 0's. So a byte can hold all 1's: 11111111, all 0's: 00000000, or a combination of the two: 10010110. V 15 7-
F RE IO SET RE
RE SE T
P 5V D O W GN D E R GN N VI
3V 3.
What You Should See: A0 A1
LE
.S
IN
H
K. AC
IS P
O N
12 11 ~ 0 1 ~ 9 ~ 8 D 7 IG 6 IT ~ 5 A ~ L (P 4 W 3 M ~ ~ 2 ) 1
H E. AR
A5
N AR
A4
TX
L SC A SD EF AR ND G 13
A3
RX
13 R1X3 T TX RX X
AN AL O G
A2
With the circuit complete, plug the Arduino in to a power source. Once powered, the buzzer will beep a few times, and all four LEDs should begin blinking. The game begins once you press any of the four buttons. Once the game has been started, a random LED will blink. Press the button associated with that color LED to replicate the pattern. With a successful guess, the pattern will repeat, this time adding another random LED. The player is to follow the pattern for as long as possible, with each successful guess resulting in an additional layer of complexity added to the original pattern.
0
Troubleshooting:
Real World Application:
Only half the circuit works If only half of your circuit is working, make sure you added the additional wire from one ground rail to the other. Remember that breadboards have two power rails on each side and that these can be connected, or bussed, together to provide the power to both sides of the same circuit.
Toys and games, such as the original Simon from Milton Bradley, have relied on electronics to provide fun and entertainment to children across the world.
No sound Once the buzzer is in the breadboard, it's hard to see the legs and which row they are connected to. If you aren't hearing any sound, make sure your wires are on the same row as the buzzer legs. Game is not working If everything starts up ok, but you're having trouble when it comes time to play the game, you may have a button or two misplaced. Pay close attention to which pin is connected to each button, as it matters which button is pressed when a particular color lights up. Page 84
Learning More Visit us Online: This is just the beginning of your exploration into embedded electronics and coding. Our website has a wealth of tutorials to whet your appetite for more knowledge. We also host a community of hackers, engineers, DIYers, etc. in our forums. So log on to our website for more information about Arduino, or to plan ahead for your next project!
www.sparkfun.com
NOTES:
Begin your Journey into Electronics This kit will guide you through experiments of varying difficulty as you learn all about embedded systems, physical computing, programming and more! This kit is perfect for anyone who wants to explore the power of the RedBoard platform.
The SparkFun Inventor’s Kit teaches basic programming, for which you will need both a computer and an internet connection.
You will also learn to assemble 16 basic physical electronic circuits, but no soldering is required. No previous experience is necessary!
KIT INCLUDES
SparkFun RedBoard Breadboard Instruction booklet Sealed relay Small servo LEDs
RGB LED Temperature sensor DC motor 8-bit shift register Push button switches Potentiometer
Photo Resistor Transistors Jumper wires USB cable Signal diodes 10k ohm resistors
330 ohm resistors Piezo buzzer Flex sensor Soft potentiometer Baseplate LCD
© SparkFun Electronics, inc. All rights reserved. The SparkFun Inventor’s kit for the SparkFun RedBoard features, specifications, system requirements and availability are subject to change without notice. All other trademarks contained herein are the property of their respective owners. The SIK Guide for the SparkFun Inventor’s Kit for the SparkFun RedBoard is licensed under the Creative Commons Attribution Share-Alike 3.0 Unported License To view a copy of this license visit: http://creativecommons.org/by-sa/3.0/ Or write: Creative Commons, 171 Second Street, Suite 300, San Francisco, CA 94105, USA.