You are on page 1of 7

LAB -12 Hardware integration with RaspberryPI

Team Members: -
1. Tom Jose Oorasala -19BLC1058
2. Ritesh Kumar Shukla -19BLC1076

Aim
Our aim is to perform hardware integration program using Raspberry PI.

Programs: -
1. Blinking LED using Raspberry PI

Procedure:

1. Import the onoff library.


2. Initialize GPIO 6 to be an output pin.
3. Set the interval to blink the LED for two seconds.
4. Synchronously read the value of pin 6 and transform 1 to 0 or 0
to 1.
5. Asynchronously write the new value to pin 6.
6. Listen to the event triggered by Ctrl-C (SIGINT).
7. Cleanly close the GPIO pin before exiting.
8. Create the file and save the file
9. Run the code – Node blink.js

Code: -
var onoff = require('onoff');
var Gpio=onoff.Gpio,led=new Gpio(6,'out'),interval;
interval = setInterval(function(){
var value=(led.readSync() +1)%2;
led.write(value,function(){
console.log("changed LED state to :"+value);
});
},2000);
process.on('SIGINT',function(){
clearInterval(interval);
led.writeSync(0);
led.unexport();
console.log('bye bye');
process.exit();
});

Output: -
Program 2:-PIR Sensor using Raspberry pi
Procedure:

1. Import the onoff library.


2. Initialize GPIO 6 to be an input pin.
3. Watch the sensor for error and value.
4. If there is error, unexport the GPIO and console log the error.
5. If value is 1. Console log (“There is someone”) else console log
(“Not Anymore”).
6. Create the file and save the file
7. Run the code – Node PIR.js

Code: -
var Gpio = require('onoff').Gpio,
sensor = new Gpio(17, 'in', 'both');
sensor.watch(function (err, value) {
if (err) exit(err);
console.log(value ? 'there is some one!' : 'not anymore!');
});
function exit(err) {
if (err) console.log('An error occurred: ' + err);
sensor.unexport();
console.log('Bye, bye!')
process.exit();
}
process.on('SIGINT', exit);
Output
Program 3 : Blink Led when Person is detected

Procedure:

1. Import the onoff library.


2. Initialize GPIO 17 to be an input pin.
3. Initialize GPIO 6 to be an output pin
4. Watch the sensor for error and value.
5. If there is error, unexport the GPIO and console log the error.
6. If value is 1. Synchronously read the value of pin 6 and
transform 0 to 1 else let the value be 0.
7. Create the file and save the file
8. Run the code – Node PIRLED.js

Code
var Gpio = require('onoff').Gpio,
sensor = new Gpio(17, 'in', 'both');
led=new Gpio(6,'out'),interval;
sensor.watch(function (err, value) {
if (err) exit(err);
if(value){
led.write(1,function(){
console.log("changed LED state to :"+value);
});
}
else{
led.write(2,function(){
console.log("changed LED state to :"+value);
});
}
});
function exit(err) {
if (err) console.log('An error occurred: ' + err);
sensor.unexport();
console.log('Bye, bye!')
process.exit();
}
process.on('SIGINT', exit);
Program 4 : Working with DHT sensor

Procedure:

1. Install the node DHT sensor and BCM2835 driver.


2. Initialise the sensor Lib.
3. Create a setInterval repeating every 2 seconds.
4. Read out sensor value.
5. Console log the values.
6. Create the file and save the file
7. Run the code – Node PIRLED.js

Code:
var sensorLib = require('node-dht-sensor');
sensorLib.initialize(22, 12);
var interval = setInterval(function () {
read(); }, 2000);
function read() {
var readout = sensorLib.read();
console.log('Temperature: ' +
readout.temperature.toFixed(2) + 'C, ' +
'humidity: ' + readout.humidity.toFixed(2) + '%');

You might also like