Home>Posts>I/O Embedded & IoT, I/O HUB>How to Build a BeagleBone Black Temperature Sensor

# How to Build a BeagleBone Black Temperature Sensor

·Categories: I/O Embedded & IoT, I/O HUB·Published On: May 23rd, 2014·3.7 min read·

## Overview

Is it hot in here?
This simple temperature measurement project gets you started with a low cost sensor. A thermistor is a resistor that changes its value based on temperature. These sensors are commonly used in rechargeable battery packs and are very accurate. Temperature range for the sensor is from -50C to 110C. You can record, display, graph and control a variety of heat or cold related devices, starting with weather, fish tank, egg incubator and of course a dog house heater!

The thermistor has a non-linear response, so math is used to obtained the correct reading. This can be a challenging part of using this sensor. This project is a step-by-step guide with the source code provided. How cool is that?

## Instructions

### Step 1

Wire the circuit as shown in the diagram. (Or make a permanent version with a proto cape.)

#### Operation

The sensor circuit is a simple voltage divider. A reference voltage of 1.8V (P9-32) is applied to the thermistor. This voltage is split proportionately between the sensor and a 10K resistor connected to ground (P9-34). The mid point will vary based on temperature, and is measured using the BeagleBone analog input. Increasing temperature reduces the resistance of the sensor. This results in a greater voltage at the input (P9-36).

### Step 2

#### Program Listing

 1234567891011121314151617181920212223242526272829303132333435363738394041 `// OnLogic Inc                ``// Workshop and Tutorials       visit   inspire.logicsupply.com``// Temperature Sensor Demo Program for the BeagleBone Black ARM Cortex A8``// Author      : Richard St-Pierre``// Sensor Type : Semitec 103AT-2 ``// Version     : 0.0   May 22, 2014  Initial creation ``//``// ``var b = require(``'bonescript'``);``var adcPin   = ``"P9_36"``;         ``// AIN5 max 1.8V``//var adcPin = "P9_39";         //AIN0``//var adcPin = "P9_40";         //AIN1``//var adcPin = "P9_37";         //AIN2``//var adcPin = "P9_38";         //AIN3``//var adcPin = "P9_33";         //AIN4``//var adcPin = "P9_36";         //AIN5``//var adcPin = "P9_35";         //AIN6`` ``var adcValue  = 0.00;           ``// Analog input  (0-100)    ``var r_bias    = 10000;          ``// Bias resistor (to GND)``var r_therm   = 0;              ``// Calculated thermistor resistance (to Vref)``var t_c       = 0;              ``// Temperature (Celsius)``var t_f       = 0;              ``// Temperature (Fahrenheit)``var r_alpha   = 0.09919;        ``// Ro * e^(-B/To)  in K, To= 298K``var c2kelvin  = 273.15;         ``// Celsius to Kelvin offset``var Bvalue    = 3435;           ``// from thermistor datasheet``loop();``function loop(){``//adcValue = Math.round(b.analogRead(adcPin)* 100);  // scale from 0-100%``adcValue = b.analogRead(adcPin);``r_therm  = Math.round(((1/adcValue)-1)*r_bias);``t_c      = Math.round(10*((Bvalue/(Math.log(r_therm/r_alpha)))-c2kelvin))/10;``t_f      = (t_c * 9/5) +32;``console.log(``'ADC = %d, resistance = %d, tempC = %d, tempF = %d'``,adcValue,r_therm,t_c, t_f);``setTimeout(loop, 500);``}`

#### Program Overview

First we request to use the bonescript library. Second we declare the pin we want to use AIN5 (P9_36).

Next we declare some variables we will need to calculate the temperature.
“Ralpha” is calculated using a reference data point for the thermistor (from the datasheet).
“B” is the material constant for the thermistor (from datasheet).
“Ro”  is the resistance of the thermistor (in ohms) at temperature To.
“To” is the temperature which corresponds to the value of Ro (typically 25C).
25C equals 298 degrees Kelvin.
The equation uses the exponential function (e^x) with the natural base “e”.

As part of the measurement, we need to convert the voltage we read on the analog pin AIN5 to a resistance value. Inside the function loop(), we first read the voltage on AIN5. From this voltage reading we can now calculate the corresponding resistance value. To do this, we take the voltage across the bias resistor (10 kilohms) and derive the current flowing in this resistor (I = V/R). Knowing the current flowing in the circuit, we can calculate the resistance of the thermistor (R=V/I). Where V = Vref – Vmeasured, the voltage present across the thermistor. Finally we can calculate the temperature from the resistance value of the thermistor.

Note:  Math.log() computes the natural logarithm and all temperature values are in Kelvin (Kelvin = C + 273.15). We use the Math.round() to manage the number of digits being displayed. The result from the equation is in Kelvin, so we subtract 273.15 to obtain a Celsius value. To make everyone happy, a conversion to Fahrenheit. Finally we display all our results to the console.

The program will call loop continuously.

## Example Display Output ### About the Author: Richard St-Pierre Richard worked as the Senior Electrical Engineer at Logic Supply before relocating with his family in 2015. A general problem solver and well-known "Lab MacGyver", Richard is always on the lookout for creative approaches to design solutions and continues to explore new opportunities to build creative solutions to common challenges.

SHARE

EDITORS’ PICKS

 01 Installing Fanless Computers: Important Dos and Don'tsApr 14 - 5 min 02 Industrial Raspberry Pi: A Brief History and the Near FutureMarch 31 - 4 min 03 Computer Vision for Improved Roadside MaintenanceMarch 30 - 3 min

Have a project? Let's talk

WHITEPAPER