20130923 教案 001 - tp.edu.tw

27
Editor: 洪正聰(中華科技大學電子系) Arduino 介紹 一、誰 據說 Massimo Banzi 之前是義大利 Ivrea 一家高科技設計學校的老師。他的學生們經常抱怨找 不到便宜好用的微控制器。 2005 年冬天, Massimo Banzi David Cuartielles 討論了這個問題。David Cuartielles 是一 個西班牙籍晶片工程師,當時在這所學校做訪問學者。兩人決定設計自己的電路板,並引入了 Banzi 的學生 David Mellis 為電路板設計編程語言。兩天以後, David Mellis 就寫出了程式碼。 又過了三天,電路板就完工了。這塊電路板被命名為 Arduino發源地是義大利的 Ivrea 鎮,所以發起人以此鎮的歷史人物 Arduin 命名。"Arduino "是個義大利 的男子名,意思是"strong friend"http://zh.forvo.com/word/arduino/ 幾乎任何人,即使不懂電腦編程,也能用 Arduino 做出很酷的東西,比如對感測器作出回應, 閃爍燈光,還能控制馬達。隨後 BanziCuartielles,和 Mellis 把設計圖放到了網上。保持設計 的開放源碼理念,因為版權法可以監管開源軟體,卻很難用在硬體上,他們決定採用共享創意許 可。共享創意是為保護開放版權行為而出現的類似 GPL 的一種許可(license)。在共享創意許 可下,任何人都被允許生產電路板的複製品,還能重新設計,甚至銷售原設計的複製品。你不需 要付版稅,甚至不用取得 Arduino 團隊的許可。 然而,如果你重新發佈了引用設計,你必須說明原始 Arduino 團隊的貢獻。如果你調整或改動 了電路板,你的最新設計必須使用相同或類似的共享創意許可,以保證新版本的 Arduino 電路板 也會一樣的自由和開放。唯一被保留的只有 Arduino 這個名字。它被註冊成了商標。如果有人想 用這個名字賣電路板,那他們可能必須付一點商標費用給 Arduino 的核心開發團隊成員。

Upload: others

Post on 31-Jan-2022

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 20130923 教案 001 - tp.edu.tw

Editor:  洪正聰(中華科技大學電子系)

 

Arduino  介紹  

一、誰 

據說 Massimo Banzi 之前是義大利 Ivrea 一家高科技設計學校的老師。他的學生們經常抱怨找

不到便宜好用的微控制器。

2005 年冬天, Massimo Banzi 跟 David Cuartielles 討論了這個問題。David Cuartielles 是一

個西班牙籍晶片工程師,當時在這所學校做訪問學者。兩人決定設計自己的電路板,並引入了

Banzi 的學生 David Mellis 為電路板設計編程語言。兩天以後,David Mellis 就寫出了程式碼。

又過了三天,電路板就完工了。這塊電路板被命名為 Arduino。

發源地是義大利的 Ivrea 鎮,所以發起人以此鎮的歷史人物 Arduin 命名。"Arduino"是個義大利

的男子名,意思是"strong friend"。

http://zh.forvo.com/word/arduino/

幾乎任何人,即使不懂電腦編程,也能用 Arduino 做出很酷的東西,比如對感測器作出回應,

閃爍燈光,還能控制馬達。隨後 Banzi,Cuartielles,和 Mellis 把設計圖放到了網上。保持設計

的開放源碼理念,因為版權法可以監管開源軟體,卻很難用在硬體上,他們決定採用共享創意許

可。共享創意是為保護開放版權行為而出現的類似 GPL 的一種許可(license)。在共享創意許

可下,任何人都被允許生產電路板的複製品,還能重新設計,甚至銷售原設計的複製品。你不需

要付版稅,甚至不用取得 Arduino 團隊的許可。

然而,如果你重新發佈了引用設計,你必須說明原始 Arduino 團隊的貢獻。如果你調整或改動

