Raspberry Pi. Using node. js to control Gpio

Source: Internet
Author: User
Tags install node

40 Gpio on a Raspberry Pi is the best thing to play.

They can be directly controlled by c,/c++, Python, Java and other languages.

Now, let's see how to do the same thing with node. js.

Before you try, install node. js, and move to: < Raspberry Pi. Installation System +NODE.JS+MONGODB Series Environment >

1. Prepare the Test folder

mkdir/home/pi/documents/test-gpio-demo/

2. Create a file Package.json

{  "name":"Test-gpio-demo",  "version":"0.0.1",  "Dependencies":{    "Rpio2":"0.4.1"  }}

3. Create a Master file Blank.js

//Blink.jsConstGpio = require ('/home/pi/documents/test-gpio/node_modules/rpio2/lib/index.js'). Gpio;varLED =NewGpio ( $);//Create P36 PinLed.open (Gpio.output, gpio.low);//set to OUTPUT, default low for(vari =0; I < -; i++) {led.toggle (); //Toggle the level status of LEDsLed.sleep ( -);//wait 500ms}led.close ();

4. Hardware Circuit Connection

36th Foot (GPIO16) through a resistor (220 kohm or 1K), and then received the positive level of the LED

Led negative level to 34th foot (GND)

5. Trial run

Node/home/pi/documents/test-gpio-demo/blank.js

Flash Light Effect:

Bright Lights

Extinguishing lamp:

Test success!

=============================== Divider Line =========================

Rpio2 General Usage:

Synchronously
Const Gpio = require ('./lib/index.js'). Gpio; Const New Gpio (gpio.open); (gpio.output);  for (var0; i++) {    gpio.toggle ();    Gpio.sleep (+);} Gpio.close ();

Asynchronously

ConstGpio = require ('.. /lib/index.js'). Gpio;ConstGpio =NewGpio ( +); Gpio.open (gpio.output);voidfunction Loop () {Promise.resolve (Gpio.toggle ()). Then (Gpio.sleep.bind (NULL, -,true) . Then (Loop)} ();p Rocess.on ("SIGINT", function () {gpio.close (); Console.log ('shutdown!'); Process.exit (0);});

Toggle with button

ConstGpio = require ('.. /lib/index.js'). Gpio;Constbutton =NewGpio ( +);ConstOutput =NewGpio ( +); Button.open (gpio.input); Output.open (Gpio.output, Gpio.low) ;//Button downButton.on ('Rising', function () {output.toggle ();}); Process.on ("SIGINT", function () {button.close ();  Output.close (); Console.log ('shutdown!'); Process.exit (0);});

Raspberry Pi. Using node. js to control Gpio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.