Professional Documents
Culture Documents
Class 1
At worst, you’ll be able to fashion a convincing disguise from pasting Arduinos on your body.
Class Kit I Contents
There will be a second update kit next week: “motion & motors”
A Word on Safety
• Electronics can hurt you
• Lead in some of the parts
• Wash up afterwards
• You can hurt electronics
• Static-sensitive: don’t shuffle your feet & touch
• Wires only bend so much
What is Arduino?
The word “Arduino” can mean 3 things
• 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
The designer of the AVR purposefully arranged its registers and instruction set so that C programs
would compile efficiently on it. This is a big deal, compared to previous microcontrollers where C
programs were almost always less efficient than a hand-coded assembly language variant.
Arduino Hardware Variety
LilyPad
DIY
(for clothing)
USB
Boarduino Kit
“Stamp”-sized
Bluetooth
many different variations to suite your needs
Openness has its advantages, many different varieties.
Anyone can build an Arduino work-alike in any form-factor they want.
Product images from Sparkfun.com and Adafruit.com
Arduino Capabilities
• 16 kBytes of Flash program memory
• 1 kByte of RAM
Don’t worry if the above doesn’t make sense, you don’t really need to know it.
Arduino Diecimila Board
test LED digital input/output “pins”
on “pin” 13
power
LED
USB interface
2”
TX/RX
LEDs ATmega168 reset
button
• View/write/edit sketches
If you’ve used Processing to write little Java programs, you’ll notice the interface looks familiar.
Arduino takes the editor GUI from Processing and some of its philosophy, but Arduino code and
Processing code are totally unrelated.
Installing Arduino
The Steps
1. Get the Arduino software & unzip it
2. Plug in Arduino board
3. Install the driver
4. Reboot
5. Run the Arduino program
6. Tell Arduino (program) about Arduino (board)
Getting and Unpacking
• On the thumbdrives
• “arduino-0010-win.zip” for Windows
• “arduino-0010-mac.zip” for Mac OS X
• Unzip the zip file. Double-click on Mac
On Windows, right-click
Use “Extract All...”
usually highest-
numbered port
pick “Diecimila”
Selecting Location & Type
starts with
tty.usbserial-
pick “Diecimila”
Arduino Software
status
area
Using Arduino
• Write your sketch
Uploading worked
And all over the Net. Search for “Arduino tutorial” or “Arduino notes” or whatever you’re interested
in and “Arduino” and likely you’ll find some neat pages.
Take a Break
+
9V resistor
– 500 ohm
(green,brown,brown) 500
LED
(flat part)
All LED circuits are essentially this: power source, current limiter, LED
Flat part of LED goes to negative, like bar in schematic
The higher the resistance, the dimmer the LED; the lower, the brighter
You don’t have to wire this up, but the following circuits are just the same
The Circuit for LED Blink
“hello world” of microcontrollers
LED
Arduino
flat part
board
resistor
pin 13
LED
gnd
resistor gnd
220 ohm
(red,red,brown)
Schematics are pretty easy to learn, not many people use wiring diagrams.
LEDs & Resistors
LED
Flat edge here for neg. side
resistor
Polarity doesn’t matter on resistors
Varying LED Brightness
Same circuit as Blink circuit but pin 9 instead of pin 13
flat part
Arduino
board
resistor
pin 9
LED
gnd
gnd resistor
220 ohm
(red,red,brown)
Very quickly, it works by making and breaking the flow several hundred times a second. So really
it’s flashing, just like blink, but doing it very fast. Our eyes make it look like brighter or dimmer.
We’ll have to build this circuit.
Let’s Wire It Up
Arduino
board
resistor
pin 9
LED
gnd
gnd
All connected,
a “bus”
not
connected
Needle-nose
pliers
Even with solderless breadboards you’ll need to cut and strip wire. Each of these costs around $5
each. If you have to get just one, get the wire stripper.
Making Jumper Wires
pliers & cutter wire stripper
~1/4”
4. jumper over
to other side
Rubber band trick around Arduino & solderless breadboard shameless stolen from Kimiko Ryokai’s
Tangible User Interface class (INFO290-13): http://courses.ischool.berkeley.edu/i290-13/f07/
LED “Fading” Sketch
Load “File/Sketchbook/Examples/Analog/Fading”
note
LOW
• Plug in power
(7-12VDC)
set to
• Power LED lights EXT
up. It works!
• Reverse steps to
reprogram
Battery Power
Center of set to
jack is EXT
• Plugging into the positive
sockets is kind of
fiddly
http://todbot.com/blog/bionicarduino/
Tod E. Kurt
tod@todbot.com
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
“Hacking Roomba”, Tod E. Kurt
obligiatory book plug
Bionic Arduino
Introduction to Microcontrollers with Arduino
Class 2
compile
upload
l in k
b k
l i n
b sketch runs
Change the “delay()” values to change blink rate
Known Good
Configuration
Rule #1 of experimenting:
schematic gnd
Snug-fit straws on
the end of your
LEDs to make
them glow more
visibly
I have a box of multi-colored straws for whatever color LED you like
Random Behavior
“CandleLight”
Uses simple
pseudo random
number generator
to mimic flame
Use random(min,max)
to pick a number between
min & max.
+5V–
50k
measure–
gnd–
When a resistor goes across a voltage difference, like +5V to Gnd, the voltage measured at any point
along a resistor’s length is proportional to the distance from one side.
If you take apart a pot, there’s a little wiper just like in the schematic symbol.
But I might have the directions reversed (clockwise vs. anti-clockwise).
What good are pots?
Why are we using Analog In 2? Because it’s in the middle. There’s no reason, any of the 6 analog
inputs would work the same.
Pot & LED Circuit
This is what your board should have on it now
+5V
Arduino
+5V board
resistor
220 (red-red-brown)
50k pin 2 pin 9
potentiometer
gnd LED
gnd
gnd
resistor resistor
pin 9 pin 9
LED
gnd gnd
gnd
We’ve been using the one on the left because it makes more sense.
But you’ll see the method on the right as well.
The reason for this is that some circuits can switch to Gnd better than they can switch to +5V.
RGB LEDs
Normal LED
anode +
anode +
cathode –
cathode –
RGB LED
anode +
red cathode –
anode +
blue cathode –
green cathode –
red blue green
Arduino
board
pin 11
pin 10
pin 9
220 (red,red,brown)
gnd
green blue red With RGB you can
make any color
(except black)
Arduino
board
pin 11
pin 10
pin 9
220 (red,red,brown)
gnd
green blue red
slightly bend the longest lead and plug it into the +5v (red) bus
plug remaining leads into rows (12,14,&16 here)
connect 220 (red-red-brown) resistors across middle to matching rows
run wires from resistors to pins 9,10,11 of Arduino, can color-code if you want
It just ramps up and down the red,green,& blue color values and writes them with analogWrite()
from http://www.arduino.cc/en/Tutorial/DimmingLEDs
Pot-controlled RGB
+5V
common anode
RGB LED
Arduino
+5V board
pin 11
pin 10
50k pin 2 pin 9
pot 220 (red,red,brown)
gnd
green blue red
gnd
Pot-controlled RGB
“RGBPotMixer”
schematic symbol
Pretty cheap too. Can get a grab bag of 100 misc from Jameco for $20
Photocell Circuit
pin A2
brown-black-orange
gnd
sensor
thermistor to analog
input
(temperature) resistor
photocell
(light)
flex sensor
(bend, deflection)
• TX – sending to PC
• RX – receiving from PC
• Used when programming
or communicating
Arduino Says “Hi”
“SerialHelloWorld”
Click on “Serial
Monitor” button to
see output
In “Serial Monitor”,
type “H”, press Send
Serial.available() tells
you if data present to read
This sketch is in the handout
Always check Serial.available() or if Serial.read() != -1 to determine if there’s actual data to read.
Can modify it to print “hello world” after it receives something, but before it checks for ‘H’.
This way you can verify it’s actually receiving something.
Arduino Communications
is just serial communications
USB to serial
Arduino
microcontroller
Processing
sketch
-OR-
Java program
-OR-
...
Also, USB is a host/peripheral protocol. Being a USB “host” means needing a lot of processing
power and software, not something for a tiny 8kB microcontroller.
It can be a peripheral. In fact, there is an open project called “AVR-USB” that allows AVR chips like
used in Arduino to be proper USB peripherals. See: http://www.obdev.at/products/avrusb/
Controlling the Computer
• The “readSerialString()”
function at right takes a
character string and sticks
available serial data into it
Piezo buzzers don’t have quartz crystals, but instead a kind of ceramic that also exhibits
piezoelectric properties.
I pronounce it “pie-zoh”. Or sometimes “pee-ay-zoh”.
Piezo Buzzers
piezo
Arduino
buzzer
board
+
pin 7
–
gnd
Piezo leads are very thin. The breadboard holes grab them better than the header sockets, which is
why the jumper leads are used.
Or you can jam a jumper wire in the holes to hold in the piezo leads.
Play a Melody
“SoundSerial”
piezo
Arduino Arduino
buzzer
board board
10k
+ +
pin 7 pin 7 piezo
(brown, buzzer
– –
black,
orange)
gnd gnd
Like most things in electronics, if you want less of something, add a resistor.
A better value would probably be 1k, but we don’t have that on hand.
This may not seem important now, but wait for the next project.
Play a Stored Melody
“PlayMelody”
No touching needed!
We’ll use light in lieu of RF Leon Theremin
As heard on Star Trek, Beach Boys, horror movies, Mars Attacks!, and bad New Age songs.
Works sorta like those touch switches, but no touching here.
That is, your body becomes a variable capacitor.
Light Theremin
“Theremin”
Okay so maybe it sounds more like a bad video game than a spooky movie
The glitchy sound is cause because of the time it takes to read the sensor
There are ways around such stuff, but requires more complex programming using timers &
interrupts
The sound can get annoying quick
Other Serial Devices
to Roomba
Ambient Orb doesn’t connect to computer though. Uses the pager network.
Ambient Devices: http://www.ambientdevices.com/
END Class 2
http://todbot.com/blog/bionicarduino/
Tod E. Kurt
tod@todbot.com
Class 3
In the handout thumbdrives, be sure to copy the Processing zip or dmg file for your OS.
Recap: Blinky LED
Make sure things still work
compile
upload
l in k
b k
l i n
b sketch runs
Change the “delay()” values to change blink rate
Class Kit 2 Contents
“motors & motion”
Class Kit 2 Manifest
“motors & motion”
When motors first came out, people thought we’d just have one for the house. The household
motor. Various attachments for vacuuming, meat grinding, ceiling fan were available, and some
houses had intricate mazes of belts and gears routed through the house to supply this rotational
power.
DC Motors
A dizzying array of parameters specify a motor
battery
M DC motor
LED
M
DC motor
These high-efficiency motors I gave you don’t generate much current (because they don’t use much
current). I have a cheapy motor that lights LEDs better that I can show you.
Transistors
Act like switches
electricity flicks the switch instead of your finger
TIP
120 collector collector
base
base
emitter emitter
TIP120
base
collector emitter schematic symbol how it kind of works
The differences between the pins are very important. The names aren’t that important, but their
functions are. The “base” is the input that you use to open and close the “switch” across the
“collector” and “emitter”. On this type of transistor (called an NPN), you need to make sure the
collector is always more positive than the emitter. Generally you do this by connecting the emitter
to ground.
Switching Motors with
Transistors
little motor big motor
DC motor +
DC motor
M M
+
resistor resistor big power
transistor transistor source
+ switch + switch +
battery
resistor
line switch
transistor
+
battery
schematic symbol
e c b
+5V
+5V
DC motor
1N4001
Arduino M
board
b c
pin 9 TIP120
500 e
(green-brown-brown)
gnd white diode line into +5V
motor across diode
Sketch
“SerialMotorSpeed”
+9V
+5V battery
9V
+5V
battery
DC motor
1N4001
Arduino M
board
pin 9 TIP120
500
(green-brown-brown)
gnd
motor w/ tape
propellor
desk ding from
motor getting loose
+5V
1N4004 5V relay
Arduino
board
1k
pin 7 toload
to load: light bulb, car ignition, washing machine, etc.
TIP120
gnd
Relay switcher
Just on/off, and a relay needs a diode too
I have a demo piezo igniter from one of these lighters. It’s fun to shock yourself.
Puts out several thousand volts. (ionization voltage of air =~ 30kV/cm)
Piezo Knock Sensor
• To read a piezo you can
just hook it into an
analog input, but:
•
Arduino
You need to drain off piezo
buzzer
board
up (brown,
black,
green)
+
analog pin 2
– gnd
1M
(brown,
black,
green)
Could also plug it directly into the Arduino, might be easier because of those thin little wires on the
piezo.
Piezo Knock
“PiezoKnock”
Number is “t”, the number of times it looped waiting for the value to drop below THRESHOLD.
Notice how it doesn’t work quite right.
How Does that Work?
• When a piezo is struck, it “rings” like a bell
• But instead of sound, it outputs voltage
• The sketch measures time above a certain
voltage, hoping to catch largest ring
volts
time
t
whack!
Depending on how fast you can watch the input, this technique works either really well or not that
well. There are much faster ways of watching inputs that loops with analogRead()
But for now it works okay
Custom Piezo Sensors
Can mount the element on anything
(under rugs, floor mat, door, your body, etc.)
buttons
I used this during Halloween a few years ago to trigger scary sounds.
Or Trigger Actuators
“PiezoMotorPulse”
+5V
Arduino DC motor
1N4001
piezo board M
buzzer
+
analog pin 2 pin 9 TIP120
– 500
gnd (green,
1M
brown,
(brown, brown)
black,
green)
Arduino
+5V board
50k pin 2
pot
gnd
gnd
And if you wire up an LED to pin 9, you can try out the “PotDimmer” sketch again to make sure
things are wired up right.
Processing
• Processing makes Java programming
as fun & easy as Arduino makes AVR
programming
• First, “install”
Processing
• Load up
“Examples » Topics
» Motion »
Bounce”
The built-in serial library adds a new function you can use to your sketch: serialEvent()
The serialEvent() function will get called whenever serial data is available.
Or you can poll with port.available().
Processing Serial
common Processing serial use
4.
Read knob,
send it’s value
Notice the bug that happens when you change the size near a border.
And Another One
“ArduinoPong”
The zipfile for the “minim” library is in the handout, called “minim-1.1-lib.zip”.
Unzip it and place the “minim” folder in the “Processing 0133/libraries” folder.
Adding Processing Libraries
Unzip, drop into “libraries” folder
unzip open
drag
This is not to build, just quickly cover. It’s not in the handout, but,
full details at: http://todbot.com/blog/2006/10/23/diy-ambient-orb-with-arduino-update/
Going Further
• DC motors
• Get some gearhead motors for serious
torque or slower RPM
• Use Lego, Erector, Meccano to build
mechanical linkages for motors
• Oh and you can now build a robot
Going Further
• Transistor switches
• Anytime you need to switch a signal more
powerful than what Arduino can use
• These transistors switch up to 1 amp of DC
voltage. For AC household currents, use
transistor to switch a relay
• Can control just about anything in your house
Going Further
• Processing & Serial communications
• Processing can talk to the Net. It’s an
Internet-to-Arduino gateway
• It can also talk to many computer
peripherals, like video cameras
• Maybe: Arduino controls the motors,
laptop controls the cameras of your robot
END Class 3
http://todbot.com/blog/bionicarduino/
Tod E. Kurt
tod@todbot.com
Class 4
compile
upload
l in k
b k
l i n
b sketch runs
Change the “delay()” values to change blink rate
Pulse Width Modulation
• More commonly called “PWM”
• Computers can’t output analog voltages
• Only digital voltages (0 volts or 5 volts)
• But you can fake it
• if you average a digital signal flipping
between two voltages.
• For example...
PWM
Output voltage is averaged from on vs. off time
output_voltage = (on_time / off_time) * max_voltage
5 volts
3.75 Volts
0 volts
75% 25% 75% 25% 75% 25%
5 volts
2.5 Volts
0 volts
50% 50% 50% 50% 50% 50%
5 volts
• Three characteristics of
PWM signals
width
• Pulse width range (min/max)
• Pulse period
(= 1/pulses per second)
height
• Voltage levels
(0-5V, for instance)
period
More specifically, these are R/C hobby servos used by remote control enthusiasts
In general, “servomotor” is a motor with an inherent feedback mechanism that allows you to send
position commands to it without requiring you to do the position reading.
Servos are Awesome
• DC motor
• High-torque gearing
• Potentiometer to
read position
• Feedback circuitry to
read pot and control
motor
• All built in, you just
feed it a PWM signal
With these little blue ones you have, you can see inside a bit at the internals of the servo.
Servos, good for what?
(and go ahead and add a wire marker to your servo like the above)
Put the red “arm” on your servo. Needs a philips screwdriver.
Many commercial servo drivers have a calibration setting to deal with servo variability
Servo and Arduino
First, add some jumper wires to the servo connector
Gnd
Power
PWM control
Plug power
wires in
Unix is rad.
Robo Cat Toy Idea
Uses “millis()” to
know what time it is
Inside your task slices, avoid using delay(), for loops, and other code structures that would cause
the code to stay inside a task for too long
This is called “cooperative multitasking”, and it’s how OSs in the 80s worked.
Arduino PWM
why all the software, doesn’t Arduino have PWM?
TX RX clock
data A->B
Device A Device B Device A Device B
data B->A
RX TX
Each device must have good “rhythm” Neither needs good rhythm, but one is the conductor
Is one better than the other? It depends on your application. Async is good if there are only two
devices and they’re both pre-configured to agree on the speed (like your Arduino sketches)
Synchronous is generally better for faster speeds (because you don’t need an accurate clock, just
the ability to watch the clock wire).
I2C, aka “Two-wire”
Synchronous serial bus with shared a data line
a little network for your gadgets
SCK clock
Master
device
SDA data
non-volatile
memory
touch sensor compass
fm transmitter
Does all the hard PWM & waveform generation for you
You should be able to buy these from Sparkfun.com in a month or so.
Nintendo Wii Nunchuck
• Standard I2C interface
• 2 buttons
• $20
If you look at the architecture for the Nintendo Wii and its peripherals, you see an almost un-Nintendo adherence
to standards. The Wii controllers are the most obvioius examples of this. The Wii controller bus is standard I2C.
The Wii remote speaks Bluetooth HID to the Wii (or your Mac or PC)
Because it uses standard I2C, it’s easy to make the Nunchuck work with Arduino, Basic Stamp or most other
microcontrollers.
See: http://www.wiili.org/index.php/Wiimote/Extension_Controllers/Nunchuk
and: http://www.windmeadow.com/node/42
and: http://todbot.com/blog/2007/10/25/boarduino-wii-nunchuck-servo/
And then there’s the Wii Remote, besides Bluetooth HID, it also has accelerometers, buttons, speaker, memory, and
is I2C master.
Accelerometer?
• Measures acceleration
(changes in speed)
• Gravity is acceleration
SCK
Arduino “Wire” library
Writing Data
Load Wire library
Join I2C bus
(as master)
Start sending
Send data
Stop sending
And what the various commands do are documented in the instructions / datasheet for a particular
device.
Arduino “Wire” library
Reading Data
Get data
And what the various commands do are documented in the instructions / datasheet for a particular
device.
Wiring up the Nunchuck
We could hack off the connector
and use the wires directly
n/c +V SCK
+V SDA
Note there *are* labels on the adapter, but they’re wrong. So you’ll have to trust the diagrams
above
Wiring it Up
SCK (pin5)
SDA (pin 4)
+5V SCK
GND SDA
Pluggin’ in the ‘chuck
Trying the Nunchuck
“NunchuckPrint”
You’re a cyborg!
switches/buttons
resistive sensors
LEDs Z
X
piezos
serial
pulse width communication
modulation
I2C
analog I/O
http://todbot.com/blog/bionicarduino/
Tod E. Kurt
tod@todbot.com