Node.js Raspberry Pi GPIO – Blinking LED

Welcome to The Coding College! In this tutorial, we’ll guide you through creating a simple blinking LED project using Node.js and the GPIO (General-Purpose Input/Output) pins of a Raspberry Pi. This is a foundational project that introduces the basics of controlling hardware with Node.js.

What You’ll Learn

  • How to set up an LED circuit with a Raspberry Pi GPIO pin.
  • How to write a Node.js script to make the LED blink.
  • Best practices for working with GPIO and Node.js.

Prerequisites

  1. Raspberry Pi with an OS installed (e.g., Raspberry Pi OS).
  2. Node.js installed on your Raspberry Pi. (Check out our Node.js on Raspberry Pi Guide for installation steps.)
  3. Basic knowledge of JavaScript and hardware connections.

Hardware Requirements

  • 1 x Raspberry Pi (any model with GPIO pins).
  • 1 x LED.
  • 1 x Resistor (330Ω recommended).
  • Jumper wires.
  • Breadboard.

Circuit Setup

  1. Connect the LED to the GPIO Pin:
    • Positive terminal (longer leg): Connect to GPIO17 (Pin 11) via a resistor.
    • Negative terminal (shorter leg): Connect to GND (Pin 6).
  2. Use a breadboard to organize your connections and avoid short circuits.

Refer to the Raspberry Pi GPIO Pinout Diagram for accurate pin numbers.

Installing Required Node.js Library

Use the onoff library for GPIO control. Install it using:

npm install onoff

Node.js Script for Blinking LED

  • Open the terminal and create a new file:
nano blink.js
  • Add the following code to blink.js:
const Gpio = require('onoff').Gpio; // Import onoff library

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

let isOn = false;

// Toggle LED state every 1 second
setInterval(() => {
    isOn = !isOn;
    led.writeSync(isOn ? 1 : 0); // Write 1 (on) or 0 (off)
}, 1000);

// Graceful shutdown
process.on('SIGINT', () => {
    led.writeSync(0); // Turn off the LED
    led.unexport();   // Free GPIO resources
    console.log('Exiting...');
    process.exit();
});
  • Save and exit: Press Ctrl + O, Enter, and Ctrl + X.

Running the Script

Run the script with sudo to access GPIO:

sudo node blink.js

Output

  • The LED connected to GPIO17 should start blinking, toggling on and off every second.

Understanding the Code

  1. Gpio Object: Represents the GPIO pin.
    • new Gpio(17, 'out'): Configures GPIO17 as an output pin.
  2. setInterval: Repeats the LED toggle operation every 1000 milliseconds.
  3. Graceful Shutdown: Ensures the LED turns off and the GPIO pin is released when you terminate the program.

Enhancements

  1. Adjust Blink Rate: Modify the interval time to make the LED blink faster or slower.
  2. Control Multiple LEDs: Add more LEDs and use different GPIO pins.
  3. Add Button Input: Control the blinking manually with a button.

Best Practices

  1. Use Resistors: Prevent damage to the LED and GPIO pin.
  2. Proper Shutdown: Always release GPIO resources to avoid errors in future executions.
  3. GPIO Pin Safety: Never exceed the voltage or current limits of GPIO pins.

Conclusion

Creating a blinking LED with Node.js and a Raspberry Pi is an excellent starting point for hardware programming. This project demonstrates how easily Node.js can control hardware, paving the way for more advanced IoT and robotics projects.

For more tutorials and hands-on projects, visit The Coding College.

Leave a Comment