๐Ÿ’ก Node.js โ€“ Blinking LED โ€“ Control Raspberry Pi GPIO with Node.js


๐Ÿงฒ Introduction โ€“ Why Blink an LED with Node.js?

Controlling a blinking LED is the classic first project in hardware programming. Using Node.js on a Raspberry Pi, you can interact with GPIO (General Purpose Input/Output) pins and create real-time control of electronic components like LEDs, sensors, motors, and more.

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

  • How to wire an LED to your Raspberry Pi GPIO pins
  • Use Node.js and the onoff library to blink the LED
  • Set up intervals for blinking
  • Safely clean up GPIO resources

๐Ÿ”Œ Hardware Requirements

๐Ÿงฐ Component๐Ÿ“„ Details
Raspberry Pi (any)Model 3B, 4, or Zero W works fine
BreadboardFor prototyping
LED (any color)Standard 5mm LED
330ฮฉ ResistorProtects the LED from overcurrent
Male-to-Male WiresFor GPIO connections

๐Ÿงพ Wiring โ€“ GPIO Pinout

  • Connect GPIO 17 (Pin 11) to the long leg (+) of the LED
  • The short leg (-) of the LED goes through a 330ฮฉ resistor to GND (Pin 6)

๐Ÿ“Œ Use pinout.xyz to confirm pin mapping.


๐Ÿ“ฆ Install Required Node.js Package

Install the GPIO controller library:

npm install onoff

๐Ÿ’ก Blink LED with Node.js (Using onoff)

// Load the onoff module to control GPIO
const Gpio = require('onoff').Gpio;

// Set GPIO17 as an output pin
const led = new Gpio(17, 'out');

// Blink every 500ms
const blinkInterval = setInterval(() => {
  // Toggle LED: 1 to 0 or 0 to 1
  led.writeSync(led.readSync() ^ 1);
}, 500);

// Stop after 10 seconds
setTimeout(() => {
  clearInterval(blinkInterval); // stop blinking
  led.writeSync(0);             // turn LED off
  led.unexport();               // free resources
  console.log('๐Ÿ›‘ LED blinking stopped');
}, 10000);

๐Ÿงช Output โ€“ What Youโ€™ll See

  • The LED connected to GPIO17 blinks ON/OFF every 0.5 seconds.
  • After 10 seconds, it stops and turns off automatically.

๐ŸŸข ON โ€“ when led.writeSync(1)
๐Ÿ”ด OFF โ€“ when led.writeSync(0)


๐Ÿงฑ Best Practices for GPIO in Node.js

โœ… Practice๐Ÿ’ก Reason
Always call unexport()Frees the GPIO pin when script ends
Add cleanup on exitPrevents LED from staying ON after process stops
Use resistorsProtects the LED and Raspberry Pi hardware
Avoid running as sudo unless requiredPrevents permission issues but be cautious

๐Ÿ” Bonus โ€“ Graceful Exit Cleanup

process.on('SIGINT', () => {
  led.writeSync(0);
  led.unexport();
  console.log('\n๐Ÿงน Cleanup done. Exiting...');
  process.exit();
});

๐Ÿ“Œ Ensures the LED is turned off if the script is stopped manually.


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

Controlling an LED with Node.js on Raspberry Pi is a foundational project to understand GPIO interaction. It prepares you for more complex projects like sensors, relays, or motor control.

๐Ÿ” Key Takeaways:

  • Use onoff to access and control GPIO pins in Node.js
  • Use setInterval and setTimeout for blinking logic
  • Always cleanup with unexport() to free GPIO
  • Works great as an intro to IoT and Node.js hardware

โš™๏ธ Real-world relevance:
Used in smart home projects, IoT devices, hardware control panels, classroom electronics, and LED status indicators.


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


โ“ Which GPIO pins can I use to control LEDs?
โœ… Any programmable GPIO like 17, 18, 27, etc. Avoid reserved ones (like GPIO2/3 used for I2C).


โ“ Can I control multiple LEDs?
โœ… Yes. Create multiple Gpio objects with different pin numbers.

const led1 = new Gpio(17, 'out');
const led2 = new Gpio(18, 'out');

โ“ Do I need to run the script as sudo?
โœ… Sometimes, especially for accessing /sys/class/gpio. Use:

sudo node blink.js

โ“ Why use a resistor with the LED?
โœ… It limits current to prevent burning out the LED or damaging GPIO pins.


โ“ Can I control LED blink rate dynamically?
โœ… Yes, use a variable for interval and update it using user input or logic.


Share Now :

Leave a Reply

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

Share

๐Ÿ’ก Node.js โ€“ Blinking LED

Or Copy Link

CONTENTS
Scroll to Top