RaspbPi LED and Pushbutton

Node.js Raspberry Pi GPIO: LED and Pushbutton Interaction

Creating an interactive hardware project with a Raspberry Pi is an exciting way to delve into the world of electronics and coding. In this guide, we’re going to explore how to control an LED light with a pushbutton using the GPIO (General Purpose Input Output) pins of a Raspberry Pi, all through the power of Node.js. This project offers a practical application of both input (pushbutton) and output (LED) using the GPIO pins, providing a foundational understanding of how to interact with hardware components through programming.

What Do We Need?

Before we dive into building the circuit and writing the script, let’s ensure we have all the necessary components:

  • Raspberry Pi (any model that has GPIO pins will work, but we’ll assume a Raspberry Pi 3 or newer for this guide)
  • Breadboard
  • LED light
  • Pushbutton
  • 220-ohm resistor (for the LED)
  • 10k-ohm resistor (for the pushbutton)
  • Jumper wires
  • An internet connection on your Raspberry Pi
  • Node.js installed on your Raspberry Pi

Building the Circuit

First, let’s assemble our circuit. This involves connecting the LED and pushbutton to the GPIO pins of the Raspberry Pi in such a way that pressing the button will change the state of the LED.

  1. LED: Connect the long leg (anode) of the LED to one of the GPIO pins (we’ll use GPIO17 for this example) through a 220-ohm resistor. Connect the short leg (cathode) to a ground (GND) pin on the Raspberry Pi.
  2. Pushbutton: Place the pushbutton on the breadboard. Connect one side to a GPIO pin (we’ll use GPIO27) and the other side to a ground (GND) pin on the Raspberry Pi. Use a 10k-ohm resistor to pull up the GPIO pin to HIGH (connect the resistor from GPIO27 to 3.3V).

Raspberry Pi and Node.js: LED and Button Script

Now, let’s get to the coding part. We’ll write a Node.js script that listens for a button press and turns the LED on or off accordingly.

First, ensure you have the onoff package installed, which allows us to interact with the GPIO pins:

npm install onoff

Here’s the Node.js script:

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

// Define the LED and Button pins
const led = new Gpio(17, 'out');
const button = new Gpio(27, 'in', 'both');

// The callback function to be called whenever the button is pressed or released
button.watch((err, value) => {
  if (err) {
    console.error('There was an error', err);
    return;
  }

  // If the button is pressed (value is 1), turn the LED on. Otherwise, turn it off.
  led.writeSync(value);
});

process.on('SIGINT', () => {
  led.unexport();
  button.unexport();
});

In this script, we:

  1. Import the onoff module and instantiate Gpio objects for both the LED and pushbutton.
  2. Use the button.watch method to add a callback function that toggles the LED based on the button’s state.
  3. Ensure we clean up by unexporting the pins when the script is stopped (with a CTRL+C, for example).

Wrapping Up

You’ve now created a simple, yet powerful, interaction between hardware components using Node.js on a Raspberry Pi. This project serves as a stepping stone into the vast world of electronics and IoT (Internet of Things), demonstrating how software can interact with the physical world. By experimenting further and integrating more components, you can build increasingly complex systems, paving the way for sophisticated projects and applications.