RaspPi GPIO Blinking LED

In this guide, we’ll explore how to use Node.js to control a GPIO (General Purpose Input Output) pin on a Raspberry Pi to make an LED blink. This project serves as an excellent starting point for those interested in hardware control using JavaScript. By the end of this tutorial, you will have a clear understanding of how to manipulate hardware through software, an essential skill for any budding Node.js developer interested in IoT (Internet of Things) projects.

1. Using the GPIO for Output

The GPIO pins on a Raspberry Pi are a bridge between the digital world of the microprocessor and the physical world. They can be programmed to interact with a wide range of external components, such as LEDs, sensors, and motors. In this tutorial, we focus on using a GPIO pin as an output to turn an LED on and off.

2. What do we need?

To follow along, you’ll need:

  • A Raspberry Pi (any model with GPIO pins will do)
  • An LED
  • A 220-ohm resistor (to protect the LED)
  • Jumper wires
  • Breadboard (optional, for easier circuit assembly)

3. Building the Circuit

Before we dive into the code, let’s set up the circuit:

  1. Insert the LED into the breadboard. LEDs have two legs; the longer leg (anode) should be connected to the Raspberry Pi’s GPIO pin, and the shorter leg (cathode) to one end of the resistor.
  2. Connect the other end of the resistor to one of the Raspberry Pi’s GND (ground) pins. This completes the circuit.
  3. Use jumper wires to connect the breadboard to the Raspberry Pi GPIO and GND pins.

4. Raspberry Pi and Node.js Blinking LED Script

First, ensure you have Node.js installed on your Raspberry Pi. You can install it using the package manager or download it directly from the Node.js website.

Next, we’ll use the onoff module for GPIO access through Node.js. Install it by running:

npm install onoff

Now, let’s write the script to blink the LED:

const Gpio = require('onoff').Gpio; // Import the onoff library
const led = new Gpio(4, 'out'); // Define the LED pin

// Function to turn the LED on and off
const blinkLed = () => {
  let value = 0;
  setInterval(() => {
    value = value ^ 1; // XOR operation to toggle the value between 0 and 1
    led.writeSync(value); // Set the LED pin to the current value, turning it on/off
  }, 1000); // Toggle every 1000ms (1 second)

// Start blinking

// Clean up on exit
process.on('SIGINT', () => {
  led.writeSync(0); // Turn LED off
  led.unexport(); // Unexport the LED GPIO to free resources
  process.exit(); // Exit the process

In this script, we utilize the onoff module to control the GPIO pins. We define an LED on GPIO pin 4 as an output. The blinkLed function toggles the LED on and off every second. We also handle the SIGINT signal to cleanly exit the program and turn off the LED.


Congratulations! You’ve successfully controlled an LED with your Raspberry Pi using Node.js. This project demonstrates the basics of hardware control and opens the door to more complex IoT projects using JavaScript. Experiment with different components and timings to further your understanding of GPIO interfacing. Your journey into the fascinating world of IoT with Node.js has just begun.