Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
RaspberryPi,NodeJSandBasicElectronics
FxNDev
Raspberry Pi, NodeJS and Basic
Electronics
SUNDAY, OCTOBER 19, 2014
Now that we have a good project setup it's time to do something
with it. If you haven't already I would recommend you read
through my previous post Raspberry Pi NodeJS Project Setup with
Git Deployment as I will be continuing from where we left o.
In this post we'll be doing some very basic electronics using the
Raspberry Pi General Purpose Input/Output aka GPIO. That of
course means we will have to play with some LEDs (yay!).
GOALS
Here's what we're hoping to accomplish:
Light up an LED by wiring it up to the GPIO
Using a transistor and code to switch the LED On and O
Using Pulse Width Modulation (PWM) to control the LED
brightness
REQUIREMENTS
http://blog.fxndev.com/raspberrypiandledfun/
1/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
http://blog.fxndev.com/raspberrypiandledfun/
2/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
We'll start by simply wiring up the LED and the resistor to the pins
on the Pi.
If you are using a good quality breakout board that labels the pins
excellent if not refer to the diagram found here.
Find the 3 volt power supply and the ground pins, usually these are
labelled 3V (or 3v3) and GND.
We want to:
Connect the 3V to the anode of the LED (the longer leg)
Cathode of the LED (the other leg) to the resistor
Resistor to the ground
http://blog.fxndev.com/raspberrypiandledfun/
3/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
If you now plug in the Pi's power supply you should see the LED
http://blog.fxndev.com/raspberrypiandledfun/
4/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
light up!
5/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
6/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
This will, create a tools directory, pull the source code for GPIO
Admin from Github, builds and installs the software and adds our
pi user to the gpio group of users which have permission to access
the GPIO pins.
Now disconnect from the SSH session and reconnect again to
ensure that the correct permissions get applied.
http://blog.fxndev.com/raspberrypiandledfun/
7/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
If everything is well the LED should now light up! To turn the LED
o again run the following commands:
echo0>/sys/devices/virtual/gpio/gpio17/value
gpioadminunexport17
Woooo!
Let's code!
Hope you got some awesome nerd chills from that; I know I did the
rst time I did that. So let's do that in code now!
You should have the base project setup from last article so we'll
carry on with that as we already have it setup to deploy on the pi.
Now some of the NodeJS packages we'll be using will have native
addon modules as to enable us to use them we need to install a
handy tool called node-gyp.
Installation is simple just run the following on the PI:
npminstallgnodegyp
This might take a bit of time so make yourself a cuppa or say hello
to me on twitter @efexen.
Now that's done we'll move to work on our development machine.
First we'll install a lovely little package ono. ono allows us to
easily turn the GPIO pins on the Pi on or o (I see what they did
http://blog.fxndev.com/raspberrypiandledfun/
8/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
9/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
}).listen(1337);
This loads the HTTP module and URL module of NodeJS and the
ono module.
varled=newGpio(17,'out');
This creates the Gpio object to represent our LED located at pin 17
and it should be an output as we want to either turn it on or o
rather than reading values from it.
http.createServer(function(req,res){
This creates our HTTP server and creates a callback function that
will be called whenever the server receives a request.
res.writeHead(200,{'ContentType':'text/html'});
This writes status code 200 (OK) to the response and sets the
Content-Type header to text/html advising the connecting client to
expect HTML response. This will be handy later on if we want to
return HTML such as a link to turn the led on or o.
varcommand=url.parse(req.url).pathname.slice(1);
This uses the URL module of the NodeJS to parse the requested url
to it's components, we take the pathname component which is
anything after the host and port. Finally we call slice(1) on this
http://blog.fxndev.com/raspberrypiandledfun/
10/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
This writes the text "It's ON" to the response and closes the
request.
The various branches of the switch command either write 0 to the
LED and respond with dierent text or by default just greets you
with a silly message.
}).listen(1337);
http://blog.fxndev.com/raspberrypiandledfun/
11/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
gitaddA
gitcommitm"LEDOnOff"
gitpushpi
Do note that the deployment might take some time initially as the
ono package is installed using NPM and the native modules are
compiled. Once the deployment has nished you should be able to
connect to the pi using your browser: http://192.168.1.90:1337/
You should see a message, if however you navigate to
http://192.168.1.90:1337/on the LED should turn on! and /o will
turn it o again!
12/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
cd~/tools
gitclonegit@github.com:sarfata/piblaster.git
cdpiblaster
./autogen.sh
./configure
make
sudomakeinstall
Next we'll change our index.js to utilise our cool new stu:
varhttp=require('http');
varurl=require('url');
varpiblaster=require('piblaster.js');
http.createServer(function(req,res){
res.writeHead(200,{'ContentType':'text/html'});
varbrightness=url.parse(req.url).pathname.slice(1);
if(brightness.length===0||isNaN(brightness)){
res.end('hello?yes,thisispi!');
}else{
piblaster.setPwm(17,brightness/100);
res.end('Brightnesssetto:'+brightness+'%');
}
}).listen(1337);
So let's have a look at the les that are dierent from last:
varpiblaster=require('piblaster.js');
13/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
if(brightness.length===0||isNaN(brightness)){
We use the piblaster module to set the duty cycle of the pin 17 to be
our input brightness divided by 100. We divide by 100 as the
piblaster treats 0 as o and 1 as 100% duty cycle.
So if we commit and push the changes to the pi again we can see
this in action.
Navigating to the root url http://192.168.1.90:1337/ still just shows
the silly message but now if we navigate to
http://192.168.1.90:1337/50 we get the LED light up with 50% of the
full brightness, changing to /10 we get 10% and so on.
WHAT NOW?
Now you go and do all sorts of cool stu with this!! Using the
NodeJS timers it's easy to create a fading eect for the LED or
maybe an app with a slider that works like a dimmer. Very similar
circuit and code can be used to control motors which could be part
of a robot or your browser controlled window blinds or or or or...
I hope this has helped you get started with your Pi creating rst
simple projects and provided with enough information to continue
creating.
Let me know what you think on twitter @efexen or via email [fxn
at fxndev dot com] and stay tuned for the next article where we'll
http://blog.fxndev.com/raspberrypiandledfun/
14/15
24/03/2016
RaspberryPi,NodeJSandBasicElectronics
be playing with the physical-web project, the Pi, NodeJS and our
LED stu so far!
SHARE THIS POST
AUTHOR
FxN
http://blog.fxndev.com/raspberrypiandledfun/
15/15