Raspberry Pi Home Security with Node.js — Node installation, adding an LCD & PIR sensor

Dan Laidlow
6 min readMay 14, 2018

--

In this post I’ll cover installation of Node.js on the Pi and then dive into adding an LCD and the PIR sensor. To install Node run the following commands:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -sudo apt-get install -y nodejs

To confirm the installation was successful type the following into a terminal and you should see the version of Node displayed.

node -v

Adding a 16x2 character LCD

We’ll start out adding the LCD display to the Pi. The LCD will be used during the testing phase to show when the PIR has sensed motion but you could also use an LED to achieve the same result. I like the I2C-1602 LCD modules because they are cheap and only require 4 wires (5V, GND, SDA, SCL) to control using the Pi’s I²C bus.

PLEASE NOTE: All wiring should be done while the Pi is off and power unplugged. Even after shutting down Pi the USB will still be supplying power to the 40-pin GPIO header.

Pi 16x2 LCD wiring

Color | Function | Pi pin number

  • Black | Ground | PIN 6
  • Red | 5V | PIN 2
  • Blue | SDA | PIN 3-BCM2
  • Green | SCL | PIN 5-BCM3

For things like this it’s handy to have some breadboard cables around, the Female-Female ones are handy for connecting breakout boards and sensors to the Pi while testing. For longer term applications I like using Dupont style crimper and connectors to make my own cables but breadboard cables work just fine for testing.

With everything connected it’s time to plug the Pi back in and boot it up. Once it’s powered on you should see the screen light up, then we just need to confirm its visible on the I²C bus. Open a terminal and enter the following:

i2cdetect -y 1

This will show a table of all the I²C devices connected to bus 1 on the Pi. All I²C devices come with a default address which in the case of my LCD is 0x27, in the photo below that is represented in the table as 27. Check your device documentation for the default address as they vary across manufacturers.

Now that we’ve confirmed the LCD is connected correctly we can start work on the code to control it. First you’ll need to install the lcdi2c package from npm, enter the following command in a terminal.

npm install lcdi2c

Then we need to create a new file called pi.js and add the following:

var lcdi2c = require('lcdi2c');
var lcd = new lcdi2c(1, 0x27, 16, 2);

The first line tells Node we require the lcdi2c module from npm, the second line uses this module to create a connection with the LCD. The constructor parameters in order are bus, address, columns & rows. So in the example above you are connecting to bus 1 at address 0x27 using a 16x2 character LCD. Then it’s just a few more lines to display some text, add the following to pi.js:

function sendMessage(message) {
var currentTime = new Date();
lcd.clear();
lcd.println(currentTime.toTimeString().substring(0, 8), 1);
lcd.println(message, 2);
}
sendMessage('It worked!');

Here we are defining a function that accepts a message parameter which will be displayed on the LCD. We start by getting the current date and time (this will become handy later) and in the second line we’re clearing the display. Then we’re printing messages to each row on the LCD with the time and the message.

Save pi.js and then open a terminal. Navigate to the directory where you saved your code and enter the following:

node pi.js

If it worked then you should see something that looks like this!

Adding a PIR sensor

Now for the most important part of this project, the PIR sensor. Turn off the Pi and disconnect the power for the next step.

Pi PIR sensor wiring

Color | Function | Pi PIN number

  • Black | Ground | PIN 34
  • Red | 5V | PIN 4
  • Yellow | Digital IO | PIN 32-BCM12

Reading the sensor is easy as it’s just a digital output with two states, high and low. Depending on the PIR sensor you may need to set some trimpots on the board for the delay and sensitivity of the sensor. I have mine set to the following values which seems to work pretty well.

Sensitivity: Set to around 2 oclock on the dial, about 3/4 of a turn on the trimpot in the clockwise direction. This sets the distance the sensor will pick up motion at.

Time: All the way counter clockwise for lowest delay.

The key when adjusting the trimpots on the sensor is to give it some time to settle before testing, it seems to need about 10–15 seconds before it collects enough of a reading to work reliably. So if you’re holding the sensor in your hand adjusting the pots you’ll have a hard time, adjust them and then set the sensor down for a while so it gets a good reading.

Once you’ve got everything connected power up the Pi. We’ll need to install the onoff module to read the sensor so enter the following in a terminal:

npm install onoff

Open up pi.js and add the following at the top of the file below the lcd variables.

var gpio = require('onoff').Gpio;
var pir = new gpio(12, 'in', 'both');

The first line tells Node we require the onoff module that we installed earlier, the second line creates a reference to PIN 12 on the Pi. The pir constructor parameters in order are pin, direction, edge. Direction refers to whether we want to use the pin as an input or an output, because we’re listening to the PIR sensor we want ‘in’. The edge parameter has three possible settings — ‘rising, ‘falling’ and ‘both’. We want to know when the pin goes high and low so we’ve selected ‘both’. Directly below the pir variable add the following:

pir.watch(function(err, value) {
if (value == 1) {
sendMessage('Intruder alert');
} else {
sendMessage('Intruder gone');
}
});

Here we’re setting up a function that will be executed when the pir state changes. When it goes high (1) that means it’s detected motion, it will then go low (0) when the the motion has stopped. We’ll re-use the sendMessage function we wrote earlier to update the LCD for each of these states. With that done pi.js should now look something like the the code below.

var lcdi2c = require('lcdi2c');
var lcd = new lcdi2c(1, 0x27, 16, 2);
var gpio = require('onoff').Gpio;
var pir = new gpio(12, 'in', 'both');
pir.watch(function(err, value) {
if (value == 1) {
sendMessage('Intruder alert');
} else {
sendMessage('Intruder gone');
}
});
function sendMessage(message) {
var currentTime = new Date();
lcd.clear();
lcd.println(currentTime.toTimeString().substring(0, 8), 1);
lcd.println(message, 2);
}

Save pi.js and run it in the terminal with the following:

node pi.js

If everything worked correctly you should see the something like the photo below when you move in front of the sensor!

That was far longer than I intended so I’ll wrap this up, keep an eye out for my next post which will go over adding the camera and setting up push notifications.

--

--