菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP 如何 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


使用数组和输出创建流动LED

在本章中,我们将使用多个GPIO引脚,通过按顺序打开和关闭它们来创建“流动”效果。


What do we need?

For this you need

注意:您需要的电阻可能与我们使用的不同,具体取决于您使用的LED类型。大多数小型LED只需要一个小的电阻,大约200-500欧姆。通常,您使用的确切值并不重要,但电阻值越小,LED就会越亮。

Click the links in the list above for descriptions of the different components.


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

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.

Raspberry Pi 3 with Breadboard. Flowing LEDs circuit

Look at the above illustration of the circuit.

  1. 在树莓派上,将跳线母头连接到GND引脚。在我们的示例中,我们使用了物理引脚6(GND,第3行,右列)
  2. 在面包板上,将连接到GND电源的跳线公头连接到右侧的接地总线。面包板的整个列都连接在一起,所以连接到哪一行并不重要。在我们的示例中,我们将其连接到第1行
  3. 对于每个LED:连接LED,使其连接到2个连接点行。在我们的示例中,我们连接了
    1. LED1连接到第5行(阴极)和第6行(阳极)J列
    2. LED2连接到第8行(阴极)和第9行(阳极)J列
    3. LED3连接到第11行(阴极)和第12行(阳极)J列
    4. LED4连接到第14行(阴极)和第15行(阳极)J列
    5. LED5连接到第17行(阴极)和第18行(阳极)J列
    6. LED6连接到第20行(阴极)和第21行(阳极)J列
    7. LED7连接到第23行(阴极)和第24行(阳极)J列
    8. LED8连接到第26行(阴极)和第27行(阳极)J列
  4. 对于每个LED:将220欧姆电阻的一条腿连接到右侧的接地总线列,另一条腿连接到与LED阴极腿相连的右侧连接点行。在我们的示例中,我们连接了
    1. LED1连接到第5行I列
    2. LED2连接到第8行I列
    3. LED3连接到第11行I列
    4. LED4连接到第14行I列
    5. LED5连接到第17行I列
    6. LED6连接到第20行I列
    7. LED7连接到第23行I列
    8. LED8连接到第26行I列
  5. 对于每个LED:将跳线母头连接到树莓派上的GPIO引脚,并将跳线公头连接到与LED阳极腿相连的右侧连接点行。在我们的示例中,我们连接了
    1. LED1从物理引脚7(GPIO 4,第4行,左列)连接到连接点第6行F列
    2. LED2从物理引脚11(GPIO 17,第6行,左列)连接到连接点第9行F列
    3. LED3从物理引脚13(GPIO 27,第7行,左列)连接到连接点第12行F列
    4. LED4从物理引脚15(GPIO 22,第8行,左列)连接到连接点第15行F列
    5. LED5从物理引脚12(GPIO 18,第6行,右列)连接到连接点第18行F列
    6. LED6从物理引脚16(GPIO 23,第8行,右列)连接到连接点第21行F列
    7. LED7从物理引脚18(GPIO 24,第9行,右列)连接到连接点第24行F列
    8. LED8从物理引脚22(GPIO 25,第11行,右列)连接到连接点第27行F列

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脚本

进入“nodetest”目录,创建一个名为“flowingleds.js”的新文件

pi@w3demopi:~ $ nano flowingleds.js

The file is now open and can be edited with the built in Nano Editor.

Write, or paste the following

flowingleds.js

var Gpio = require('onoff').Gpio; //包含onoff以与GPIO交互
var LED04 = new Gpio(4, 'out'), //为所有GPIO输出引脚声明变量
  LED17 = new Gpio(17, 'out'),
  LED27 = new Gpio(27, 'out'),
  LED22 = new Gpio(22, 'out'),
  LED18 = new Gpio(18, 'out'),
  LED23 = new Gpio(23, 'out'),
  LED24 = new Gpio(24, 'out'),
  LED25 = new Gpio(25, 'out');

//将所有LED变量放入一个数组中
var leds = [LED04, LED17, LED27, LED22, LED18, LED23, LED24, LED25];
var indexCount = 0; //一个计数器
dir = "up"; //流动方向变量

var flowInterval = setInterval(flowingLeds, 100); //每100毫秒运行flowingLeds函数

function flowingLeds() { //流动LED函数
  leds.forEach(function(currentValue) { //对于数组中的每个项目
    currentValue.writeSync(0); //关闭LED
  });
  if (indexCount == 0) dir = "up"; //如果计数达到零,则将流动方向设置为“up”
  if (indexCount >= leds.length) dir = "down"; //如果计数达到7,则将流动方向设置为“down”
  if (dir == "down") indexCount--; //如果方向向下,则向下计数
  leds[indexCount].writeSync(1); //打开数组索引与计数匹配的LED
  if (dir == "up") indexCount++ //如果方向向上,则向上计数
};

function unexportOnClose() { //退出程序时运行的函数
  clearInterval(flowInterval); //停止流动间隔
  leds.forEach(function(currentValue) { //对于每个LED
    currentValue.writeSync(0); //关闭LED
    currentValue.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 flowingleds.js

现在LED应该按顺序打开和关闭,形成流动效果。

End the program with Ctrl+c.


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

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