In this chapter we will use several GPIO pins to create a "flowing" effect by turning them on and off in sequence.
For this you need:
Note: The resistor you need can be different from what we use depending on the type of LEDs you use. Most small LEDs only need a small resistor, around 200-500 ohms. It is generally not critical what exact value you use, but the smaller the value of the resistor, the brighter the LED will shine.
Click the links in the list above for descriptions of the different components.
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.
Look at the above illustration of the circuit.
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.
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:
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
.