選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Node.js 樹莓派 GPIO - 閃爍 LED


使用 GPIO 輸出

在本章中,我們將使用樹莓派及其 GPIO 來讓 LED 閃爍。

我們使用 Node.js 和 onoff 模組來控制 GPIO。

要使 LED 燈亮起,我們將 GPIO 引腳用作“輸出”,然後建立一個指令碼來開啟和關閉它(閃爍)。


What do we need?

在本章中,我們將建立一個簡單的示例來控制 LED 燈。

For this you need

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.

Raspberry Pi 3 with Breadboard. Simple LED circuit

Look at the above illustration of the circuit.

  1. 在樹莓派上,將第一根跳線的母頭連線到 Ground。您可以使用任何 GND 引腳。在本例中,我們使用了物理引腳 9(GND,第 5 行,左側列)。
  2. 在麵包板上,將第一根跳線的公頭連線到右側的 Ground Bus 列。您的麵包板的整列都已連線,因此使用哪一行都可以。在本例中,我們將其連線到了第 1 行。
  3. 在樹莓派上,將第二根跳線的母頭連線到 GPIO 引腳。在本例中,我們使用了物理引腳 7(GPIO 4,第 4 行,左側列)。
  4. 在麵包板上,將第二根跳線的公頭連線到您選擇的 Tie-Point 行。在本例中,我們將其連線到了 A 列第 5 行。
  5. 在麵包板上,將電阻的一條腿連線到右側的 Ground Bus 列。您的麵包板的整列都已連線,因此使用哪一行都可以。在本例中,我們將其連線到了第 5 行。
  6. 在麵包板上,將電阻的另一條腿連線到您選擇的右側 Tie-Point 行。在本例中,我們使用了 J 列第 5 行。
  7. 在麵包板上,將 LED 的陰極(較短的引腳)連線到您從 GND 連線到電阻的同一 Tie-Point 行。在本例中,我們使用了 F 列第 5 行。
  8. 在麵包板上,將 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 次),然後再次關閉!


×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援