๐Ÿ›Ž๏ธ Node.js โ€“ LED + Pushbutton โ€“ Control GPIO Input & Output on Raspberry Pi


๐Ÿงฒ Introduction โ€“ Why Combine LED with a Push Button?

Combining an LED and a pushbutton with Node.js on a Raspberry Pi allows you to build interactive hardware applications. From doorbells to voting machines, this simple project teaches you how to handle GPIO input (button) and output (LED) using the onoff library in Node.js.

๐ŸŽฏ In this guide, youโ€™ll learn:

  • How to wire an LED and button to Raspberry Pi GPIO
  • Detect button press and control LED using Node.js
  • Use GPIO input events (rising, falling)
  • Handle cleanup and debounce for smooth interactions

๐Ÿ”Œ Hardware Required

๐Ÿงฐ Component๐Ÿ“‹ Description
Raspberry PiAny model with GPIO header
LED (any color)Visual indicator
330ฮฉ ResistorFor LED current protection
Push Button SwitchStandard tactile switch
10kฮฉ ResistorPull-down resistor for stable signal
Breadboard + Jumper WiresFor easy prototyping

๐Ÿงพ Wiring Overview

๐Ÿ”‹ LED Circuit

  • GPIO 17 (Pin 11) โ†’ 330ฮฉ Resistor โ†’ LED Anode (long leg)
  • LED Cathode (short leg) โ†’ GND (Pin 6)

๐Ÿ”˜ Button Circuit

  • One leg of the button โ†’ GPIO 4 (Pin 7)
  • Same leg โ†’ 10kฮฉ Resistor โ†’ GND (Pin 6)
  • Other leg โ†’ 3.3V (Pin 1)

๐Ÿ“Œ The 10kฮฉ pull-down resistor ensures the input stays LOW when button is unpressed.


๐Ÿ“ฆ Install Required Library

npm install onoff

๐Ÿ’ก Node.js Code โ€“ LED with Push Button

const Gpio = require('onoff').Gpio;

// GPIO17 for LED (output), GPIO4 for Button (input with interrupt)
const led = new Gpio(17, 'out');
const button = new Gpio(4, 'in', 'rising', { debounceTimeout: 10 });

// Listen to button press
button.watch((err, value) => {
  if (err) {
    console.error('โŒ Error:', err);
    return;
  }
  
  console.log('๐Ÿ”˜ Button pressed!');
  led.writeSync(1); // Turn LED ON
  
  // Turn off LED after 500ms
  setTimeout(() => {
    led.writeSync(0);
  }, 500);
});

// Cleanup on exit
process.on('SIGINT', () => {
  led.writeSync(0);
  led.unexport();
  button.unexport();
  console.log('\n๐Ÿงน Clean GPIO and Exit.');
  process.exit();
});

๐Ÿงช Output โ€“ What Happens

  • Each button press turns the LED ON for 500ms.
  • Output in terminal: ๐Ÿ”˜ Button pressed!

๐Ÿงฑ Best Practices for GPIO Input/Output

โœ… Practice๐Ÿ’ก Why Itโ€™s Useful
Use debounceTimeoutPrevents multiple triggers from button bounce
Always unexport GPIOs on exitFrees up pins and avoids lock issues
Test button logic with LEDsEasy visual feedback for button state
Avoid floating GPIO inputAlways use pull-down or pull-up resistors
Use rising, falling, or bothChoose based on action trigger (press/release)

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

By combining an LED and a button using Node.js on a Raspberry Pi, you learned how to respond to physical events using GPIO. This is the foundation of many real-world IoT and embedded system projects.

๐Ÿ” Key Takeaways:

  • Use Gpio(pin, 'in', 'rising') to detect button press
  • Use watch() to listen for GPIO changes asynchronously
  • Combine GPIO input and output for interactive circuits
  • Debounce input to avoid multiple unwanted triggers

โš™๏ธ Real-world relevance:
Used in IoT dashboards, home automation, security systems, voting booths, and educational tools.


โ“FAQs โ€“ Node.js with Button & LED on Raspberry Pi


โ“ Why does the LED stay on sometimes?
โœ… Make sure led.writeSync(0) is called after use. Also check wiring or remove debounce delay.


โ“ What does debounceTimeout do?
โœ… It prevents repeated triggers caused by switch bouncing. A value like 10ms usually works well.


โ“ Can I detect button release too?
โœ… Yes. Change the input mode to 'both' and check the value parameter (1 for press, 0 for release).

const button = new Gpio(4, 'in', 'both');

โ“ Can I use more than one button or LED?
โœ… Absolutely! Just create more Gpio() instances with different pin numbers.


โ“ Do I need sudo to run this code?
โœ… On some systems, GPIO access requires root. Run with sudo if you get permission errors:

sudo node led-button.js

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ›Ž๏ธ Node.js โ€“ LED + Pushbutton

Or Copy Link

CONTENTS
Scroll to Top