Brainy Pi

Available to select audience (currently beta)

Are you a Node.js developer looking to explore the world of GPIO programming? In this blog post, we will walk you through a step-by-step guide on how to blink an LED on a Brainy Pi using Node.js. Whether you’re a beginner or an experienced developer, we aim to explain the code in a simple and straightforward manner. Lets explore GPIO programming using nodeJS on Brainy Pi

Hardware Requirements

Before we dive into the programming aspect, let’s take a quick look at the hardware requirements for this project. To follow along, you will need the following:
  • Brainy Pi board
  • LED
  • Resistor (around 220 ohms)
  • Jumper wires
Make sure you have these components ready before moving on to the next section.

Installing Node.js on BrainyPi

To begin programming GPIO using Node.js on the Brainy Pi, you need to have Node.js installed on your device. If you haven’t already installed Node.js, follow these steps:
  1. Open the terminal on your Brainy Pi.
  2. Run the following command to update the package list:
    sudo apt update
  3. Install Node.js using the following command:
    sudo apt install nodejs
That’s it! Node.js is now installed on your Brainy Pi. You can verify the installation by running node -v in the terminal, which should display the Node.js version.

Coding the GPIO

Now that you have Node.js installed, let’s jump into the code.
  1. Download the code, by cloning
    git clone https://github.com/brainypi/brainypi-gpio-examples.git
    cd using-gpio-lib/nodejs
  2. The folder contains 2 files
    1. gpiolib.js – This contains the core gpio functions
    2. main.js – This is the example which uses the core gpio functions to blink the led
Below is the explanation of the main.js code:
var fs = require('fs');
var GPIO = require('./gpiolib');

var blink_period = 500; // in milliseconds

var gpio_154 = new GPIO.NewGpio('154', 'out', '0');
  • The code begins by importing the fs module, which provides access to the file system.
  • It also imports a custom module gpiolib using require('./gpiolib'). This module likely contains functions for GPIO manipulation.
  • The variable blink_period is set to 500 milliseconds, indicating the duration of each blink.
  • A new GPIO object is created using the NewGpio constructor from the gpiolib module. It takes three parameters: the pin number (‘154’), the direction (‘out’ for output), and the initial state (‘0’).
GPIO.pinMode(gpio_154, function(err, pin) {
    if (err) throw err; // Error occurred during pin export
    setInterval(function() {
        gpio_154.value == '0' ? GPIO.digitalWrite(gpio_154, '1') : GPIO.digitalWrite(gpio_154, '0');
    }, blink_period);
});
  • The GPIO.pinMode function is called, passing the gpio_154 object and a callback function. This function is responsible for setting the pin mode of the GPIO pin.
  • Inside the callback function, an interval is set using setInterval. This interval repeatedly executes the provided function at the specified blink_period.
  • The function inside setInterval checks the current value of gpio_154.value. If it is ‘0’, it calls GPIO.digitalWrite with the gpio_154 object and ‘1’ as arguments. If it is not ‘0’, it calls GPIO.digitalWrite with the gpio_154 object and ‘0’ as arguments.
  • The GPIO.digitalWrite function is responsible for writing the given value to the GPIO pin.
Overall, this code sets up a GPIO pin with number ‘154’ as an output pin, and then repeatedly blinks the LED connected to that pin by toggling the value between ‘0’ and ‘1’ at the specified blink_period.

Physical pins to GPIO number

From the above code it accepts GPIO number and not the physical gpio pins. To convert the physical gpio pins to GPIO number use the image below.

For example: Physical pin number 16 is GPIO number 154

Running the Program

To run the program and see the LED blink, follow these steps:
  1. Connect the LED to the Brainy Pi board using the appropriate GPIO pin.
  2. Open a terminal and navigate to the directory.
    cd using-gpio-lib/nodejs
  3. Run the program using the following command: sudo node main.js.
  4. Observe the LED blinking at regular intervals.
Ensure that the hardware connections are properly made before running the program.

Conclusion

Congratulations! You have successfully learned GPIO programming using nodeJS on Brainy Pi . By following the step-by-step explanation of the code, you should now have a better understanding of how to control GPIO pins and blink an LED using Node.js.
In this blog post, we covered the hardware requirements, installation of Node.js on Brainy Pi, coding the GPIO, and running the program. Moreover, we hope this information has provided you with valuable insights and a clear understanding of GPIO programming because it’s essential to grasp these concepts in order to effectively utilize Brainy Pi.
Feel free to explore further and expand your knowledge in this exciting field of development. Happy coding!
0 Comments

Leave a reply

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

*