Professional Documents
Culture Documents
1 -7
Arduino
2014
arduino.
(12.10)
, ,
.
(Hands on Training).
arduino.
,
.
.
.
. ,
Arduino .
1,2 8
Arduino
: 1989
: 2084
: . ,
, 2012
.............................................................................................................................................. vi
1 ............................................................................................................................................. 1
Arduino ....................................................................................................................................................... 1
2 ............................................................................................................................................. 4
Arduino Uno ............................................................................................................................................... 4
2.1
.......................................................................................................................................... 4
2.2
2.3
........................................................................................................................... 5
2.4
Arduino ................................................................................................... 6
2.5
......................................................................................................................... 6
2.6
............................................................................................................................... 7
2.7
............................................................................................................................... 8
2.8
..................................................................................................... 8
2.9
........................................................................................... 8
2.10
Arduino ........................................................13
2.11
2.12
H .....................................................................................................15
2.13
.............................................................15
2.14
2.15
2.16
(Libraries) ...................................................................................20
vi
8 .........................................................................................................................................100
Processing .............................................................................................................................................100
8.1
Processing..........................................................................................100
8.2
Processing .....................................................................................100
8.3
.................................................................................................101
8.4
.....................................................................................................102
8.5
5: .......................103
9 .........................................................................................................................................110
.................................................................................................110
viii
1 : Arduino
1
Arduino
Arduino
Smart Project. , Arduino
2005 ,
Ivrea
.
, Arduino
, .
, ,
/
. Arduino
( )
.
.
.
; Arduino .
,
Arduino
. , Projects
( hardware) software /
( Flash, Processing, MaxMSP). Arduino
Atmega382 chip FTDI
.
Arduino
(BootLoader).
Flash Memory Arduino USB .
, ,
Arduino
.
1 : Arduino
Arduino:
: Arduino
. ,
.
:
Arduino
.
: Arduino
. ,
. ,
.
Arduino
,
:
Arduino Uno
Arduino Diecimila
Arduino Duemilanove
Arduino Mega1280
Arduino Mega2560
Arduino Mini
Arduino Nano
Arduino USB
Arduino Stamp
Arduino Fio
Arduino NG
Arduino NG+
Arduino Extreme
Arduino Bluetooth
LilyPad Arduino
Serial Arduino
Arduino
.
Arduino
Atmel AVR
Flash
KiB
EEPROM
KiB
SRAM
KiB
Diecimila
Duemilanove
Uno
Mega
Fio
Mega 2560
ATmega168
ATmega168/328
ATmega328
ATmega1280
ATmega328P
ATmega2560
16
16
32
128
32
256
0.5
0.5
1
4
1
4
1
1
2
8
2
8
/
14
14
14
54
14
54
PWM
6
6
6
14
6
14
6
6
6
16
8
16
1 : Arduino
1.1 Arduino
Arduino (IDE)
Java
. IDE Arduino , C,
C++, Arduino, ..
,
Arduino Wiring (C, C++). IDE Arduino
GNU toolchain AVR Libc
C, C++ AVR ,
avrudude Flash
memory Arduino.
Arduino
Creative Commons
Attribution Share-Alike 2.5. , (IDE) Arduino
GNU General Public License
Version2.
2 : Arduino Uno
2
Arduino Uno
2.1
Arduino Uno .
Arduino 14
input/output pins 6 .
20 pins
(, LEDs, LCD ) (Ultrasonic, ,
accelometers .).
USB.
,
, . ,
.
: 2 3.
.
4
10
2 : Arduino Uno
attachInterrupt().
0,1.
2.3
Arduino
. ,
. 0 (RX) 1 (TX),
USB. , (USB),
0 1
.
,
Arduino Arduino.
Serial Monitor
Serial.begin().
11
2 : Arduino Uno
begin()
end()
available()
read()
peek()
flush()
print()
println()
write()
( )
( )
( )
( )
( byte )
( buffer )
( )
( Print(), )
( )
2.4 Arduino
Microcontroller:
:
:
():
Digital I/O Pins:
SRAM:
EEPROM:
Clock Speed:
ATmega328
5V
7-12V
6-20V
14 ( 6 PWM
)
6
40 mA
50 mA
32 KB 0,5 KB
bootloader
2 KB
1 KB
16 MHz
2.5
Arduino :
12
2 : Arduino Uno
, Flash EPROM (
). SRAM
.
SRAM, ,
. ,
, . ,
sketch (
). ,
SRAM.
,
.
2.6
Arduino Uno
2.1mm ( )
Arduino USB .
H .
, 9Volt 220V.
Arduino Vin GND
.
.
6 20 Volts.
7 Volt pin 5Volt
5 Volts. , 12 Volts
. ,
9 Volts.
:
VIN. .
.
5V. 5Volt.
.
Vin , USB
5V
3V3. 50mA.
GND.
13
2 : Arduino Uno
2.7
rduino Uno
, Arduino .
ATMega382 L 5 Volt UART,
( RX) 0 ( TX) 1 .
,
Arduino
ATmega382
chip FTDI. ,
.
Arduino USB
COM
2.2 ATmega328
. firmware ATmega382
USB COM
. , Windows .inf .
Arduino
Arduino. RX TX LED
USB-to-chip USB
( 0 1).
2.8
Arduino Uno Wiring (
C++ ).
.
STK500.
Processing Wiring,
(pen source).
" ".
2.9
,
, .
Windows XP.
14
2 : Arduino Uno
1. Arduino USB
Arduino Uno. USB
.
2. Arduino
Arduino-1.0
http://arduino.cc/en/Main/Software.
3.
Arduino Uno
USB. LED .
4.
+ , .
+ ,
.
15
2 : Arduino Uno
+ Arduino Uno.
.
+ Arduino.
10
16
2 : Arduino Uno
+ drivers .
+ Arduino.
11
17
2 : Arduino Uno
5. Arduino
Arduino.exe
6. Board
, Tools Board
Arduino Uno COM15.
12
18
2 : Arduino Uno
2.10 Arduino
Arduino
, , ,
, .
Arduino .
sketch. sketch
.
/ / .
Arduino
.
,
sketch, sketch .
(Tabs)
2.3 (IDE) Arduino
13
19
2 : Arduino Uno
:
Verify/Compile (/):
Upload:
New(): sketch
Open(): sketch,
Save(): sketch
Serial Monitor( ):
14
20
2 : Arduino Uno
2.12 H
Arduino :
//
void setup ()
{
//
}
void loop ()
{
//
}
sketch Arduino
setup() loop(). setup() , sketch
(reset) Arduino. ,
, (pins)
. , loop()
.
sketch,
.
2.13
,
Arduino :
if
if ... else
for
while
do ... while
switch ... case
break
continue
return
goto
( )
( )
( )
( )
( )
( )
( )
( )
( )
( )
=
+
*
( )
( )
( )
( )
15
21
2 : Arduino Uno
/
%
( )
( )
&&
||
!
( )
( )
( )
&
|
^
~
<<
>>
( )
( )
( )
( )
( )
( )
( )
++
-( )
+=, -=, *=, /=, %=
&=, |=, ^=, ~=, <<=, >>=
( )
( )
==
!=
<
>
<=
>=
()
()
()
()
( )
( )
*
&
( )
( )
HIGH
LOW
false
true
INPUT
OUTPUT
A0, ..., 5
( )
( )
( )
( )
( )
( )
( )
16
22
2 : Arduino Uno
boolean
( )
char
( 8 )
unsigned char
( 8 )
byte
( 8 )
int
( 16 )
unsigned int
( 16 )
word
( 16 )
long
( 32 )
( 32 )
unsigned long
float, double
( )
String
( )
char(), byte()
int(), word(), long()
float(), double()
pinMode()
( )
digitalWrite()
( )
digitalRead()
( )
( )
analogReference()
analogWrite()
( PWM )
analogRead()
( )
tone()
( )
noTone()
( )
shiftOut()
( )
pulseIn()
( s HIGH LOW)
millis()
micros()
delay()
delayMicroseconds()
( ms)
( s)
( - ms)
( - s)
max()
( )
min()
( )
abs()
( )
constrain()
( )
map()
( )
pow()
( )
17
23
2 : Arduino Uno
sqrt()
sin()
cos()
tan()
( )
( )
( )
( )
random()
( )
randomSeed()
( )
lowByte()
( byte )
highByte()
( byte )
bitRead()
( )
bitWrite()
( )
bitSet()
( 1 )
bitClear()
( 0 )
bit()
( 2)
attachInterrupt()
( )
detachInterrupt()
( )
interrupts()
( )
noInterrupts()
( )
Serial
( )
pinMode(): /
.
: pinMode(pin, mode)
:
pin:
.
mode: INPUT/OUTPUT
18
24
2 : Arduino Uno
digitalRead(): ,
HIGH LOW.
: digitalRead(pin)
:
pin:
.
: HIGH/LOW
analogWrite(): (PWM ) .
LED
.
analogWrite(),
analogWrite() (
digitalWrite() digitalRead() ).
PWM 490 Hz. Arduino
3, 5, 6, 9, 10, 11.
: analogWrite(pin, value)
:
pin:
value: 0 255
19
25
2 : Arduino Uno
analogRead(): .
: analogRead(pin)
:
pin:
: 0 1023
2.16 (Libraries)
.
sketch, Sketch Import Library.
#include sketch.
sketch,
. sketch ,
.
,
libraries (),
sketchbook. .
Arduino.
( ):
Messenger -
.
NewSoftSerial - SoftwareSerial.
OneWire - ( Dallas Semiconductor)
one Wire.
PS2Keyboard - PS2.
Simple Message System - Arduino
SSerial2Mobile - e-mail
( AT )
Webduino - web server ( Arduino
Ethernet Shield)
X10 - X10
Xbee - XBees API
SerialControl - Arduino
Servo Servo.
:
Capacitive Sensing -
Debounce - (..
).
20
26
2 : Arduino Uno
LED:
Improved LCD library - LCD
Arduino LCD .
GLCD LCD KS0108 chipset.
LedControl - LED MAX7221
MAX7219.
LedControl - Matrix
LED.
LedDisplay - HCMS-29xx LED.
:
Tone -
.
PWM:
TLC5940 - 16 12 bit PWM .
:
DateTime -
.
MsTime2 - 2
.
:
Streaming - .
[1], [2], [3], [4] [5].
21
27
8: Processing
8
Processing
Processing
,
.
,
,
.
, , ,
Processing ,
.
8.1 Processing
/ GPL/LGPL.
,
GNU/Linux, Mac OS X Windows.
, (2D)
(3D) .
OpelGL 3D.
stand-alone desktop Web-based
(applets).
,
, , ..
Processing
Java,
.
8.2 Processing
Processing .
100
28
8: Processing
Wiring Arduino,
hardware. Wiring, Arduino ,
,
Processing AVR
Atmel. Processing : Design By
Numbers, Fritzing, Mobile Processing, Processing.js, Spde, Processing in Clojure
Processing Monsters.
8.3
Processing :
//
void setup()
{
//
}
void draw()
{
//
}
(Tabs)
9.1: (IDE) Processing
101
29
8: Processing
,
Processing. setup()
.
, , ,
.. setup()
, draw()
. draw() setup()
noLoop().
8.4
Processing
, .
, , , ,
, ..
http://processing.org/reference/libraries. ,
Tools,
.
Arduinoscope
http://code.google.com/p/arduinoscope/downloads/list, ControlP5
http://www.sojamo.de/libraries/controlP5/.
processing-1.5\modes\java\libraries ControlP5 arduinoscope
( ).
[22], [23].
102
9:
:
[] . (2009). , .
[] Banzi, M. (2009). Getting Started with Arduino. O'Reilly.
[C] (2011). .
(
)
Arduino.
:
Arduino
[1] http://arduino.cc/en/Guide/Environment?from=Tutorial.Bootloader
[2] http://arduino.cc/en/Guide/Windows
[3] http://arduino.cc/en/Reference/HomePage
[4] http://arduino.cc/en/Main/ArduinoBoardUno
[5] http://arduino.cc/en/Tutorial/Memory
[6] http://makeprojects.com/Wiki/Servos
[7] http://letsmakerobots.com/
[8] http://makeprojects.com/
L293D
[9]http://luckylarry.co.uk/arduino-projects/control-a-dc-motor-with-arduino-and-l293dchip/
[10]http://www.engineersgarage.com/electronic-components/l293d-motor-driver-ic
PWM
[11] http://en.wikipedia.org/wiki/Pulse-width_modulation
[12] http://arduino.cc/en/Tutorial/PWM
Ping
[13] http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor
[14] https://sites.google.com/site/parallaxinretailstores/home/ping-ultrasonic-distancesensor
Bluetooth
111
9:
[15] http://wiring.org.co/learning/tutorials/bluetooth/
[16] http://www.sparkfun.com/tutorials/67
[17] http://www.arduino.cc/playground/Learning/Tutorial01
PID Controller
[18]http://nefeli.lib.teicrete.gr/browse/stef/hlk/2011/KakolevaAggeliki,TsadakisAndreas/
attached-document-1323080192-956324-349/Kakoleva_Tsantakis2011.pdf
[19] http://en.wikipedia.org/wiki/PID_controller
[20] http://www.grobot.gr/phpBB3/viewtopic.php?f=5&t=14&st=0&sk=t&sd=a
[21] http://arduino.cc/playground/Code/PIDLibrary
Processing
[22] http://www.arduino.cc/playground/Interfacing/Processing
[23] http://www.processing.org/reference/
:
[24] http://arduino.cc/forum/
[25] http://www.instructables.com
[27]http://makeprojects.com/Project/Build-your-own-Arduino-Controlled-Robot-/577/
112
32
3
www.arduino.cc
ARDUINO IDE
o ARDUINO IDE ARDUINO UNO
o
o
3.1
:
Arduino Uno
BreadBroad
USB Arduino
: , LED,
33
3.2 1
Led 1sec,
/ . ( -
)
Arduino pin,
, .
, .
pin HIGH LOW. HIGH
'1' 5V DC, LOW '0' 0V DC
(ground).
pin
Arduino. led board
Blink
Arduino
: LED.
Arduino Board
breadboard
LED, 220,
34
Fritzing. ,
: Fritzing project page
35
[Get Code]
36
3.3 2
Led button polling
37
38
3.4 3
Led button Interrupt
( loop
)
const byte LED = 13; // Integer () LED
13
led.
const byte BUTTON = 2; // Integer () BUTTON
2
button.
// Interrupt Service Routine (ISR)
void pinChange () // pinChange (
{
noInterrupts (); //
if (digitalRead (BUTTON) == HIGH) // Pin 2 (
button) button ,
digitalWrite (LED, HIGH); // 1 (5V) pin 13, led
else //:
digitalWrite (LED, LOW); // 0 (0V) pin 13, led
interrupts (); //
} // end of pinChange
void setup () //
{
pinMode (LED, OUTPUT); // so we can update the LED
// LED, pin 13
0-5V
digitalWrite (BUTTON, HIGH); // internal pull-up resistor
// BUTTON, pin 2
0-5V
attachInterrupt (0, pinChange, CHANGE); // attach interrupt handler
//
. 0
. pinChange
ISR . LOW, RISING, FALLING,
CHANGE .
} // end of setup
void loop () //
{
// loop doing nothing
//
}
39
Arduino pins
Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The table
below shows the available interrupt pins on various boards.
Board
int.0
int.1
int.2
int.3
Uno, Ethernet
2
3
Mega2560
2
3
21
20
Leonardo
3
2
0
1
Due
(see below)
int.4
int.5
19
7
18
The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all
available pins. You can directly specify the pin number in attachInterrupt().
3.5 4
, Led1
Led2 PWM
.
41
:
int sensorPin = A0; // select the input pin for the potentiometer
// Integer () sensorPin
0 ( 0
pin, 1 ADC)
.
int ledPin1 = 13;
// select the pin for the LED1
// Integer () ledPin1
13
led1.
int sensorValue = 0; // variable to store the value coming from the sensor
// Integer () sensorValue
0
ADC
int ledPin2 = 9; // LED2 connected to digital pin 9
// Integer () ledPin2
9
led2. PWM Output
int outputValue = 0;
// value output to the PWM (analog out)
// Integer () outputValue
0
led2
void setup() { //
// declare the ledPin1 as an OUTPUT:
pinMode(ledPin1, OUTPUT); //
ledPin1, pin 13 0-5V
pinMode(ledPin2, OUTPUT); //
ledPin2, pin 9 0-5V
}
void loop() { //
// read the value from the sensor:
sensorValue = analogRead(sensorPin); //
pin 0 ADC sensorValue
// turn the ledPin on
digitalWrite(ledPin1, HIGH); // 1 (5V) pin 13,
led1
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// ms
0 1023 ms
// turn the ledPin off:
digitalWrite(ledPin1, LOW); // 0 (0V) pin 13,
led1
// stop the program for for <sensorValue> milliseconds:
42
delay(sensorValue);
// ms
0 1023 ms
//fade section
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255); //
0 1023 0-255
outputValue
// change the analog out value:
analogWrite(ledPin2, outputValue); // PWM pin 9 (led2)
ADC 0 255
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2); // // 2ms
ADC
}
, .
analogWrite() 0 255, , analogWrite(255)
100% ,
analogWrite(127) 50% ( ) [12].
(PWM)
43
3.6 5
, Led1 Led2 ,
/
:
int sensorPin = A0;
void setup() { //
// declare the ledPin1 as an OUTPUT:
pinMode(ledPin1, OUTPUT); //
ledPin1, pin 13 0-5V
44
pinMode(ledPin2, OUTPUT); //
ledPin1, pin 9 0-5V
// initialize serial communications at 9600 bps:
Serial.begin(9600); // 9600
}
void loop() { //
// read the value from the sensor:
sensorValue = analogRead(sensorPin); //
pin 0 ADC sensorValue
// turn the ledPin on
if (sensorValue < 511 ) { // ADC 511
2,5 volt :
digitalWrite (ledPin1, HIGH); // 1 (5V) pin 13,
led1
digitalWrite (ledPin2, LOW);
// 0 (0V) pin 9,
led2
}
else //:
{
digitalWrite (ledPin1, LOW); // 0 (0V) pin 13,
led1
digitalWrite(ledPin2, HIGH); // 1 (5V) pin 9,
led2
}
//serial section
// print the results to the serial monitor:
//
Serial.print("\t sensor = " );
Serial.println(sensorValue);
45
3.7 6
, EEPROM,
/ EEPROM
#include <EEPROM.h> //
EEPROM
// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0; // addr
0
46
47
3.8 7
Twinkle Twinkle Little Star
:
int speakerPin = 9; // speakerPin
9
buzzer
int length = 15; // the number of notes
// length 15
char notes[] = "ccggaagffeeddc "; // a space represents a rest
// (char)
ccggaagffeeddc
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; //
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4
int tempo = 300; // tempo
300
()
void playTone(int tone, int duration) { // playTone
tone (khz) duration
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
48
delayMicroseconds(tone);
49
3.9 8
Display LCD
50
:
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 7
* LCD Enable pin to digital pin 6
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
51
....
52
53
arduino
arduino
54
ATmega328
55
56