了電路板,你的最新設計必須使用相同或類似的共享創意許可,以保證新版本的 Arduino 電路板

也會一樣的自由和開放。唯一被保留的只有 Arduino 這個名字。它被註冊成了商標。如果有人想

用這個名字賣電路板,那他們可能必須付一點商標費用給 Arduino 的核心開發團隊成員。 

Page 2: 20130923 教案 001 - tp.edu.tw

  

二、版本 

 

 

Page 3: 20130923 教案 001 - tp.edu.tw

 

 三、模組 

Arduino shield 

Page 4: 20130923 教案 001 - tp.edu.tw

 

 

Page 5: 20130923 教案 001 - tp.edu.tw

  

 

Page 6: 20130923 教案 001 - tp.edu.tw

 

 

Page 7: 20130923 教案 001 - tp.edu.tw

 

 

 

四、感測器 

Page 8: 20130923 教案 001 - tp.edu.tw

 

 

Page 9: 20130923 教案 001 - tp.edu.tw

 

 

Page 10: 20130923 教案 001 - tp.edu.tw

 五、支援作業系統 

   

Windows

Mac OS X

Linux: 32 bit, 64 bit

六、UNO 

http://arduino.cc/en/Main/ArduinoBoardUno   

Arduino Uno R3 Front:

 

Page 11: 20130923 教案 001 - tp.edu.tw

 

Arduino Uno R3 Back:

  

 

 

  

 

 

Page 12: 20130923 教案 001 - tp.edu.tw

 

ATmega328 : datasheet http://www.atmel.com/Images/doc8161.pdf

  

 

Page 13: 20130923 教案 001 - tp.edu.tw

 

 

 

Page 14: 20130923 教案 001 - tp.edu.tw

 

硬體規格:

微控制器 ATmega328

工作電壓 5V

輸入電壓(建議) 7-12V

輸入電壓(限制) 6-20V

數位 I/O Pins 14 支(其中有 6 支腳位可提供 PWM 輸出)

類入 Input Pins 6 支

I/O pin 直流電流 40mA

3.3V pin 直流電流 50mA

Flash 記憶體 32KB, 其中 0.5KB 拿去給 bootloader 使用

SRAM 2KB

EEPROM 1KB

時脈 16MHz

數位 I/O Pins:

14 支數位 I/O Pins 可以當作 input 使用,也可以當作 output 使用,使用方法是透過 pinMode(),

digitalWrite(), and digitalRead() 這幾個函式。這 14 支數位 I/O Pins,其中幾支腳有特殊的功能:

Page 15: 20130923 教案 001 - tp.edu.tw

Serial 通訊 0(RX) 和 1 (TX) 這兩支腳。用來接收(RX)與傳輸(TX) TTL 訊號的序列資

料。這兩支腳也連接到 USB Converter 晶片中。

外部中斷 2 和 3 這兩支腳。這兩支腳可以利用外部事件觸發中斷。詳細內容請參考

attachInterrupt() 函式。

PWM 3, 5, 6, 9, 10 和 11 共六支腳。透過 analogWrite() 函式可以提供 8-bit

的 PWM 輸出。

SPI 10 (SS), 11 (MOSI), 12 (MISO) 和 13 (SCK) 這四支腳。這四支腳搭配 SPI

Library 可提供 SPI 序列通訊。

LED 13。內建一顆 LED,當 pin 腳為 HIGH 時,LED 打開,當 pin 腳為 LOW 時,

LED 關閉。

類比輸入 Pins:

Arduino Uno 有 6 支類比輸入腳,標記為 A0 到 A5,每支腳都可提供 10 位元的解析 (即 1024 種不同

的數值)。這些腳位所用的參考電壓預設為 0 到 5V,不過參考電壓也是可以更改的,方法是透過 AREF 腳

和 analogReference() 函式。

另外,有幾支腳也有特殊功能:

