Raspberry Pi 3 - Simple LED Blinking Program Using Node.js

LED Blinking Program using NodeJs
                                    Figure 1:
LED Blinking Program using NodeJs

You can read my previous articles here:

For Flashing/Blinking the LED on breadboard using Node.js follow below steps

Required list of components:

  1. 1 - Raspberry Pi device with 5 V power supply.
  2. 1 - Bread Board
  3. 1 - Male to Female jumper cable
  4. 1 - LED

Step 1: Firstly, connect the GPIO pin to the bread board as shown in figure.

Connect the GPIO pin to the bread board
                               Figure 2: Connect the GPIO pin to the bread board

Step 2: Here we use the PIN Number: “ 4 “ (As per number 4(pin rank 7) , third from the top – left ). For getting the output from the raspberry pi connect eh jumper wire from Raspberry PI GPIO pin “4” to bread board as shown in image,

Connect eh jumper wire from Raspberry PI GPIO pin
                              Figure 3: Connect eh jumper wire from Raspberry PI GPIO pin

Step 3: Now take LED and put it on bread board as shown in image.

Connect the Plus terminal of LED to the Jumper cable (green cable in image) coming from the GPIO Pin “4” as shown in image,

Connect the Plus terminal of LED to the Jumper cable
                        Figure 4: Connect the Plus terminal of LED to the Jumper cable

Step 4: Now take another jumper cable (black cable in image) connect it the GPIO pin “Ground” as per number 3(Pin rank 6), third from the top right . And add other terminal of this wire to minus terminal of the LED as shown in figure.

Black cable in image
                                       Figure 5: Black cable in image

Step 5: Here for detecting the plus/minus terminal of LED, bigger (toll) terminal of LED is Plus and small (short) terminal of LED is minus. Or you can define it by cutting side part of LED that is Minus terminal.

Detecting the plus-minus terminal of LED
                                       Figure 6: Detecting the plus-minus terminal of LED

Step 6: Now write the Node.js program for blink LED every 1 second for 1 minute.

Here we use “Onoff ” module for the accessing the GPIO Pin of raspberry pi device.

  1. -- -- -- -- -- -- -- -- --  
  2. var Gpio = require('onoff').Gpio,  
  3.     led = new Gpio(4, 'out');  
  4. var iv = setInterval(function ()  
  5. {  
  6.     led.writeSync(led.readSync() === 0 ? 1 : 0)  
  7. }, 1000);  
  8. // Stop blinking the LED and turn it off after 5 seconds.  
  9. setTimeout(function ()  
  10. {  
  11.     clearInterval(iv); // Stop blinking  
  12.     led.writeSync(0); // Turn LED off.  
  13.     led.unexport(); // Unexport GPIO and free resources  
  14. }, 60000);  
Step 7: Now run the program and the following output can be seen:

Program Show the Output
                 Figure 7: Program Show the Output


Now enjoy with your Raspberry Pi. If you liked my tutorial, then please comment on this.

Next Recommended Readings