Node.js Raspberry Pi GPIO - LED and Pushbutton
同時使用輸入和輸出
在上一章中,我們學習瞭如何使用 Raspberry Pi 及其 GPIO 來讓 LED 閃爍。
為此,我們使用了一個 GPIO 引腳作為“輸出”。
在本章中,我們將使用另一個 GPIO 引腳作為“輸入”。
我們希望 LED 在按下連線到麵包板的按鈕時點亮,而不是閃爍 5 秒。
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 1k 歐姆電阻
- 1 x 直插 LED
- 1 x 按鈕
- 4 x Female to male jumper wires
- 1 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
現在是時候在我們的麵包板上構建電路了。我們將以上一章建立的電路作為起點。
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
After the LEDs stop blinking on the Raspberry Pi, then pull out the power plug from the Raspberry Pi (or turn of the power strip it is connected to).
Just pulling the plug without shutting down properly may cause corruption of the memory card.

Look at the above illustration of the circuit.
- 從上一章建立的電路開始
在 Raspberry Pi 上,將跳線的母端連線到一個 5V 電源引腳。在我們的示例中,我們使用了物理引腳 2(5V,第 1 行,右側列) - 在麵包板上,將連線到 5V 電源的跳線的公端連線到右側的 電源軌。麵包板的整列是連線的,所以行號無關緊要。在我們的示例中,我們將其連線到第 1 行。
- 在麵包板上,放置按鈕,使其跨越溝槽。在我們的示例中,它連線到第 13 行和第 15 行,E 列和 F 列。
- 在麵包板上,將 1k 歐姆電阻的一條腿連線到右側的 接地軌列,另一條腿連線到與按鈕右側其中一個腿相連的右側接線柱行。在我們的示例中,我們將一側連線到第 13 行,J 列,另一側連線到最近的 接地軌孔。
- 在麵包板上,將一根公對公跳線從右側 電源軌 連線到與按鈕另一條腿相連的右側接線柱行。在我們的示例中,我們將一側連線到第 15 行,J 列,另一側連線到最近的 電源軌孔。
- 在 Raspberry Pi 上,將跳線的母端連線到一個 GPIO 引腳。在我們的示例中,我們使用了物理引腳 11(GPIO 17,第 6 行,左側列)。
- 在麵包板上,將跳線的公端連線到按鈕腿上直接跨越 地線 連線腿的左側接線柱行。在我們的示例中,我們將其連線到第 13 行,A 列。
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.
Raspberry Pi 和 Node.js LED 和按鈕指令碼
轉到“nodetest”目錄,並建立一個名為“buttonled.js
”的新檔案。
pi@w3demopi:~ $ nano buttonled.js
The file is now open and can be edited with the built in Nano Editor.
Write, or paste the following
buttonled.js
var Gpio = require('onoff').Gpio; //包含onoff以與GPIO互動
var LED = new Gpio(4, 'out'); //使用 GPIO 引腳 4 作為輸出
var pushButton = new Gpio(17, 'in', 'both'); //使用 GPIO 引腳 17 作為輸入,並處理“both”(按下和釋放)按鈕事件
pushButton.watch(function (err, value) { //監視 pushButton GPIO 上的硬體中斷,指定回撥函式
if (err) { //如果有錯誤
console.error('發生錯誤', err); //將錯誤訊息輸出到控制檯
return;
}
LED.writeSync(value); //根據按鈕狀態(0 或 1)開啟或關閉 LED
});
function unexportOnClose() { //程式退出時執行的函式
LED.writeSync(0); //關閉LED
LED.unexport(); //取消匯出 LED GPIO 以釋放資源
pushButton.unexport(); //取消匯出按鈕 GPIO 以釋放資源
};
process.on('SIGINT', unexportOnClose); //使用者透過 Ctrl+c 關閉程式時執行的函式
Press "Ctrl+x
" to save the code. Confirm with "y
", and confirm the name with "Enter
".
Run the code
pi@w3demopi:~ $ node buttonled.js
現在,按下按鈕時 LED 應該會亮起,釋放按鈕時會熄滅。
End the program with Ctrl+c
.