Node.js Raspberry Pi GPIO - ไฟ LED ไหล
การใช้อาร์เรย์ที่มีเอาต์พุตเพื่อสร้างไฟ LED แบบไหล
ในบทนี้เราจะใช้พิน GPIO หลายอันเพื่อสร้างเอฟเฟกต์ "ไหล" โดยเปิดและปิดตามลำดับ
เราต้องการอะไร?
สำหรับสิ่งนี้คุณต้อง:
- Raspberry Pi พร้อม Raspian, อินเทอร์เน็ต, SSH พร้อม Node.js ติดตั้งอยู่
- โมดูล onoffสำหรับNode.js
- 1 x เขียงหั่นขนม
- ตัวต้านทาน 8 x 220 โอห์ม
- 8 x LED ทะลุผ่านรู
- สายจัมเปอร์หญิงถึงชาย 9 เส้น
หมายเหตุ:ตัวต้านทานที่คุณต้องการอาจแตกต่างจากที่เราใช้ ทั้งนี้ขึ้นอยู่กับประเภทของ LED ที่คุณใช้ LED ขนาดเล็กส่วนใหญ่ต้องการตัวต้านทานขนาดเล็กเท่านั้น ประมาณ 200-500 โอห์ม โดยทั่วไปไม่สำคัญว่าค่าที่แน่นอนที่คุณใช้ แต่ยิ่งค่าของตัวต้านทานน้อยกว่า ไฟ LED ก็จะยิ่งสว่างขึ้น
คลิกลิงก์ในรายการด้านบนเพื่อดูคำอธิบายส่วนประกอบต่างๆ
สร้างวงจร
ถึงเวลาสร้างวงจรบน Breadboard ของเราแล้ว
หากคุณยังใหม่ต่ออุปกรณ์อิเล็กทรอนิกส์ เราขอแนะนำให้คุณปิดสวิตช์ Raspberry Pi และใช้แผ่นรองป้องกันไฟฟ้าสถิตย์หรือสายดินเพื่อหลีกเลี่ยงไม่ให้เกิดความเสียหาย
ปิด Raspberry Pi อย่างถูกต้องด้วยคำสั่ง:
pi@w3demopi:~ $ sudo shutdown -h now
หลังจากที่ไฟ LED หยุดกะพริบบน Raspberry Pi แล้ว ให้ดึงปลั๊กไฟออกจาก Raspberry Pi (หรือหมุนรางปลั๊กไฟที่เชื่อมต่ออยู่)
การดึงปลั๊กโดยไม่ปิดอย่างถูกต้องอาจทำให้การ์ดหน่วยความจำเสียหายได้
ดูภาพประกอบด้านบนของวงจร
- บน Raspberry Pi ให้เชื่อมต่อขาตัวเมียของสายจัมเปอร์เข้ากับพินGND ในตัวอย่างของเรา เราใช้ Physical Pin 6 ( GND , แถว 3 , คอลัมน์ขวา )
- บน Breadboard ให้ต่อขาตัวผู้ของสายจัมเปอร์ที่เชื่อมต่อกับ ไฟ GNDเข้ากับGround Busทางด้านขวา คอลัมน์ทั้งหมดบนเขียงหั่นขนมของคุณเชื่อมต่อกัน ดังนั้นจึงไม่สำคัญว่าแถวไหน ในตัวอย่างของเรา เราแนบมันกับแถว 1
- สำหรับ LED แต่ละตัว: ต่อ LED เพื่อเชื่อมต่อกับแถว Tie-Point 2 แถว ในตัวอย่างของเราเราเชื่อมต่อ:
- LED1 ถึงแถว 5 (แคโทด) & 6 (แอโนด) คอลัมน์J
- LED2 ถึงแถวที่ 8 (แคโทด) & 9 (แอโนด) คอลัมน์J
- LED3 to rows 11 (cathode) & 12 (anode) column J
- LED4 to rows 14 (cathode) & 15 (anode) column J
- LED5 to rows 17 (cathode) & 18 (anode) column J
- LED6 to rows 20 (cathode) & 21 (anode) column J
- LED7 to rows 23 (cathode) & 24 (anode) column J
- LED8 to rows 26 (cathode) & 27 (anode) column J
- 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:
- LED1 to row 5 column I
- LED2 to row 8 column I
- LED3 to row 11 column I
- LED4 to row 14 column I
- LED5 to row 17 column I
- LED6 to row 20 column I
- LED7 to row 23 column I
- LED8 to row 26 column I
- 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:
- LED1 from Physical Pin 7 (GPIO 4, row 4, left column) to Tie-point row 6 column F
- LED2 from Physical Pin 11 (GPIO 17, row 6, left column) to Tie-point row 9 column F
- LED3 from Physical Pin 13 (GPIO 27, row 7, left column) to Tie-point row 12 column F
- LED4 from Physical Pin 15 (GPIO 22, row 8, left column) to Tie-point row 15 column F
- LED5 from Physical Pin 12 (GPIO 18, row 6, right column) to Tie-point row 18 column F
- LED6 from Physical Pin 16 (GPIO 23, row 8, right column) to Tie-point row 21 column F
- LED7 from Physical Pin 18 (GPIO 24, row 9, right column) to Tie-point row 24 column F
- 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
.