Arduino - Department of Computer Science

advertisement
Using Arduino
Material taken from Todbot blog
Bionic Arduino
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
What is the Arduino
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Arduino Duemilanove
• 13 digital input/output pins
– Including 6 with PWM for analog output
• 6 analog input pins
• USB connection
– For serial I/O and uploading board
• ATmega168 or ATmega328 processor
– 16k or 32k for program, 1k or 2k for data
• Arduino bootloader
• ~ $30
• More Info
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Other Arduino Boards
• Arduino Mega
– Lots of I/O pins and lots (128k) of program space
– ~ $65
• LilyPad
– For wearable applications
– ~ $20
• Boarduino
– Fits within a breadboard
– ~ $20, but must be soldered!
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Activity 1
• Start the Arduino IDE from the terminal
window with the command: arduino
• Make sure to connect the PC to the Arduino
using the USB cable.
• Designate the connection port and processor
type.
• Try out the “blink” example
– Select: File > Examples > Digital > Blink
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Connect the USB Cable
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Select Connection and Type
atmega328
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Arduino IDE
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Status Messages
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Activity 1 Modified---Add an
External LED to pin 13
• File > Examples > Digital > Blink
• LED’s have polarity
– Negative indicated by flat side of the housing and
a short leg
www.instructables.com
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Terminology
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Arduino Control
• Very much like Processing/Java
– if, then, while, switch
– Functions, expressions
• setup()
– Runs once at beginning (or reset)
• loop()
– Runs continuously
• Like draw()
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Arduino Digital I/O
• pinMode(pin, mode)
– Sets pin to either INPUT or OUTPUT
• digitalRead(pin)
– Reads HIGH or LOW from a pin
• digitalWrite(pin, value)
– Writes HIGH or LOW to a pin
• Electronic stuff you can ignore
– Output pins can provide 40 mA of current
– Writing HIGH to an input pin installs a 20KΩ pullup
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Arduino Timing
• delay(ms)
– Pauses for a few milliseconds
• delayMicroseconds(us)
– Pauses for a few microseconds
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Circuits
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Breadboard Fundamentals
Connect this column
to GND (0 volts)
Connect this
column to 5V
All holes in each of the four long
columns are connected internally.
Use these to route power and ground.
Connect this column
to GND (0 volts)
Connect this
column to 5V
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
There are ten holes that form the rows.
These are divided into two sets of five holes.
All holes in each set of five are connected
Activity 2
• File > Examples > Digital > Melody
• Speakers have polarity. Positive indicated by
small + or dot on piezoelectric speaker.
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Activity 2
• Get a breadboard and a piezoelectric speaker
• Assemble as shown
Image credit: Tod Kurt
• Try out the built-in “melody” example
– Select: File > Examples > Digital > toneMelody
• Try this version of Melody.pde
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Digital? Analog?
•
•
•
•
Digital has two values: on and off
Analog has many (infinite) values
Computers don’t really do analog, they quantize
Remember the 6 analog input pins---here’s how
they work
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Image credit: Tod Kurt
Analog to Digital
• Number of states
or “bins” is the
resolution
• Arduino resolution
is 10-bits (1024
values)
• 5/1024 = 4.8 mV
smallest voltage
change you can
measure
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Image credit: Eoin Brazil
Analog Output
• Computers can’t output analog voltages
– Only digital voltages (0 volts or 5 volts)
• Simulating an analog signal with Pulse Width
Modulation (PWM)
– Can’t directly supply 2.5V, but can pulse the output
on and off really fast to produce the same effect--the same effective voltage.
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Analog Output
Vary the
effective voltage
by modulating
the width of
the high
pulse
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Image credit: Tod Kurt
Activity 3
• Hardware Needed:
–
–
–
–
Resistor (in 220-500 Ohm range)
Breadboard
LED
Wire
• Assemble as shown
(MOVE the piezo
speaker to pin 7)
Image credit: Tod Kurt
–Select: File > Examples > Analog > Fading
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
PWM is Everywhere
• Lamp dimmers, motor, speed control, power
supplies, noise making, etc.
• Now on to analog input using sensors--DON’T DISASSEMBLE your breadboards
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Sensing the Dark: Photoresistors
• a.k.a. a photocell or light-dependent resistor
• It is a variable resistor
– Brighter light => lower resistance
• Create a voltage divider to use the photoresistor
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Image credit: Tod Kurt
Activity 4
• Hardware Needed:
–
–
–
–
Resistor (approx. 10k Ohm)
Breadboard
Photoresistor
Wire
• Assemble as shown
Image credit: Tod Kurt
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
A Theremin
• Create a theremin using the photoresistor
– A spooky sound machine
• Works by measuring your body’s electric field
– We’ll use light to control the tone produced
• We’ll also control the brightness of the LED
using the photoresistor.
• Verify your breadboard configuration!
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Theremin Breadboard Configuration
Hardware Needed: Resistors, Breadboard, Wire,
Photoresistor, Piezo speaker, LED
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
The Theremin Program
Load and run the
TherminAndLED
program
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Sensors
• There are many analog sensors that could be
used in place of the photoresistor
• Check out our “theremin” demos created using
other sensors
Image credit: Tod Kurt
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Communication Between the
Arduino and Your Computer
Arduino
Program
Serial (TTL)
Communication
Arduino
Board
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Processing
Program
Your
Computer
Processing
• Includes libraries supporting serial
communication, video capture, audio
processing…
• VERY IMPORTANT: enter the following
command in the terminal window to update
the serial library:
~brock/bin/serialfix
• Start Processing from the terminal window
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Activity 5
• Hardware Needed:
–
–
–
–
Resistor (10k Ohm)
Breadboard
Photoresistor
Wire
• Don’t reassemble - this is
the same schematic as
Activity 4.
Image credit: Tod Kurt
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Activity 5
• Open SimpleWrite.pde into the
Arduino IDE.
• Open SimpleRead.pde into Processing.
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Serial Communication in Arduino
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Serial Communication in Processing
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Clarifications
• SimpleWrite spends the first second sampling
the photocell readings and setting limits.
• The Arduino program restarts when the
Processing program starts.
• If the Processing program begins behaving
badly, try pressing the restart button on the
Arduino.
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Project 2
• Project 2
Dean Brock, Rebecca Bruce and Susan Reiser, CCSC SE 2009
Download