Node.js Raspberry Pi GPIO - LED i przycisk
Korzystanie zarówno z wejścia, jak i wyjścia
W poprzednim rozdziale dowiedzieliśmy się, jak używać Raspberry Pi i jego GPIO do migania diody LED.
Do tego wykorzystaliśmy pin GPIO jako „Wyjście”.
W tym rozdziale użyjemy innego pinu GPIO jako „Wejście”.
Zamiast migać przez 5 sekund, chcemy, aby dioda zapalała się po naciśnięciu przycisku podłączonego do płytki stykowej.
Czego potrzebujemy?
W tym rozdziale stworzymy prosty przykład, w którym sterujemy oświetleniem LED za pomocą przycisku.
Do tego potrzebujesz:
- Raspberry Pi z Raspianem, internetem, SSH, z zainstalowanym Node.js
- Moduł onoff dla Node.js
- 1 x deska do krojenia chleba
- Rezystor 1x68 Ohm
- Rezystor 1x1 k Ohm
- 1 x dioda LED z otworem przelotowym;
- 1 x przycisk
- 4 x przewody połączeniowe żeńskie na męskie
- 1 x przewody połączeniowe z męskiego na męskie
Kliknij łącza na powyższej liście, aby uzyskać opisy różnych komponentów.
Uwaga: Rezystor, którego potrzebujesz, może różnić się od tego, którego używamy, w zależności od typu używanej diody LED. Większość małych diod LED potrzebuje tylko małego rezystora, około 200-500 omów. Zasadniczo nie jest decydujące, jakiej dokładnej wartości użyjesz, ale im mniejsza wartość rezystora, tym jaśniej dioda LED będzie świecić.
W tym rozdziale będziemy budować na obwodzie, który zbudowaliśmy w poprzednim rozdziale, więc rozpoznasz niektóre części z powyższej listy.
Budowanie obwodu
Teraz nadszedł czas, aby zbudować obwód na naszej Breadboard. Jako punkt wyjścia użyjemy obwodu, który stworzyliśmy w poprzednim rozdziale .
Jeśli jesteś nowicjuszem w elektronice, zalecamy wyłączenie zasilania Raspberry Pi. I użyj maty antystatycznej lub paska uziemiającego, aby uniknąć uszkodzenia.
Zamknij poprawnie Raspberry Pi za pomocą polecenia:
pi@w3demopi:~ $ sudo shutdown -h now
Gdy diody przestaną migać na Raspberry Pi, wyciągnij wtyczkę zasilania z Raspberry Pi (lub wyłącz listwę zasilającą, do której jest podłączona).
Samo wyciągnięcie wtyczki bez prawidłowego wyłączenia może spowodować uszkodzenie karty pamięci.
Spójrz na powyższą ilustrację obwodu.
- Starting with the circuit we created in the last chapter:
On the Raspberry Pi, connect the female leg of a jumper wire to a 5V power pin. In our example we used Physical Pin 2 (5V, row 1, right column) - On the Breadboard, connect the male leg of the jumper wire connected to the 5V power, to the Power Bus on the right side. That entire column of your breadboard is connected, so it doesn't matter which row. In our example we attached it to row 1
- On the Breadboard, connect the push button so that it fits across the Trench. In our example it connects to rows 13 and 15, columns E and F
- On the Breadboard, connect one leg of the 1k ohm resistor to the Ground Bus column on the right side, and the other leg to the right side Tie-Point row where it connects to one of the right side legs of the push button. In our example we attached one side to Tie-Point row 13, column J, and the other side to the closest Ground Bus hole
- On the Breadboard, connect a male-to-male jumper wire from the right Power Bus, to the right Tie-Point row that connects to the other leg of the push button. In our example we attached one side to Tie-Point row 15, column J, and the other side to the closest Power Bus hole
- On the Raspberry Pi, connect the female leg of a jumper wire to a GPIO pin. In our example we used Physical Pin 11 (GPIO 17, row 6, left column)
- On the Breadboard, connect the male leg of the jumper wire to left Tie-Point row the Push Button leg that is directly across the Ground connection leg. In our example we attached it to row 13, column A
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 LED and Button Script
Go to the "nodetest" directory, and create a new file called "buttonled.js
":
pi@w3demopi:~ $ nano buttonled.js
The file is now open and can be edited with the built in Nano Editor.
Write, or paste the following:
buttonled.js
var
Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var
LED = new Gpio(4, 'out'); //use GPIO pin 4 as output
var pushButton = new
Gpio(17, 'in', 'both'); //use GPIO pin 17 as input, and 'both' button presses,
and releases should be handled
pushButton.watch(function (err, value) {
//Watch for hardware interrupts on pushButton GPIO, specify callback function
if (err) { //if an error
console.error('There was an
error', err); //output error message to console
return;
}
LED.writeSync(value); //turn LED on or off depending on the button state (0 or
1)
});
function unexportOnClose() { //function to run when exiting program
LED.writeSync(0); // Turn LED off
LED.unexport(); // Unexport LED
GPIO to free resources
pushButton.unexport(); // Unexport Button
GPIO to free resources
};
process.on('SIGINT', unexportOnClose); //function to
run when user closes using ctrl+c
Press "Ctrl+x
" to save the code. Confirm with "y
", and confirm the name with "Enter
".
Run the code:
pi@w3demopi:~ $ node buttonled.js
Now the LED should turn on when you press the button, and turn off when you release it.
End the program with Ctrl+c
.