I2C 4 (SDA) 和 5 (SCL) 這兩支腳。透過 Wire library 可以提供 I2C 通訊。

其它:

AREF 類比輸入的參考電壓,搭配 analogReference() 函式一起使用。

Reset 當 Reset 腳為 LOW 時,微控制器會重置。

 

Power

The Arduino Uno can be powered via the USB connection or with an external power supply. The

power source is selected automatically.

External (non-USB) power can come either from an AC-to-DC adapter (wall-wart) or battery. The

adapter can be connected by plugging a 2.1mm center-positive plug into the board's power jack.

Leads from a battery can be inserted in the Gnd and Vin pin headers of the POWER connector.

The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however,

the 5V pin may supply less than five volts and the board may be unstable. If using more than 12V,

the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.

The power pins are as follows:

VIN. The input voltage to the Arduino board when it's using an external power source (as opposed

to 5 volts from the USB connection or other regulated power source). You can supply voltage

through this pin, or, if supplying voltage via the power jack, access it through this pin.

Page 16: 20130923 教案 001 - tp.edu.tw

5V.This pin outputs a regulated 5V from the regulator on the board. The board can be supplied

with power either from the DC power jack (7 - 12V), the USB connector (5V), or the VIN pin of the

board (7-12V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage

your board. We don't advise it.

3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA.

GND. Ground pins.

Memory

The ATmega328 has 32 KB (with 0.5 KB used for the bootloader). It also has 2 KB of SRAM and 1

KB of EEPROM (which can be read and written with the EEPROM library).

Input and Output

Each of the 14 digital pins on the Uno can be used as an input or output,

using pinMode(), digitalWrite(), and digitalRead()functions. They operate at 5 volts. Each pin can

provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by

default) of 20-50 kOhms. In addition, some pins have specialized functions:

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins

are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.

External Interrupts: 2 and 3. These pins can be configured to trigger an interrupt on a low

value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.

PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.

SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). These pins support SPI communication using

the SPI library.

LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED

is on, when the pin is LOW, it's off.

The Uno has 6 analog inputs, labeled A0 through A5, each of which provide 10 bits of resolution

