Meld Homepage , Electronic PCB PCBA , Software .NET AVR PLM BOM , Network TCP UDP MQTT , Microcontrollers AVR ARM , Powersupplys Switchmode , Nursecall MC5000 CD2000

Date: 2017-09-12
Author: Erik Leth Danielsen
How to Blink an LED on Raspberry Pi 3 Node.js

How to Blink an LED on Raspberry Pi 3 Node.js

Short:
My goal was to describe how to blink an LED mainly in node.js


Hardware:

Connect led+ to (GPIO Pin11) and led- to (GPIO Pin9 GND) through a 680R resistor.
gpio drawing


1 Nodejs
2 C
3 Pyton
4 Scratch

Software Nodejs:

Short:
I have tried npm wiring-pi and WiringPi-Node, but it doesn't work on raspberry pi 3
It's based on wiringpi.h
Youtube wiring-pi nodejs

Instead i found npm rpio
It's based on bcm2835.h
Support In, Out, I2C, PWM, SPI

Get the Newest version of Nodejs and Npm:

Obs1 Update Pre installed nodejs and node-red, on a raspberry pi:
$ update-nodejs-and-nodered
$ sudo apt-get install npm
See node-red

Obs2 Install nodejs and npm, on a raspberry pi:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install nodejs npm

Install rpio:

Download code:
meldcorpio.zip

Install rpio option 1:
move meldcorpio folder to home/pi by ftp
$ cd meldcorpio
$ npm install 'This will install the dependencies described in package.json

Install rpio option 2:
move meldcorpio folder to home/pi by ftp
$ cd meldcorpio
$ npm install rpio
npm rpio

Code:

Blink Sync: (sleep bad programming)
To run it
$ cd meldcorpio
$ node blinksync.js

//Get rpio object

var rpio = require('rpio');

//Set Pin 11 to Output Init Low

rpio.open(11, rpio.OUTPUT, rpio.LOW);

//Loop/Blink 5 times

for (var i = 0; i < 5; i++) {

       /* On for 1 second */

       rpio.write(11, rpio.HIGH);

       rpio.sleep(1);

        /* Off for half a second (500ms) */

       rpio.write(11, rpio.LOW);

       rpio.msleep(500);

}

 

Blink Async: (This is the right way to do it)
To run it
$ cd meldcorpio
$ node blinkasync.js

console.log("press ctrl+c to exit");

//Get rpio object

var rpio = require('rpio');

//Set Pin 11 to Output Init Low

rpio.open(11, rpio.OUTPUT, rpio.LOW);

//Timer Toggle led every 500mSec

var state=false;

setInterval(blink, 500);

function blink()

{

       if(state)rpio.write(11, rpio.HIGH);

       else rpio.write(11, rpio.LOW);

       state=!state;

}

 

Loop Speed Test, Benchmarking Nodejs:
To run it
$ cd meldcorpio
$ node tstloop.js
This will give a square wave 970KHz
sometimes 486Khz maybe node does something else in the background.

console.log("press ctrl+c to exit");

//Get rpio object

var rpio = require('rpio');

//Set Pin 11 to Output Init Low

rpio.open(11, rpio.OUTPUT, rpio.LOW);

//Loop Toggle led until ctrl+c

while(1) {

       rpio.write(11, rpio.HIGH);

       rpio.write(11, rpio.LOW);

}

 


See other language Benchmarking Gpio

Software C:


bcm2835.h:


Install:

bcm2835

Code:

Blink Sync:

//Blink GPIO17 Pin11 GPIO_0

#include <stdio.h>

#include <bcm2835.h>

int main(void)

{

    printf("press ctrl+c to exit\n");

    if(!bcm2835_init())return 1;

    bcm2835_gpio_fsel(17,BCM2835_GPIO_FSEL_OUTP);

    while(1)

    {

        bcm2835_gpio_set(17);

        bcm2835_delay(500);

        bcm2835_gpio_clr(17);

        bcm2835_delay(500);

    }

    return 0;

}

 


wiringpi.h:


Install:

Wiring Pi

Code:

Blink Sync:

//Blink GPIO17 Pin11 GPIO_0

#include <stdio.h>

#include <wiringPi.h>

int main(void)

{

    printf("press ctrl+c to exit\n");

    wiringPiSetup();

    pinMode(0, OUTPUT);

    while (1)

    {

        digitalWrite(0, HIGH);

        delay(500);

        digitalWrite(0, LOW);

        delay(500);

    }

    return 0;

}

 


Software Python:

#Blink GPIO17 Pin11 GPIO_0

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(11, GPIO.OUT)

print "press ctrl+c to exit"

while True:

    GPIO.output(11,True)

    time.sleep(0.5)

    GPIO.output(11,False)

    time.sleep(0.5)

GPIO.cleanup()


Software Scratch:


Links:
node.js
Visual Studio Code
Benchmarking Gpio

Privacy Policy © MELDCO APS | skovsgaardsparken 6 | 8362 hoerning | E-mail: info@meldco.dk | VAT ID: 34576149 412773