這周學習 輸入感測器和輸入裝置。

實踐作業:製作帶有輸入感測器的電路

給微控制器電路增加一個感測器,測量感興趣的值

這周開始,晶片 Attiny45 將會頻繁出場:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

電路設計

嘗試用 phototransistor (光電電晶體)作為輸入模組,去控制 LED 。

在 Eagle 先畫 schematic:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

然後生成 board。可以先用 autorouter 自動佈線,然後再根據需要手動調整。

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

在完成設計之前,需要用 ERC 命令檢查 schematic 的錯誤,用 DRC 檢查 board 的錯誤。根據機器刀頭的尺寸,設定線路間隔為 16mil 。

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

全部仔細檢查後,再分別把 top 圖層和 dimension 圖層輸出為兩個 png 檔案,要注意勾選單色輸出:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Download the eagle sch & brd & png files。

電路板製作

依然使用 Roland SMR-20 刻電路板,用 1/64 刀頭刻線路,1/32 切割外框。在

http://

fabmodules。org

裡面,設定輸入的 png 影象 dpi 為 1500 ,然後計算雕刻路徑:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

將元件們焊好:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

程式

這周開始用 Arduino 寫程式。在上傳之前,需要選好板子的型別以及晶片。 Attiny45 使用 8 MHz 內頻。

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

程式碼如下,也可以到這裡下載。

// set pin numbers:

const int buttonPin = 3; // the number of the pushbutton pin

const int ledPin = 4; // the number of the LED pin

// variables will change:

int buttonState = 0; // variable for reading the pushbutton status

void setup() {

// initialize the LED pin as an output:

pinMode(ledPin, OUTPUT);

// initialize the pushbutton pin as an input:

pinMode(buttonPin, INPUT);

}

void loop() {

// read the state of the pushbutton value:

buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed。

// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

} else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

}

在這個簡單的電路程式中,光電電晶體起到開關的作用,根據光亮控制電路中 LED 的明滅:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

試驗1:用 Python 和串列埠通訊

製作一塊帶有光電電晶體的板子:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

下載 hello。light。45。c 和 makefile。在 terminal cd 進入資料夾,輸入命令

make -f hello。light。45。make

得到迴應:

avr-gcc -mmcu

=

attiny45 -Wall -Os -DF_CPU

=

8000000

-I。/ -o hello。button。45。out hello。button。45。c

avr-objcopy -O ihex hello。button。45。out hello。button。45。c。hex

\

avr-size ——mcu

=

attiny45 ——format

=

avr hello。button。45。out

AVR Memory Usage

————————

Device: attiny45

Program:

364

bytes

8。9% Full

。text + 。data + 。bootloader

Data:

0

bytes

0。0% Full

。data + 。bss + 。noinit

然後輸入命令:

sudo make -f hello。light。45。make program-usbtiny

得到迴應:

avr-objcopy -O ihex hello。light。45。out hello。light。45。c。hex

\

avr-size ——mcu

=

attiny45 ——format

=

avr hello。light。45。out

AVR Memory Usage

————————

Device: attiny45

Program:

502

bytes

12。3% Full

。text + 。data + 。bootloader

Data:

1

bytes

0。4% Full

。data + 。bss + 。noinit

avrdude -p t45 -P usb -c usbtiny -U flash:w:hello。light。45。c。hex

avrdude: AVR device initialized and ready to accept instructions

Reading

|

################################################## | 100% 0。00s

avrdude: Device

signature

=

0x1e9206

avrdude: NOTE:

“flash”

memory has been specified, an erase cycle will be performed

To disable this feature, specify the -D option。

avrdude: erasing chip

avrdude: reading input file

“hello。light。45。c。hex”

avrdude: input file hello。light。45。c。hex auto detected as Intel Hex

avrdude: writing flash

502

bytes

Writing

|

################################################## | 100% 0。74s

avrdude:

502

bytes of flash written

avrdude: verifying flash memory against hello。light。45。c。hex:

avrdude: load data flash data from input file hello。light。45。c。hex:

avrdude: input file hello。light。45。c。hex auto detected as Intel Hex

avrdude: input file hello。light。45。c。hex contains

502

bytes

avrdude: reading on-chip flash data:

Reading

|

################################################## | 100% 0。94s

avrdude: verifying 。。。

avrdude:

502

bytes of flash verified

Python 程式可以在這裡下載。

用 USB to TTL 連線好板子並執行程式:

python hello。light。45。py /dev/ttyUSB0

這時出現錯誤提示:

Traceback (most recent call last):

File “hello。light。45。py”, line 62, in

ser = serial。Serial(port,9600)

File “/Library/Python/2。7/site-packages/serial/serialutil。py”, line 180, in __init__

self。open()

File “/Library/Python/2。7/site-packages/serial/serialposix。py”, line 294, in open

raise SerialException(msg。errno, “could not open port %s: %s” % (self。_port, msg))

