Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Programming the BeagleBone

You're reading from  Programming the BeagleBone

Product type Book
Published in Jan 2016
Publisher
ISBN-13 9781784390013
Pages 180 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

Programming the BeagleBone
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Cloud9 IDE Blinking Onboard LEDs Blinking External LEDs Controlling LED Using a Push Button Reading from Analog Sensors PWM – Writing Analog Information Internet of Things with BeagleBone Physical Computing in Python UART, I2C, and SPI Programming Internet of Things using Python GPIO Control in Bash BeagleBone Capes
Pinmux and the Device Tree Index

Program to print temperature


Write the following code in Cloud9 and save it as tmp36.js. Run the program and check the printed temperature. Then touch the plastic body of the sensor for a few seconds (avoid touching metal pins). You should see an increase in temperature. The code for tmp36.js is as follows:

var b = require('bonescript');
var loopTimer = setInterval(readVoltageLoop, loopTime);
var loopTime = 2000;
var TMP36 = 'P9_40';

function readVoltageLoop()
{
  b.analogRead(TMP36, printTemperature);
}

function printTemperature(pinObj)
{
  var volt = pinObj.value * 1.8;
  var temperature = (100 * volt) - 50;
  console.log("Voltage at input pin = " + volt.toPrecision(3) + " Temperature in Celsius =" + temperature.toPrecision(3) );
}

Explanation

We want to print the temperature after every 2 seconds. So, we created a timer using setInterval(). This timer will call the function readVoltageLoop() after every 2 seconds. The function readVoltageLoop() calls analogRead() on pin P9_40. The first...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}