[FabNotes23] 作業(yè)11:輸入設(shè)備 | Week11 - Input Devices

給微控制器電路增加一個(gè)傳感器户辫,測(cè)量感興趣的值

這周開始渡讼,芯片 Attiny45 將會(huì)頻繁出場(chǎng):

電路設(shè)計(jì)

嘗試用 phototransistor (光電晶體管)作為輸入模塊溜哮,去控制 LED 掸屡。

在 Eagle 先畫 schematic:

然后生成 board擎勘±涫兀可以先用 autorouter 自動(dòng)布線刀崖,然后再根據(jù)需要手動(dòng)調(diào)整。

在完成設(shè)計(jì)之前拍摇,需要用 ERC 命令檢查 schematic 的錯(cuò)誤亮钦,用 DRC 檢查 board 的錯(cuò)誤。根據(jù)機(jī)器刀頭的尺寸充活,設(shè)置線路間隔為 16mil 蜂莉。

全部仔細(xì)檢查后,再分別把 top 圖層和 dimension 圖層輸出為兩個(gè) png 文件混卵,要注意勾選單色輸出:

Download the eagle sch & brd & png files.

電路板制作

依然使用 Roland SMR-20 刻電路板映穗,用 1/64 刀頭刻線路,1/32 切割外框幕随。在 fabmodules.org 里面蚁滋,設(shè)置輸入的 png 圖像 dpi 為 1500 ,然后計(jì)算雕刻路徑:

將元件們焊好:

程序

這周開始用 Arduino 寫程序赘淮。在上傳之前辕录,需要選好板子的類型以及芯片。 Attiny45 使用 8 MHz 內(nèi)頻梢卸。

代碼如下走诞,也可以到這里下載


  // 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);
  }
}

在這個(gè)簡(jiǎn)單的電路程序中低剔,光電晶體管起到開關(guān)的作用速梗,根據(jù)光亮控制電路中 LED 的明滅:

試驗(yàn)1:用 Python 和串口通信

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

下載 hello.light.45.cmakefile肮塞。在 terminal cd 進(jìn)入文件夾,輸入命令 make -f hello.light.45.make

得到回應(yīng):

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

得到回應(yīng):

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 連接好板子并運(yùn)行程序:

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

這時(shí)出現(xiàn)錯(cuò)誤提示:

Traceback (most recent call last):
  File "hello.light.45.py", line 62, in <module>
    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 一下這個(gè)問題 Failed to open port /dev/ttyUSB0 - ROS Answers: Open Source Q&A Forum姻锁。

問題可能是因?yàn)槲疫€沒有裝 FTDI (用于 USB 和串口通信)驅(qū)動(dòng)枕赵。

使用命令 ls /dev/tty* 可以列出當(dāng)前可用的串口。確實(shí)沒有 /dev/tty.usbserial-A400gwhT 串口位隶。所以我嘗試安裝 FTDI 驅(qū)動(dòng) - D2XX Direct Drivers 以及 Virtual COM Port Drivers拷窜。但是依然不成功。

接著涧黄,Google 到這一篇 How to Install FTDI Drivers - learn.sparkfun.com 照著再裝了一遍驅(qū)動(dòng)篮昧。重啟電腦后,插入 FTDI 2 USB 設(shè)備笋妥, /dev/tty.usbserial-A400gwhT 總算出現(xiàn)在串口列表中懊昨。

接著,嘗試用新的串口運(yùn)行程序:

python hello.light.45.py /dev/tty.usbserial-A400gwhT 9600

繼續(xù)看到錯(cuò)誤提示:

command line: hello.light.45.py serial_port

到 python 程序中仔細(xì)看了看春宣,將 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]

問題解決:

測(cè)試視頻

實(shí)驗(yàn)2:開關(guān)

做了一個(gè)帶開關(guān)的板子:

下載 hello.button.45.cmakefile砂吞。 在 terminal cd 進(jìn)入文件夾向挖,運(yùn)行命令: make -f hello.button.45.makesudo make -f hello.button.45.make program-usbtiny名段。都正常红氯。

然后用 TTL 2 USB 連接板子:

運(yùn)行命令

python term.py /dev/tty.usbserial-A400gwhT 9600

每按一次按鈕,屏幕打出一個(gè)“du”:

測(cè)試視頻

實(shí)驗(yàn)3:溫度傳感器

做了一個(gè)帶有溫度傳感器的板子:

下載 hello.temp.45.cmakefile嚷辅。輸入命令 make -f hello.button.45.makesudo make -f hello.button.45.make program-usbtiny簿姨。

然后運(yùn)行程序 python hello.temp.45.py /dev/tty.usbserial-A400gwhT 9600 。傳感器開始實(shí)時(shí)監(jiān)測(cè)溫度變化簸搞。

課程資源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末扁位,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子攘乒,更是在濱河造成了極大的恐慌贤牛,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件则酝,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡闰集,警方通過查閱死者的電腦和手機(jī)沽讹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來武鲁,“玉大人爽雄,你說我怎么就攤上這事°迨螅” “怎么了挚瘟?”我有些...
    開封第一講書人閱讀 164,782評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵叹谁,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我乘盖,道長(zhǎng)焰檩,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評(píng)論 1 294
  • 正文 為了忘掉前任订框,我火速辦了婚禮析苫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘穿扳。我一直安慰自己衩侥,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,733評(píng)論 6 392
  • 文/花漫 我一把揭開白布矛物。 她就那樣靜靜地躺著茫死,像睡著了一般。 火紅的嫁衣襯著肌膚如雪履羞。 梳的紋絲不亂的頭發(fā)上璧榄,一...
    開封第一講書人閱讀 51,578評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音吧雹,去河邊找鬼骨杂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛雄卷,可吹牛的內(nèi)容都是我干的搓蚪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼丁鹉,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼妒潭!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起揣钦,我...
    開封第一講書人閱讀 39,241評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤雳灾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后冯凹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體谎亩,經(jīng)...
    沈念sama閱讀 45,686評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,878評(píng)論 3 336
  • 正文 我和宋清朗相戀三年宇姚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了匈庭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,992評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡浑劳,死狀恐怖阱持,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情魔熏,我是刑警寧澤衷咽,帶...
    沈念sama閱讀 35,715評(píng)論 5 346
  • 正文 年R本政府宣布鸽扁,位于F島的核電站,受9級(jí)特大地震影響镶骗,放射性物質(zhì)發(fā)生泄漏桶现。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,336評(píng)論 3 330
  • 文/蒙蒙 一卖词、第九天 我趴在偏房一處隱蔽的房頂上張望巩那。 院中可真熱鬧,春花似錦此蜈、人聲如沸即横。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽东囚。三九已至,卻和暖如春战授,著一層夾襖步出監(jiān)牢的瞬間页藻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評(píng)論 1 270
  • 我被黑心中介騙來泰國打工植兰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留份帐,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,173評(píng)論 3 370
  • 正文 我出身青樓楣导,卻偏偏與公主長(zhǎng)得像废境,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子筒繁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,947評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容