(i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to

change the upper end of their range using the AREF pin and the analogReference() function.

Additionally, some pins have specialized functionality:

TWI: A4 or SDA pin and A5 or SCL pin. Support TWI communication using the Wire library.

There are a couple of other pins on the board:

AREF. Reference voltage for the analog inputs. Used with analogReference().

Reset. Bring this line LOW to reset the microcontroller. Typically used to add a reset button to

shields which block the one on the board.

Page 17: 20130923 教案 001 - tp.edu.tw

See also the mapping between Arduino pins and ATmega328 ports. The mapping for the Atmega8,

168, and 328 is identical.

Communicat ion

The Arduino Uno has a number of facilities for communicating with a computer, another Arduino,

or other microcontrollers. The ATmega328 provides UART TTL (5V) serial communication, which

is available on digital pins 0 (RX) and 1 (TX). An ATmega16U2 on the board channels this serial

communication over USB and appears as a virtual com port to software on the computer. The

'16U2 firmware uses the standard USB COM drivers, and no external driver is needed.

However, on Windows, a .inf file is required. The Arduino software includes a serial monitor which

allows simple textual data to be sent to and from the Arduino board. The RX and TX LEDs on the

board will flash when data is being transmitted via the USB-to-serial chip and USB connection to

the computer (but not for serial communication on pins 0 and 1).

A SoftwareSerial library allows for serial communication on any of the Uno's digital pins.

The ATmega328 also supports I2C (TWI) and SPI communication. The Arduino software includes

a Wire library to simplify use of the I2C bus; see the documentation for details. For SPI

communication, use the SPI library.

Programming

The Arduino Uno can be programmed with the Arduino software (download). Select "Arduino Uno

from the Tools > Board menu (according to the microcontroller on your board). For details, see

the reference and tutorials.

The ATmega328 on the Arduino Uno comes preburned with a bootloader that allows you to upload

new code to it without the use of an external hardware programmer. It communicates using the

original STK500 protocol (reference, C header files).

You can also bypass the bootloader and program the microcontroller through the ICSP (In-Circuit

Serial Programming) header; see these instructions for details.

The ATmega16U2 (or 8U2 in the rev1 and rev2 boards) firmware source code is available .

The ATmega16U2/8U2 is loaded with a DFU bootloader, which can be activated by:

On Rev1 boards: connecting the solder jumper on the back of the board (near the map of Italy) and

then resetting the 8U2.

On Rev2 or later boards: there is a resistor that pulling the 8U2/16U2 HWB line to ground, making

it easier to put into DFU mode.

You can then use Atmel's FLIP software (Windows) or the DFU programmer (Mac OS X and Linux)

to load a new firmware. Or you can use the ISP header with an external programmer (overwriting

the DFU bootloader). See this user-contributed tutorial for more information.

Page 18: 20130923 教案 001 - tp.edu.tw

Automat ic (Sof tware) Reset

Rather than requiring a physical press of the reset button before an upload, the Arduino Uno is

designed in a way that allows it to be reset by software running on a connected computer. One of

the hardware flow control lines (DTR) of theATmega8U2/16U2 is connected to the reset line of

the ATmega328 via a 100 nanofarad capacitor. When this line is asserted (taken low), the reset line

drops long enough to reset the chip. The Arduino software uses this capability to allow you to

upload code by simply pressing the upload button in the Arduino environment. This means that

the bootloader can have a shorter timeout, as the lowering of DTR can be well-coordinated with the

start of the upload.

This setup has other implications. When the Uno is connected to either a computer running Mac

OS X or Linux, it resets each time a connection is made to it from software (via USB). For the

following half-second or so, the bootloader is running on the Uno. While it is programmed to

ignore malformed data (i.e. anything besides an upload of new code), it will intercept the first few

bytes of data sent to the board after a connection is opened. If a sketch running on the board

receives one-time configuration or other data when it first starts, make sure that the software with

which it communicates waits a second after opening the connection and before sending this data.

The Uno contains a trace that can be cut to disable the auto-reset. The pads on either side of the

trace can be soldered together to re-enable it. It's labeled "RESET-EN". You may also be able to

disable the auto-reset by connecting a 110 ohm resistor from 5V to the reset line; see this forum

thread for details.

USB Overcurrent Protect ion

The Arduino Uno has a resettable polyfuse that protects your computer's USB ports from shorts

and overcurrent. Although most computers provide their own internal protection, the fuse

provides an extra layer of protection. If more than 500 mA is applied to the USB port, the fuse will

automatically break the connection until the short or overload is removed.

Phys ica l Character is t ics

The maximum length and width of the Uno PCB are 2.7 and 2.1 inches respectively, with the USB

connector and power jack extending beyond the former dimension. Four screw holes allow the

board to be attached to a surface or case. Note that the distance between digital pins 7 and 8 is 160

mil (0.16"), not an even multiple of the 100 mil spacing of the other pins.

 

 

1. open-source Arduino environment:

http://arduino.cc/en/Main/Software   

Download Arduino 1.0.5 (release notes), hosted by Google Code:

Page 19: 20130923 教案 001 - tp.edu.tw

Windows

Mac OS X

Linux: 32 bit, 64 bit

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Page 20: 20130923 教案 001 - tp.edu.tw

 

 

 

 

Wiring-based language (syntax and libraries), similar to C++ with some slight simplifications and

modifications, and a Processing-based integrated development environment.[

http://en.wikipedia.org/wiki/Arduino  http://arduino.cc/en/Reference/HomePage

 

 

 

一 、 Standard Libraries

EEPROM - reading and writing to "permanent" storage

Ethernet - for connecting to the internet using the Arduino Ethernet Shield

Firmata - for communicating with applications on the computer using a standard

serial protocol.

LiquidCrystal - for controlling liquid crystal displays (LCDs)

Page 21: 20130923 教案 001 - tp.edu.tw

SD - for reading and writing SD cards

Servo - for controlling servo motors

SPI - for communicating with devices using the Serial Peripheral Interface (SPI) Bus

SoftwareSerial - for serial communication on any digital pins

Stepper - for controlling stepper motors

Wire - Two Wire Interface (TWI/I2C) for sending and receiving data over a net of

devices or sensors.

二 、 Leonardo Only Libraries

Keyboard - Send keystrokes to an attached computer.

Mouse - Control cursor movement on a connected computer.

三 、 Contributed Libraries  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Page 22: 20130923 教案 001 - tp.edu.tw

 

 

 

Code 

The example code is very simple, credits are to be found in the comments.

   

/* Blinking LED 

  * ‐‐‐‐‐‐‐‐‐‐‐‐ 

  * 

  * turns on and off a light emitting diode(LED) connected to a digital     

  * pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino   

  * board because it has a resistor attached to it, needing only an LED 

 

  * 

  * Created 1 June 2005 

  * copyleft 2005 DojoDave <http://www.0j0.org> 

  * http://arduino.berlios.de 

  * 

  * based on an orginal by H. Barragan for the Wiring i/o board 

  */ 

 

int ledPin = 13;                                  // LED connected to digital pin 13 

 

void setup() 

    pinMode(ledPin, OUTPUT);            // sets the digital pin as output 

 

void loop() 

Page 23: 20130923 教案 001 - tp.edu.tw

    digitalWrite(ledPin, HIGH);      // sets the LED on 

    delay(1000);                                    // waits for a second 

    digitalWrite(ledPin, LOW);        // sets the LED off 

    delay(1000);                                    // waits for a second 

}

 

 

 

 

 

Page 24: 20130923 教案 001 - tp.edu.tw

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Page 25: 20130923 教案 001 - tp.edu.tw

七、參考資料 

參考資料:

 

 

 

八、參考實作 

Button: http://www.seeedstudio.com/wiki/GROVE_-_Starter_Kit_V0.9b

http://www.arduino.cc/en/Tutorial/button

LED: http://www.seeedstudio.com/wiki/GROVE_-_Starter_Kit_V0.9b

程式

http://www.arduino.cc/en/Tutorial/BlinkingLED

Tilt switch:

http://www.seeedstudio.com/wiki/GROVE_-_Starter_Kit_V0.9b

http://www.arduino.cc/en/Tutorial/TiltSensor

Light Sensor:

http://www.seeedstudio.com/wiki/Grove_-_Light_Sensor

Rotary angle sensor

http://www.seeedstudio.com/wiki/GROVE_-_Starter_Kit_v1.1b#Grove_-_Rot

ary_Angle_Sensor

Sound Sensor: http://www.seeedstudio.com/wiki/Grove_-_Sound_Sensor

int ledPin = 13;

int time = 1000;

void setup() {

pinMode(ledPin, OUTPUT);

Serial.begin(9600);

Serial.println("\n[memCheck]");

Page 26: 20130923 教案 001 - tp.edu.tw

Serial.println(freeRAM(), DEC);

}

void loop() {

digitalWrite(ledPin, HIGH);

delay(time);

digitalWrite(ledPin, LOW);

delay(time);

}

int freeRAM() {

extern int __heap_start, *__brkval;

int v;

return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int)

__brkval);

}

int k=0;

void setup()

{

Serial.begin(9600);

}

void loop()

{

char c;

if(Serial.available()>0)

{

c=Serial.read();

Serial.print(c);

k=1;

}

else

{ if (k!=0)

{

Serial.println();

k=0;

Page 27: 20130923 教案 001 - tp.edu.tw

}

}

delay(100);

}