--- tags : Public Resources author : - name : Chris Lin --- # 108 Arduino教學 ## 介紹成員 * 為什麼聯合社課 * 創意機械、資訊研究簡單介紹(專長、成員) ## 介紹專案 * 播影片 * 軟體介紹(為什麼使用Arduino) > [name=Chris Lin]硬體 : 設計好的pin腳、根據需求有多種選擇性(一種語言)、許多周邊開發板和傳感器 > 編程環境 : 整合式編輯器、Arduino核心、內建程式庫、範例sketch、編譯器、燒錄程式 * 工具介紹 ## Arduino - Refers to a whole family of board that have different advantages and disadvantages - Started as a research project at the Interaction Design Institute of Ivrea in the early 2000s - UNO / Nano / Due / mini * UNO : Atmega328, 16MHz, 32KB of program memory, 1KB of EEPROM, 2KB of RAM, 14 digital I/O, 6 analog inputs, 5V and 3.3V(including a power jack) ![](https://i.imgur.com/O7cZM5c.png) * Nano V3.0 : 相較UNO不可接shield,有針腳可放在麵包板上做開發 * Mini * Due : 32-bit Atmel SAM3X8E ARM Cortex-M3 CPU - Arduino IDE runs on Win, MacOS, Linux - 開放原始電路設計圖 ![](https://i.imgur.com/ig8JDdl.png) - 電路可獨自運作或與連接之電腦溝通(serial communication) - Open Source : https://github.com/arduino/Arduino - 討論區 : https://forum.arduino.cc/ - some cool project : https://create.arduino.cc/projecthub ## Arduino Nano ### 簡述 - 單晶片微控制器(ATmega328) Atmel corporation, AVR architecture, megaAVR series ![](https://i.imgur.com/dnAorua.png) > [name=Chris Lin] > - Atmel半導體公司 > - Flash, EEPROM, and SRAM are all integrated onto a single chip > - Have serial interfaces, which can be used to connect larger serial EEPROMs or flash chips - Harvard architecture 8bits RISC(reduced instruction set computer) > [name=Chris Lin]Harvard -> 指令和資料分開 ![](https://i.imgur.com/dnAorua.png) - 技術參數 ![](https://i.imgur.com/s8XtS9R.png =400x400) - Flash memory for program storage > [name=Chris Lin]存放程式和初始資料 > 一區程式碼、一區bootloader > 主機端的avrdude(AVR downloader/ uploader)和bootloader溝通進行燒錄(不須額外燒錄器=>self-programming) > 燒錄工具將編譯後的程式碼燒錄 - SRAM(static random access memory) - EEPROMs(electrically-erasable programmalble read-only memory) > [name=Chris Lin]電子抹除式 多次複寫 ### 腳位 ![](https://i.imgur.com/MsXOHGH.png =300x250) - Power : VIN(7-12V)、5V、3V3(3.3V) > [name=Chris Lin]regulator - Reset : botton, digitalwrite(reset, LOW), resetFunc() - Serial port (UART) : hardwired TX(1)/RX(0) > [name=Chris Lin]Universal Asynchronize Receiver/Transmitter無時脈同步線 > 需傳送時起始通訊程序(協定上有開始和結束) > 名詞 : 序列(串列)、同步、雙工 > 相對於SPI\I2C同步傳輸(用於CPU和周邊晶片) > - Digital Pins : * digitalread()、digitalwrite();pinmode() * PWM(Pulse Width Modulation) : * 利用數位訊號高頻率的切換模擬出類比訊號 * analogWrite(pin, value) : value介於0~255(可算Duty Cycle) * 490 Hz (pins 5, 6: 980 Hz) > [name=Chris Lin]Duty Cycle = Ton/Tof(%) > V = Von * Duty Cycle(%) > 例如開關頻繁使得看起來比較暗(調整脈波工作週期影響平均值來控制電壓) - Analog Pins : * analogRead() * 接收類比訊號(電壓、聲音、光亮、溫度) * ADC(Analog to Digital Converter) - Interrupt(higher priority) : attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) * ISR(Interupt Service Routine) * mode = RISING/FALLING/CHANGE * Interupt occure when voltage changed on assigned pin - SPI(Serial Peripheral Interface) : * MOSI(Master Out Slave In) * MISO(Master In Slave Out) * SS(Slave Select) : en(dis)able device * Device's SS pin **low** => ignore master(AT mode) * **high** => listen to master * SCK(Serial Clock) : the palses which syncronizes the transmitted data from the master - I2C : - When start all pins get pulled low. ### 配置 - Reset botton - mini usb - ICSP header : - LEDs : power, TX, RX, LED (D13) - COM port (communication port) * parallel port(印表機) VS. serial port(滑鼠、數據機) * PC上指RS-232(transmitting bit by bit),通常有四個 * ATmega has USB to serial converter chip (真正電腦的COM port需查此晶片的data) * 傳輸 1 bytes 的資料 = 1 parity bit + 8 data bits + 1 stop bit = 10 bits > [name=Chris Lin]Nano在實體層是TTL位準的5V > 需FTDI或CH340G轉成RS232軟體層的TX/RX訊號 ## 佈線 & 焊接 ![](https://i.imgur.com/1NJxOeV.jpg) ![](https://imgur.com/fV4k6Dt.jpg) ### 小技巧 : 1. 佈線的時候可以在電路板上先做起點、終點的**記號** 2. 焊同一條線的兩端點時先**等一邊稍冷**再做另一邊 3. 焊會碰觸的兩條線同樣需稍做一些等待 4. 從**較矮**之元件**開始焊**(單芯線->排母->排針) 5. 在焊排母的時候需**注意**是否有**歪斜** ## Arduino Software Requirement * Online Tool https://create.arduino.cc/editor (Install Arduino Create plugin) https://create.arduino.cc/getting-started/plugin/welcome * Download IDE https://www.arduino.cc/en/Main/Software ## Basic Introduction > [name=Chris Lin]關於程式的基本介紹 > 接著解構我們的程式,函式之後再講 > Sketch -> verify(編譯產生二進位碼) -> upload(燒錄) 1. 選擇IDE 2. install libraries https://www.arduino.cc/en/Reference/Libraries 4. set board, port 5. include libraries * <servo.h> * attach and write * <SoftwareSerial.h> * Baud : bits per second * set two sides with the same baud (the highest acceptable rate) * use serial monitor to observe * Serial.begin() ## Code Introduction > [name=Chris Lin]課程可先讓學員習寫小程式(和我們程式所需功能相關者),可兩周 - Variable - Function : https://www.arduino.cc/reference/en/ - Sructure : setup, loop - Practice(LED, Servo motor, serial) https://hackmd.io/@ChrisLin/HyblQ1u4U > [name=Chris Lin]上面的筆記本連結是給大家的引導練習題 > 也許還可以讓大家加寫SPI,秀文字在LCD板(手邊有) - Introduction of our program([main](https://drive.google.com/file/d/1PcnwADfp76JOhnKTsO3fZCxIec4gNEq5/view?usp=sharing), [HC05](https://drive.google.com/file/d/1nNnpI53Lfemx_CbBTS6h5XOGzGP5wr5O/view?usp=sharing)) ## 燒錄 * 步驟 : 開發版"Arduino Nano" -> 處理器"ATmega168P(Old Bootloader) -> 序列埠(COM4) * Online : 1. Plugin 2. Serial Monitor 3. 雲端草稿本 * IDE 1. COM Port 驅動程式 2. 燒錄前需按住reset鍵直到出現上傳中字樣立即放開 ## Arduino app * [package](https://drive.google.com/file/d/19dfeVJGi6pBOdaAXsM8kLYPkmIC1orE6/view?usp=sharing) ## HC-05 藍牙模組 * 支援SPP(Serial Port Profile序列埠規範) * 主從(master/slave)一體型 ![](https://i.imgur.com/sFLpgoY.png) *上圖為無底板類型* ![](https://i.imgur.com/H6MBPQf.png) *上圖為第34腳為KEY類型* HC-05預設為slave,但是可藉由AT指令切換為master。 可以和HC-05之間互連也可以跟手機連,這裡使用HC-05當中有EN,藉由實體鍵進入AT模式的版本,並且示範藍芽為從、手機為主的連接模式。 ## 藍芽連接 * HC-05(slave)設定步驟 : 接RXD(Nano的傳送端)、TXD(Nano的接收端)、GND -> 將HC-05程式燒到Nano -> 按住EN實體鍵 -> 將5V接到VCC -> 藍芽板LED慢閃(進入AT Mode) -> 打開序列埠監控視窗 -> 輸入"AT" -> 顯示OK ``` AT command : AT+VERSION AT+UART? (查鮑率) AT+NAME="the name you want to give" AT+PSWD="password you want to give" AT+ROLE? (0 for slave ; 1 for master) AT+ADDR? (查此藍芽模組MAC) ``` * 手機端設定步驟 : 打開藍芽 -> 尋找藍芽Device -> 找到剛剛命名的藍芽或者未更改名稱HC-05 -> 輸入密碼進行配對 -> 如此即完成