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 次),然后再次关闭!