Node.js Raspberry Pi GPIO - 흐르는 LED


출력이 있는 어레이를 사용하여 흐르는 LED 생성

이 장에서는 여러 GPIO 핀을 사용하여 순서대로 켜고 끄는 방식으로 "흐르는" 효과를 생성합니다.


우리는 무엇이 필요한가?

이를 위해서는 다음이 필요합니다.

참고: 필요한 저항기는 사용하는 LED 유형에 따라 사용하는 것과 다를 수 있습니다. 대부분의 소형 LED에는 약 200-500옴의 작은 저항만 있으면 됩니다. 일반적으로 어떤 정확한 값을 사용하는지는 중요하지 않지만 저항 값이 작을수록 LED가 더 밝게 빛납니다.

다양한 구성 요소에 대한 설명을 보려면 위 목록의 링크를 클릭하십시오.


회로 구축

이제 브레드보드에 회로를 만들 차례입니다.

전자 제품을 처음 접하는 경우 Raspberry Pi의 전원을 끄는 것이 좋습니다. 정전기 방지 매트나 접지 스트랩을 사용하여 손상되지 않도록 하십시오.

다음 명령을 사용하여 Raspberry Pi를 올바르게 종료합니다.

pi@w3demopi:~ $ sudo shutdown -h now

Raspberry Pi에서 LED가 깜박임을 멈추면 Raspberry Pi에서 전원 플러그를 뽑습니다(또는 연결된 전원 스트립을 켭니다).

제대로 종료하지 않고 플러그를 뽑기만 하면 메모리 카드가 손상될 수 있습니다.

브레드보드가 있는 라즈베리 파이 3.  흐르는 LED 회로

위의 회로도를 보십시오.

  1. Raspberry Pi에서 점퍼 와이어의 암 다리를 GND 핀에 연결합니다. 이 예에서는 물리적 핀 6( GND , 행 3, 오른쪽 열)을 사용했습니다.
  2. Breadboard에서 GND 전원에 연결된 점퍼선의 수컷 다리를 오른쪽 의 Ground Bus 에 연결합니다. 브레드보드의 해당 열 전체가 연결되어 있으므로 어느 행이든 상관 없습니다. 이 예에서는 행 1에 연결했습니다.
  3. 각 LED에 대해: 2개의 Tie-Point 행에 연결되도록 LED를 연결합니다. 이 예에서는 다음을 연결했습니다.
    1. LED1에서 행 5(음극) 및 6(음극) J 열
    2. LED2에서 행 8(음극) 및 9(음극) J 열
    3. LED3 to rows 11 (cathode) & 12 (anode) column J
    4. LED4 to rows 14 (cathode) & 15 (anode) column J
    5. LED5 to rows 17 (cathode) & 18 (anode) column J
    6. LED6 to rows 20 (cathode) & 21 (anode) column J
    7. LED7 to rows 23 (cathode) & 24 (anode) column J
    8. LED8 to rows 26 (cathode) & 27 (anode) column J
  4. For each LED: Connect one of the legs of a 220 ohm resistor from the the Ground Bus column on the right side, and the other leg to the right side Tie-Point row where it connects to the cathode leg of the LED. In our example we connected:
    1. LED1 to row 5 column I
    2. LED2 to row 8 column I
    3. LED3 to row 11 column I
    4. LED4 to row 14 column I
    5. LED5 to row 17 column I
    6. LED6 to row 20 column I
    7. LED7 to row 23 column I
    8. LED8 to row 26 column I
  5. For each LED: Connect the female leg of a jumper wire to a GPIO pin on the Raspberry Pi, and the male leg of the jumper wire to the right side Tie-Point row where it connects to the anode leg of the LED. In our example we connected:
    1. LED1 from Physical Pin 7 (GPIO 4, row 4, left column) to Tie-point row 6 column F
    2. LED2 from Physical Pin 11 (GPIO 17, row 6, left column) to Tie-point row 9 column F
    3. LED3 from Physical Pin 13 (GPIO 27, row 7, left column) to Tie-point row 12 column F
    4. LED4 from Physical Pin 15 (GPIO 22, row 8, left column) to Tie-point row 15 column F
    5. LED5 from Physical Pin 12 (GPIO 18, row 6, right column) to Tie-point row 18 column F
    6. LED6 from Physical Pin 16 (GPIO 23, row 8, right column) to Tie-point row 21 column F
    7. LED7 from Physical Pin 18 (GPIO 24, row 9, right column) to Tie-point row 24 column F
    8. LED8 from Physical Pin 22 (GPIO 25, row 11, right column) to Tie-point row 27 column 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.



Raspberry Pi and Node.js Flowing LEDs Script

Go to the "nodetest" directory, and create a new file called "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; //include onoff to interact with the GPIO
var LED04 = new Gpio(4, 'out'), //use declare variables for all the GPIO output pins
  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');

//Put all the LED variables in an array
var leds = [LED04, LED17, LED27, LED22, LED18, LED23, LED24, LED25];
var indexCount = 0; //a counter
dir = "up"; //variable for flowing direction

var flowInterval = setInterval(flowingLeds, 100); //run the flowingLeds function every 100ms

function flowingLeds() { //function for flowing Leds
  leds.forEach(function(currentValue) { //for each item in array
    currentValue.writeSync(0); //turn off LED
  });
  if (indexCount == 0) dir = "up"; //set flow direction to "up" if the count reaches zero
  if (indexCount >= leds.length) dir = "down"; //set flow direction to "down" if the count reaches 7
  if (dir == "down") indexCount--; //count downwards if direction is down
  leds[indexCount].writeSync(1); //turn on LED that where array index matches count
  if (dir == "up") indexCount++ //count upwards if direction is up
};

function unexportOnClose() { //function to run when exiting program
  clearInterval(flowInterval); //stop flow interwal
  leds.forEach(function(currentValue) { //for each LED
    currentValue.writeSync(0); //turn off LED
    currentValue.unexport(); //unexport GPIO
  });
};

process.on('SIGINT', unexportOnClose); //function to run when user closes using ctrl+cc

Press "Ctrl+x" to save the code. Confirm with "y", and confirm the name with "Enter".

Run the code:

pi@w3demopi:~ $ node flowingleds.js

Now the LEDs should turn on and off in sequence, creating a flowing effect.

End the program with Ctrl+c.