Node.js Raspberry Pi GPIO - 깜박이는 LED


출력에 GPIO 사용

이 장에서는 Raspberry Pi와 GPIO를 사용하여 LED를 깜박이게 할 것입니다.

우리는 GPIO를 제어하기 위해 onoff 모듈과 함께 Node.js를 사용합니다.

LED 조명을 켜려면 GPIO 핀을 "출력"으로 사용하고 켜고 끄는(깜박임) 스크립트를 만듭니다.


우리는 무엇이 필요한가?

이 장에서는 LED 조명을 제어하는 ​​간단한 예를 만들 것입니다.

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

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

참고: 필요한 저항기는 사용하는 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에서 첫 번째 점퍼 와이어의 암 레그를 Ground 에 연결합니다 . 모든 GND 핀을 사용할 수 있습니다. 이 예에서는 물리적 핀 9( GND , 행 5, 왼쪽 열)를 사용했습니다.
  2. 브레드보드에서 첫 번째 점퍼 와이어의 수 레그를 오른쪽 의 접지 버스 열에 연결합니다. 브레드보드의 해당 열 전체가 연결되어 있으므로 어느 행이든 상관 없습니다. 이 예에서는 행 1에 연결했습니다.
  3. On the Raspberry Pi, connect the female leg of the second jumper cable to a GPIO pin. In this example we used Physical Pin 7 (GPIO 4, row 4, left column)
  4. On the Breadboard, connect the male leg of the second jumper wire to the Tie-Point row of your choice. In this example we connected it to row 5, column A
  5. On the Breadboard, connect one leg of the resistor to the Ground Bus column on the right side. That entire column of your breadboard is connected, so it doesn't matter which row. In this example we have attached it to row 5
  6. On the Breadboard, connect the other leg of the resistor to the right side Tie-Point row of your choice. In this example we have used row 5, column J
  7. On the Breadboard, connect the cathode leg (the shortest leg) of the LED to the same Tie-Point row that you connected the resistor from GND to. In this example we used row 5, column F
  8. On the Breadboard, connect the anode leg (the longest leg) of the LED to the same Tie-Point row that you connected the jumper from the GPIO pin to. In this example we used row 5, column E

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 Blinking LED Script

Now that we have everything set up, we can write a script to turn the LED on and off.

Start by making a directory where we can keep our Node.js scripts:

pi@w3demopi:~ $ mkdir nodetest

Go to our new directory:

pi@w3demopi:~ $ cd nodetest

Now we will create a new file called "blink.js" using the Nano Editor:

pi@w3demopi:~ $ nano blink.js

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

Write, or paste the following code:

blink.js

var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var LED = new Gpio(4, 'out'); //use GPIO pin 4, and specify that it is output
var blinkInterval = setInterval(blinkLED, 250); //run the blinkLED function every 250ms

function blinkLED() { //function to start blinking
  if (LED.readSync() === 0) { //check the pin state, if the state is 0 (or off)
    LED.writeSync(1); //set pin state to 1 (turn LED on)
  } else {
    LED.writeSync(0); //set pin state to 0 (turn LED off)
  }
}

function endBlink() { //function to stop blinking
  clearInterval(blinkInterval); // Stop blink intervals
  LED.writeSync(0); // Turn LED off
  LED.unexport(); // Unexport GPIO to free resources
}

setTimeout(endBlink, 5000); //stop blinking after 5 seconds

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

Run the code:

pi@w3demopi:~ $ node blink.js

Now the LED should blink for 5 seconds (10 times) before turning off again!