serial。serialutil。SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: ‘/dev/ttyUSB0’

Google 一下這個問題 Failed to open port /dev/ttyUSB0 - ROS Answers: Open Source Q&A Forum。問題可能是因為我還沒有裝 FTDI (用於 USB 和串列埠通訊)驅動。

使用命令 ls /dev/tty* 可以列出當前可用的串列埠。確實沒有 /dev/tty。usbserial-A400gwhT 串列埠。所以我嘗試安裝 FTDI 驅動 - D2XX Direct Drivers 以及 Virtual COM Port Drivers。但是依然不成功。

接著,Google 到這一篇 How to Install FTDI Drivers - learn。sparkfun。com 照著再裝了一遍驅動。重啟電腦後,插入 FTDI 2 USB 裝置, /dev/tty。usbserial-A400gwhT 總算出現在串列埠列表中。

接著,嘗試用新的串列埠執行程式:

python hello。light。45。py /dev/tty。usbserial-A400gwhT

9600

繼續看到錯誤提示:

command line: hello。light。45。py serial_port

到 python 程式中仔細看了看,將 len(sys。argv) 從 2 改為 3:

if (len(sys。argv) != 3):

print “command line: hello。light。45。py serial_port”

sys。exit()

port = sys。argv[1]

問題解決:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

測試影片

實驗2:開關

做了一個帶開關的板子:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

下載 hello。button。45。c 和 makefile。 在 terminal cd 進入資料夾,執行命令:

make -f hello。button。45。make

sudo make -f hello。button。45。make program-usbtiny

都正常。然後用 TTL 2 USB 連線板子:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

執行命令

python term。py /dev/tty。usbserial-A400gwhT

9600

每按一次按鈕,螢幕打出一個“du”:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

測試影片

實驗3:溫度感測器

做了一個帶有溫度感測器的板子:

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

下載 hello。temp。45。c 和 makefile。輸入命令

make -f hello。button。45。make

sudo make -f hello。button。45。make program-usbtiny

然後執行程式

python hello。temp。45。py /dev/tty。usbserial-A400gwhT 9600

感測器開始實時監測溫度變化。

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

課堂筆記

1。 通訊模組

pySerial: Python與串列埠通訊

miniterm: pySerial 的操作終端

例項檔案rx。py, term。py

API:serialport(跟 js 互動),chrome。serial,WebUSB API

bit timing 位定時: 關鍵引數 xxx bits/sec

clocks (10% RC, 1% RC calibrated, 。5% resonator, 50 ppm crystal)

2。開關

button 按鈕開關,slide 滑動開關

3。 運動感測器

pyroelectric 熱釋電紅外PIR運動感測器探測器模組。可以測量身體的熱輻射,檢測活動和變化。

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

test motion module video

4。距離感測器

sonar 聲納:Ultrasonic Module HC-SR04 Distance Sensor

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

test video

5。磁場感測器

Hall effect:霍爾效應是指當固體導體放置在一個磁場內,且有電流透過時,導體內的電荷載子受到洛倫茲力而偏向一邊,繼而產生電壓(霍爾電壓)的現像。電壓所引致的電場力會平衡洛倫茲力。

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

test video

6。溫度感測器

NTC 負溫度係數熱敏電阻(Negative Tempperature Coefficient Thermistor) 熱敏電阻: 電阻阻值隨溫度而上升。溫度越高,敏感度越低。

RTD thermistors: lower temperature, more sensitive (NTC table)

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

test video

7。光感測器

phototransistor 光電電晶體

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

test video

8。加速,方向,旋轉感測器

3D accelerometer I2C

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

test video

9。聲音感測器

MEMS, Microphones > Knowles SPU0414HR5H-SB-7

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

test video

10。震動感測器

piezo 壓電

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

11。壓力感測器

force sensing resistor 力敏電阻

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

strain gauge 應變儀

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

load cell 稱重感測器

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

12。 step response 階躍響應

resistance 電阻, capacitance 電容, inductance 電感, position 位置, pressure 壓力, proximity 接近度, tilt 傾斜, acceleration 加速, humidity 溼度

project: touchpad, multitouch, 。。。

loading (test video),transmit-receive (test video)

dielectric spectroscopy measurement 介電譜測量

13。 影象

module

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置

cameras, boards

OpenCV: 視屏處理庫

libuvc: a cross-platform library for USB video devices

WebRTC:WebRTC (Web Real-Time Communications) 使 Web 應用程式和網站來捕獲和可選流媒體音訊/影片,以及瀏覽器之間任意資料交換,而不需要一箇中介。

更多資料

The Physics of Information Technology

Basics of Measuring the Dielectric Properties of Materials

Measure the Difference

課程資源

Fab Academy 2016 課表

Week11 課程影片

Week11 課程大綱

我的英文筆記 Gitbook

公眾號文章列表

Fab Academy 第十一週:輸入裝置

Fab Academy 第十一週:輸入裝置