Sunday, August 24, 2014

Taking the Temperature with the BeagleBone Black and Javascript

Where I live the temperature this weekend is into the triple digits so there is no way we are going outside to do anything which means we had time for another project with the BeagleBone.  With the temperatures being so high, we decided to figure out how to take the temperature with the BeagleboneBlack.  To take the temperature we will use a TMP36GZ temperature sensor and will develop the application to read the sensor in Javascript.

The first thing we need to do is to wire the temperature sensor. Here is the wiring diagram for this project:



You will notice that we connected the center pin of the TMP36GZ sensor to pin P9_40, which is an analog pin that can be used with the analogRead and analogWrite functions.

Once we had the sensor wired, we needed to developed an application to read the sensor and print out the temperature.  Here is the code that I wrote:

var b = require('bonescript');

console.log("Started");
var tempPin = 'P9_40';

b.analogRead(tempPin, printTemp);

function printTemp(aRead) {
    console.log("Geting temp");
    var x = (aRead.value * 1800/1024);
    console.log("value " + aRead.value);
    console.log("x:  " + x);

    var cel = 100*x -50;
    var fah = (cel *9/5)+32;
    console.log("Fah:  " + fah);
    console.log("Cel:  " + cel);
}

We begin by loading in the bonescript module using node.js require function.  Next we set the tempPin variable to the pin connected to the TMP36GZ temperature sensor.  In our case, we connected the sensor to pin 40 of the P9 expansion header.

We set the pin mode of the tempPin to ANALOG_OUTPUT so we can read the pin and then called the analogRead function to read the pin.  In the printTemp function we converted the voltage to a Celsius temperature and then the Celsius temperature to Fahrenheit and printed the temperature to the console.


If you recall from my earlier post (http://myroboticadventure.blogspot.com/2014/06/using-javascript-with-bonescript-to.html) that I was not really a fan of the Cloud 9 IDE that came with the Angstrom image however I had not tried the latest version that came with the Debian image.  Since I have the latest Debian image on my test Beaglebone Black, I decided to give the new Cloud 9 IDE a try.  I do like the latest version a lot more and I think I will with hold my judgment on it until I use it a bit more.  I would recommend that if you did not like Cloud 9 before that you should give it a second chance. 

1 comment:

  1. Hello, I have been attempting this project for a while now and I keep getting an error on the code side of things.
    I would really appreciate it if you get back to me.

    ReplyDelete