Node.js 樹莓派 GPIO - 閃爍 LED
使用 GPIO 輸出
在本章中,我們將使用樹莓派及其 GPIO 來讓 LED 閃爍。
我們使用 Node.js 和 onoff 模組來控制 GPIO。
要使 LED 燈亮起,我們將 GPIO 引腳用作“輸出”,然後建立一個指令碼來開啟和關閉它(閃爍)。
What do we need?
在本章中,我們將建立一個簡單的示例來控制 LED 燈。
For this you need
- A Raspberry Pi with Raspian, internet, SSH, with Node.js installed
- Node.js 的 onoff 模組
- 1 x Breadboard
- 1 x 68 歐姆電阻
- 1 x 直插 LED
- 2 x 母對公跳線
Click the links in the list above for descriptions of the different components.
Note: The resistor you need can be different from what we use depending on the type of LED you use. Most small LEDs only need a small resistor, around 200-500 ohms. It is generally not critical what exact value you use, but the smaller the value of the resistor, the brighter the LED will shine.
Building the Circuit
Now it is time to build the circuit on our Breadboard.
If you are new to electronics, we recommend you turn off the power for the Raspberry Pi. And use an anti-static mat or a grounding strap to avoid damaging it.
Shut down the Raspberry Pi properly with the command
pi@w3demopi:~ $ sudo shutdown -h now
在樹莓派上的 LED 停止閃爍後,拔掉樹莓派的電源插頭(或關閉連線到它的電源排插)。
Just pulling the plug without shutting down properly may cause corruption of the memory card.

Look at the above illustration of the circuit.
- 在樹莓派上,將第一根跳線的母頭連線到 Ground。您可以使用任何 GND 引腳。在本例中,我們使用了物理引腳 9(GND,第 5 行,左側列)。
- 在麵包板上,將第一根跳線的公頭連線到右側的 Ground Bus 列。您的麵包板的整列都已連線,因此使用哪一行都可以。在本例中,我們將其連線到了第 1 行。
- 在樹莓派上,將第二根跳線的母頭連線到 GPIO 引腳。在本例中,我們使用了物理引腳 7(GPIO 4,第 4 行,左側列)。
- 在麵包板上,將第二根跳線的公頭連線到您選擇的 Tie-Point 行。在本例中,我們將其連線到了 A 列第 5 行。
- 在麵包板上,將電阻的一條腿連線到右側的 Ground Bus 列。您的麵包板的整列都已連線,因此使用哪一行都可以。在本例中,我們將其連線到了第 5 行。
- 在麵包板上,將電阻的另一條腿連線到您選擇的右側 Tie-Point 行。在本例中,我們使用了 J 列第 5 行。
- 在麵包板上,將 LED 的陰極(較短的引腳)連線到您從 GND 連線到電阻的同一 Tie-Point 行。在本例中,我們使用了 F 列第 5 行。
- 在麵包板上,將 LED 的陽極(較長的引腳)連線到您從 GPIO 引腳連線到跳線的同一 Tie-Point 行。在本例中,我們使用了 E 列第 5 行。
Your circuit should now be complete, and your connections should look pretty similar to the illustration above.
Now it is time to boot up the Raspberry Pi, and write the Node.js script to interact with it.
樹莓派和 Node.js 閃爍 LED 指令碼
現在我們已經設定好了一切,我們可以編寫一個指令碼來開啟和關閉 LED。
首先建立一個目錄,我們可以在其中存放 Node.js 指令碼。
pi@w3demopi:~ $ mkdir nodetest
進入我們新建立的目錄。
pi@w3demopi:~ $ cd nodetest
現在我們將使用 Nano 編輯器建立一個名為“blink.js
”的新檔案。
pi@w3demopi:~ $ nano blink.js
The file is now open and can be edited with the built in Nano Editor.
編寫或貼上以下程式碼:
blink.js
var Gpio = require('onoff').Gpio; //包含onoff以與GPIO互動
var LED = new Gpio(4, 'out'); //使用 GPIO 引腳 4,並指定其為輸出
var blinkInterval = setInterval(blinkLED, 250); //每 250ms 執行一次 blinkLED 函式
function blinkLED() { //開始閃爍的函式
if (LED.readSync() === 0) { //檢查引腳狀態,如果狀態為 0(或關閉)
LED.writeSync(1); //將引腳狀態設定為 1(開啟 LED)
} else {
LED.writeSync(0); //將引腳狀態設定為 0(關閉 LED)
}
}
function endBlink() { //停止閃爍的函式
clearInterval(blinkInterval); //停止閃爍間隔
LED.writeSync(0); //關閉LED
LED.unexport(); //取消匯出 GPIO 以釋放資源
}
setTimeout(endBlink, 5000); // 5 秒後停止閃爍
Press "Ctrl+x
" to save the code. Confirm with "y
", and confirm the name with "Enter
".
Run the code
pi@w3demopi:~ $ node blink.js
現在 LED 應該會閃爍 5 秒(10 次),然後再次關閉!