Académique Documents
Professionnel Documents
Culture Documents
Class 1/4
• Community-built
• Examples wiki (the “playground”) editable by anyone
• Forums with lots of helpful people
Arduino Hardware
• Similar to Basic Stamp (if you know of it)
• but cheaper, faster, & open
USB
2”
TX/RX
LEDs ATmega328
power pins
2.7” analog input pins
Arduino Capabilities
• 32 KB of Flash program memory
• 2 KB of RAM
Boarduino Kit
“Stamp”-sized
Bluetooth
many different variations to suite your needs
Arduino Terminology
“sketch” – a program you write to run on an
Arduino board
“pin” – an input or output connected to something.
e.g. output to an LED, input from a knob.
“digital” – value is either HIGH or LOW.
(aka on/off, one/zero) e.g. switch state
“analog” – value ranges, usually from 0-255.
e.g. LED brightness, motor speed, etc.
Arduino Software
• Integrated Development
Environment
• View/write/edit sketches
usually highest-
numbered port
pick “Arduino
Uno”
Selecting Location & Type
starts with
tty.usbserial-
pick “Arduino
Uno”
Arduino Software
status
area
Load Blink Example
• File → Examples → 01.Basics → Blink
Save a copy
TX/RX flash
l i n k
b k
l i n
b sketch runs
Status Messages
Size depends on
complexity of your sketch
Uploading worked
All connected,
a “bus”
not
connected
Useful Tools
Wire stripper Wire cutters
Needle-nose
pliers
Making Jumper Wires
pliers & cutter wire stripper
~1/4”
Using Solderless
Breadboards
Using needle nose pliers can help
push wires & components into holes
Making Circuits
+
9V resistor
– 500 ohm
(green,brown,brown) 500
LED
(flat part)
Voltage Load
Source
Image source: Engineer's Mini Notebook, Mims III, Master Publishining, 2007.
LEDs & Resistors
LED
Flat edge here for neg. side
resistor
Resistors have no polarity
Current Limiting
Resistors
note
This example shows how to fade an LED using the analogWrite() function.
*/
void setup() {
// nothing happens in setup
}
void loop() {
// fade in from min to max in increments of 5 points:
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
Snug-fit straws on
the end of your
LEDs to make
them glow more
visibly
Random Behavior
“CandleLight”
Uses simple
pseudo random
number generator
to mimic flame
Use random(min,max)
to pick a number between
min & max.
/*
* CandleLight
* -----------
*
* Use random numbers to emulate a flickering candle with a PWM'd LED
*
*/
void setup() {
randomSeed(0); // initialize the random number generator
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
val = random(100,255); // pick a random number between 100 and 255
analogWrite(ledPin, val); // set the LED brightness
void loop()
{
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(0, 255, 255); // aqua
delay(1000);
}
Note the use of a user void setColor(int red, int green, int blue)
{
defined function analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Return Type Parameters
Name
void loop()
{
Loop Function
leds = 0;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
updateShiftRegister();
delay(500);
}
}
User Defined
void updateShiftRegister()
{ Function
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
byte leds = 0;
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
leds = 0;
updateShiftRegister();
delay(500);
for (int i = 0; i < 8; i++)
{
bitSet(leds, i);
updateShiftRegister();
delay(500);
}
}
bitSet
Sets (writes a 1 to) a bit of a numeric variable
bitSet(x,n)
x - the variable being acted upon
n - the bit to set, starting at 0 for the LSB
http://arduino.cc/en/Reference/BitSet
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
shiftOut
Shifts out a byte of data one bit at a time
shiftOut(dataPin, clockPin, bitOrder, value)
dataPin - the pin on which to output each bit (int)
clockPin - the pin to toggle once the dataPin has
been set to the correct value (int)
bitOrder: either MSBFIRST or LSBFIRST
value: the data to shift out. (byte)
http://arduino.cc/en/Reference/ShiftOut
The Serial Monitor
LOW
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
}
}
Using digitalRead()
• In setup():
pinMode(myPin,INPUT_PULLUP) makes
a pin an input with the pullup resistor enabled
• In loop(): digitalRead(myPin) gets
switch’s position
• If doing many tests, use a variable to hold the output value of
digitalRead().
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
Combines “Blink” & “Fading” }
http://duksta.org/electronics/blr-arduino
John Duksta
ducksauz@hellyeah.com
Resources
http://arduino.cc/
Official homepage. Also check out the Playground & forums
http://ladyada.net/learn/arduino/
Great Arduino tutorials
http://todbot.com/blog/category/arduino/
Various movies, hacks, tutorials on Arduino
http://freeduino.org/
Index of Arduino knowledge
http://adafruit.com/
Arduino starter kits, Boarduino Arduino clone, lots of cool kits
http://sparkfun.com/
Sells Arduino boards and lots of neat sensors & stuff
Books:
“Physical Computing”, Dan O’Sullivan & Tom Igoe
“Making Things Talk”, Tom Igoe