In the previous chapter we learned how to use a Raspberry Pi and its GPIO to make a LED blink.
For that we used a GPIO pin as "Output".
In this chapter we will use another GPIO pin as "Input".
Instead of blinking for 5 seconds, we want the LED to light up when you push a button connected to the breadboard.
In this chapter we will create a simple example where we control a LED light with a Push Button.
For this you need:
Click the links in the list above for descriptions of the different components.
Note: The resistor you need can be different from what we use depending on the type of LED 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.
In this chapter we will build on the circuit we built in last chapter, so you will recognize some of the parts in the list above.
Now it is time to build the circuit on our Breadboard. We will use the circuit we created in the last chapter as a starting point.
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 "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:
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
.