Raspberry Pi 2, Node.js, Ultrasonic Sensor
Sunday, December 6, 2015
Raspberry Pi 2, Node.js, Ultrasonic Sensor
Note: This Post is based on http://thejackalofjavascript.com/rpi-an-ultrasonic-sensor-measure-distances/ The post here in this link is based on Old Raspberry Pi. which does not work well with Raspberry Pi 2.
Here is the solution:
As you can see from the above diagram, the HC-SR04 sensor has 4 pins.
As you can see from the above diagram, we will connect the VCC pin of the sensor to +5v and GND to GND. We will connect the Trigger pin to GPIO pin 17 and finally we will connect a 1K Ohm resistor between the Echo pin and GPIO pin 18.
The Echo pin outputs a 5V output, which will damage the GPIO pin, if interfaced directly. This is why we connect a 1K Ohm resistor between the Echo and GPIO pin 18.
First we will initialize a new node project here. Run
Now we will install a node module named 'r-pi-usonic' to interact with the Ultrasonic sensor. Run
Now we will install a node module named 'readline'. Run
Now we will install a node module named 'math-statistics' to interact with the Ultrasonic sensor. Run
Next, we will create a new file named sense.js. And we will open the same in the nano editor. Run
'use strict';
var readline = require('readline');
var statistics = require('math-statistics');
var usonic = require('./node_modules/r-pi-usonic/lib/usonic.js');
var print = function (distances) {
var distance = statistics.median(distances);
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (distance < 0) {
process.stdout.write('Error: Measurement timeout.\n');
} else {
process.stdout.write('Distance: ' + distance.toFixed(2) + ' cm');
}
};
var initSensor = function (config) {
var sensor = usonic.createSensor(config.echoPin, config.triggerPin, config.timeout);
console.log('Config: ' + JSON.stringify(config));
var distances;
(function measure() {
if (!distances || distances.length === config.rate) {
if (distances) {
print(distances);
}
distances = [];
}
setTimeout(function () {
distances.push(sensor());
measure();
}, config.delay);
}());
};
Run this program now:
sudo node surveyor.js
Here is the solution:
Components needed
- 1 – Raspberry pi B+
- 1 – Breadboard
- 1 – 1K Ohm resistor
- 1 – HC-SR04 Ultrasonic sensor
- 4 – Jumper wires
Understanding HC-SR04 Ultrasonic Sensor
HC-SR04 sensor works with a power of 5v. It has a range of approximately 2cm to 4m. It outputs a frequency of 40khz, twice the human ear can hear. (more info on the sensor here)As you can see from the above diagram, the HC-SR04 sensor has 4 pins.
- VCC : +5v power supply
- Trigger : A high input to this pin for a duration of 10 micro seconds, activates the sensor
- Echo : This pin returns the time taken for the transmitted signal to come back after hitting an object.
- Ground : Connected to the ground
Setup HC-SR04 Ultrasonic Sensor
As you can see from the above diagram, we will connect the VCC pin of the sensor to +5v and GND to GND. We will connect the Trigger pin to GPIO pin 17 and finally we will connect a 1K Ohm resistor between the Echo pin and GPIO pin 18.
The Echo pin outputs a 5V output, which will damage the GPIO pin, if interfaced directly. This is why we connect a 1K Ohm resistor between the Echo and GPIO pin 18.
Node.js Program
Login to your pi via ssh – terminal/putty. As soon as you ssh into pi, you will be landing inside the /home/pi folder. We will create a new folder here named node_programs. And inside this folder, we will be maintaining all our programs. Run
mkdir node_programs
To step inside that folder, run
cd node_programs
For this post, we will create a new folder named ultraSonicSensor and will step inside this folder. Run
mkdir ultraSonicSensor && cd ultraSonicSensor
Note : You can run multiple commands separated by a &&.First we will initialize a new node project here. Run
npm init
Fill it up as applicable.Now we will install a node module named 'r-pi-usonic' to interact with the Ultrasonic sensor. Run
npm install r-pi-usonic --save
npm install readline --save
Now we will install a node module named 'math-statistics' to interact with the Ultrasonic sensor. Run
npm install math-statistics --save
Next, we will create a new file named sense.js. And we will open the same in the nano editor. Run
nano sense.js
Paste the below code into the nano editor (Filename: /home/pi/node_programs/ultraSonicSensor/sense.js)'use strict';
var readline = require('readline');
var statistics = require('math-statistics');
var usonic = require('./node_modules/r-pi-usonic/lib/usonic.js');
var print = function (distances) {
var distance = statistics.median(distances);
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (distance < 0) {
process.stdout.write('Error: Measurement timeout.\n');
} else {
process.stdout.write('Distance: ' + distance.toFixed(2) + ' cm');
}
};
var initSensor = function (config) {
var sensor = usonic.createSensor(config.echoPin, config.triggerPin, config.timeout);
console.log('Config: ' + JSON.stringify(config));
var distances;
(function measure() {
if (!distances || distances.length === config.rate) {
if (distances) {
print(distances);
}
distances = [];
}
setTimeout(function () {
distances.push(sensor());
measure();
}, config.delay);
}());
};
Run this program now:
sudo node surveyor.js
Subscribe to:
Posts (Atom)