菜单
×
   ❮     
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 提供支持