[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

[pi8] Turning on-off a LED with rpi-gpio.js

Before continue reading, please take a look at GPIO(General Purpose Input/Output ports). See also GPIO pinout to explore different pin interfaces.

In addition to your Raspberry Pi running Raspbian & S8 and rpi-gpio.js loaded, what you will need is:

BreadBoardUploaded Image: breadboard.png
A led x1Uploaded Image: greenled.jpg
330 ohm resistor x1Uploaded Image: 330Ohms.jpg
Male-Female jumper wiresUploaded Image: jWires.jpg

J8 Expansion Header

Check the GPIO pins layout on your pi device.

Install rpi-gpio.js

Check for rpi-gpio.js documentation and follow install instructions
$ sudo npm install rpi-gpio


Making connections

In order to try a minimal sample we need to do the connections as follow:

Uploaded Image: ledPin3.jpg


First try with rpi-gpio.js

  1. Copy the next few lines of js code to a file named ledOn.js
    var gpio = require('rpi-gpio');
     
    gpio.setup(3, gpio.DIR_OUT, write); 
    function write() {
        gpio.write(3, true, function(err) {
            if (err) throw err;
            console.log('Written to pin');
        });
    }
    
  2. Start this sample with,
    $ sudo node ledOn.js
    and you should notice the LED on.
  3. Stop node execution with Ctrl-C

Blinking LED with S8

Final step is trying a sample with S8:
  1. Start your S8 system server executing
    $ sudo node n8.snapshot.js
    take note of the server address e.g. SystemServer running at http://192.168.1.???:8088
  2. connect from U8 tools image to S8 system running in your Raspberry Pi.
  3. Load rpi-gpio framework to your system (using the remote/connected workspace) doing a #fileIn.
  4. Finally copy this few lines to your remote workspace. Just select/paint all this code and click on #doIt button
    | gpio pin delay count max off on |
    gpio  := RPI gpio.
    pin   := 3.
    delay := 50.
    count := 0.
    max   := 50.
    
    off := [
        [gpio write: pin value: 0 with: on]
            valueDeferred: delay
    ].	
    on := [	
        count > max ifTrue: [
    	gpio destroy.
    	self print: 'Close pins...'.		
        ] ifFalse: [
    	[ gpio write: pin value: 1 with: off.
    	  count := count + 1.
    	] valueDeferred: delay
        ]
    ].
    gpio 
    	setup: pin 
    	direction: gpio dirOut
    	with: on.
    What you'll see is a cycle of blinking 50 times with an interval of 50 ms. Congratulations!!


References