Welcome to The Coding College! In this tutorial, we will combine an LED and a pushbutton to create an interactive project using Node.js and the GPIO pins of a Raspberry Pi. This project introduces the concept of reading input signals (pushbutton) and controlling outputs (LED).
What You’ll Learn
- How to connect and program a pushbutton to work with GPIO pins.
- How to use a button press to control an LED.
- Best practices for Node.js GPIO projects.
Prerequisites
- Raspberry Pi with an operating system installed (e.g., Raspberry Pi OS).
- Node.js installed on your Raspberry Pi. If not, check out our Node.js Setup Guide.
- Basic knowledge of electronics and JavaScript.
Hardware Requirements
- 1 x Raspberry Pi (any model with GPIO pins).
- 1 x LED.
- 1 x Resistor (330Ω recommended for the LED).
- 1 x Pushbutton.
- 1 x Resistor (10kΩ for the pushbutton).
- Jumper wires.
- Breadboard.
Circuit Setup
LED Connection
- Connect the positive leg of the LED to GPIO17 (Pin 11) via a 330Ω resistor.
- Connect the negative leg of the LED to GND (Pin 6).
Pushbutton Connection
- Connect one terminal of the button to GPIO27 (Pin 13).
- Connect the other terminal to GND (Pin 6).
- Add a 10kΩ pull-up resistor between GPIO27 and 3.3V to prevent floating signals.
Refer to the GPIO Pinout Diagram for accurate pin connections.
Installing Required Libraries
Install the onoff
library for GPIO control:
npm install onoff
Node.js Code
- Open a terminal and create a new file:
nano led_button.js
- Add the following code:
const Gpio = require('onoff').Gpio; // Import onoff library
// Initialize GPIO pins
const led = new Gpio(17, 'out'); // GPIO17 as output for LED
const button = new Gpio(27, 'in', 'both'); // GPIO27 as input for the button
// Button press handler
button.watch((err, value) => {
if (err) {
console.error('Error reading button:', err.message);
return;
}
led.writeSync(value); // Turn LED on/off based on button state
console.log(`Button pressed: ${value}`);
});
// Graceful shutdown
process.on('SIGINT', () => {
led.writeSync(0); // Turn off LED
led.unexport(); // Free GPIO resources
button.unexport();
console.log('Exiting...');
process.exit();
});
- Save and exit: Press
Ctrl + O
,Enter
, andCtrl + X
.
Running the Code
Run the script with sudo
for GPIO access:
sudo node led_button.js
Expected Behavior
- When you press the button, the LED turns on.
- When you release the button, the LED turns off.
Understanding the Code
- GPIO Initialization:
led
: Configured as an output pin to control the LED.button
: Configured as an input pin with theboth
edge to detect button presses and releases.
button.watch()
: Listens for state changes on the button pin (pressed/released) and executes a callback function.- Graceful Shutdown: Ensures that GPIO resources are freed when the program exits.
Enhancements
- Toggle LED State: Modify the code so the LED toggles on/off with each button press.
- Debouncing: Add software debouncing to prevent false triggers caused by noise.
- Multiple Buttons: Control multiple LEDs with different buttons.
Best Practices
- Pull-Up/Down Resistors: Always use resistors to prevent floating GPIO pins.
- Proper Shutdown: Free GPIO resources to avoid errors in future executions.
- Avoid Overloading GPIO Pins: Use external components for high-power devices.
Conclusion
This project demonstrates how to combine inputs and outputs on a Raspberry Pi using Node.js. By integrating a pushbutton and an LED, you’ve taken your first step into creating interactive hardware projects.
For more coding tutorials and hardware projects, visit The Coding College.