PS 7400 prohibited spaceprohibitedspace.org
Doc no.PS-LOG-0003
Date
ClassHack
Reading time3 min read
← Workshop log

Distraction: measuring temperature with Arduino

So at work, we’re evaluating an Intel NUC. The specifications look pretty shiny but some reviewers were mentioning that they ran pretty hot, which might be a problem for us, so I decided to run some tests.

I essentially wanted to know how hot the case got when I tried running a compile on it. Since I don’t have a fancy FLIR gun (they’re sooooper expensive), I decided that the next best thing would be to attach some sensors to it and record the temperature over time.

I have a bunch of Arduinos lying about so I grabbed a spare Uno and ordered some TMP36 sensors which are nice because they provide a voltage that is linear to the measured temperature on the sensor. This means that primary school math is all that’s required to get it working.

arduino uno temperature sensors and stripped cables
arduino uno temperature sensors and stripped cables

I stripped some solid core cable and soldered it to the pins of the sensors, making sure that I had put colour-coded heat shrink tubing on each end; one to seal the pins on the sensor and another to make it easy to recognise what each lead was (V+, GND and output). I then put heat-shrink tubing on the ends to keep it together.

temperature sensor with leads and heat shrink tubing
temperature sensor with leads and heat shrink tubing

Once I had created the sensors, I wrote (read: stole from someone) a quick program to output the temperature.

int sensorPin = 0;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int reading = analogRead(sensorPin);

  float voltage = reading * 5.0;
  voltage /= 1024.0; 

  float temperature = (voltage - 0.5) * 100;

  Serial.println(temperature);

  delay(1000);
}

That worked swell. I tested each of the sensors to make sure I hadn’t stuffed any of them (they are static sensitive) and then extended the first program (in the fastest and stupidest way possible) to read three sensors and output them to serial.

int sensorPin1 = 0;
int sensorPin2 = 1;
int sensorPin3 = 2;

void setup()
{
  Serial.begin(9600);  
}

void loop()                    
{
  int reading1 = analogRead(sensorPin1);
  int reading2 = analogRead(sensorPin2);
  int reading3 = analogRead(sensorPin3);  

  float voltage1 = reading1 * 5.0;
  voltage1 /= 1024.0; 
  float voltage2 = reading2 * 5.0;
  voltage2 /= 1024.0;
  float voltage3 = reading3 * 5.0;
  voltage3 /= 1024.0;

  float temperature1C = (voltage1 - 0.5) * 100 ;
  float temperature2C = (voltage2 - 0.5) * 100;
  float temperature3C = (voltage3 - 0.5) * 100;
  Serial.print("sensor_1:");
  Serial.print(temperature1C); Serial.print(";");
  Serial.print("sensor_2:");
  Serial.print(temperature2C); Serial.print(";");
  Serial.print("sensor_3:");
  Serial.println(temperature3C);

  delay(5000);
}

I then wired up a mini breadboard with the sensors since I didn’t want cables going willy-nilly over the Arduino. I didn’t want to solder it because this was a one-off test and I would most likely want to reuse the sensors.

image of breadboard with sensors and arduino wired up
image of breadboard with sensors and arduino wired up

I then wired the sensors up to the NUC. I placed one on top of the case above the CPU and another in front of the CPU vent. The third I left as an ambient temperature reference away from the case. I secured the sensors with Kapton tape, just in case:

Intel NUC computer with temperature sensors attached
Intel NUC computer with temperature sensors attached

The TMP36 datasheet recommends a cap on the V+ before you hit the sensor to avoid noise but I was not looking for great accuracy. You could see the noise in the graphs but it was minimal for our use case (< 0.5˚C).

I wanted the data to be shown as a graph so I needed timestamps for the data (arduino was no help here). I wrote a quick python script to capture and timestamp the data from the serial port and write it to a CSV file.

import serial
import signal
import sys
import time
import collections
from datetime import datetime

serial_device = '/dev/cu.usbmodem1411'
serial_speed = 9600
log_file = "temperature-{:%Y-%b-%d-%H-%M-%S}.log".format( datetime.now() )

sensor_names = collections.OrderedDict()
sensor_names['sensor_1'] = 'Ambient'
sensor_names['sensor_2'] = 'CPU Exhaust'
sensor_names['sensor_3'] = 'Case (above CPU)'

def signal_handler( signal, frame ):
    print( 'You pressed Ctrl+C!' )
    sys.exit( 0 )

signal.signal( signal.SIGINT, signal_handler )

def main():
    with serial.Serial( serial_device, serial_speed, timeout=1 ) as ser:
        readLoop = True
        print "Connecting to %s at %s. Press Ctrl+C to end logging." % (serial_device, serial_speed)
        with open( log_file, 'w', 0 ) as f:
            f.write( "Timestamp," + ",".join( sensor_names.values() ) )
            while readLoop:
                line = ser.readline()
                if line:
                    sensors = dict( [ e.split( ':' ) for e in line.split( ';' ) ] )
                    output = []
                    output.append("{:%Y-%b-%d-%H-%M-%S}".format( datetime.now() ) )
                    for sensor_name in sensor_names.keys():
                        output.append( sensors[sensor_name] )
                    f.write( ",".join( output ) )

if __name__ == "__main__":
    main()

If I’d actually had more than a sparrow thought about all of this, I would have spent some noggin time on what was going to read this and tailored the output accordingly, but geez I had already spent most of the evening soldering and testing, it was 10:45pm and my wife was going to kill me as it was so cut me some slack!

Anyhoo, I ended up with some data and now I had to decide how to graph it. This wasn’t permanent otherwise I would have sent it to our InfluxDB instance, so I settled on Bokeh. Bokeh runs in python and integrates nicely with Jupyter. I might have to fiddle with it (e.g. annotate the data) so this seemed the best option. This was the python script to generate the graph:

from bokeh.plotting import figure, output_notebook, show
from datetime import datetime
import time
import csv

x = []
ambient = []
cpu_exhaust = []
cpu_case = []

with open('data/baseline-temperature-2016-Oct-07-08-50-36.log', 'rb') as csvfile:
    temps = csv.reader(csvfile, delimiter=',', quotechar='|')
    temps.next() # skip header - should have used the named columns but hey
    for row in temps:
        dt = datetime.fromtimestamp(time.mktime(time.strptime(row[0],"%Y-%b-%d-%H-%M-%S")))
        x.append( dt )
        ambient.append(row[1])
        cpu_exhaust.append(row[2])
        cpu_case.append(row[3])

# output to static HTML file
output_notebook()

# create a new plot with a title and axis labels
p = figure(title="Baseline Temperature",
           x_axis_label='Time',
           x_axis_type="datetime",
           y_axis_label='Temperature deg C',
           plot_width=900)

# add a line renderer with legend and line thickness
p.line(x, ambient, legend="Ambient", line_width=2)
p.line(x, cpu_exhaust, legend="CPU Exhaust", line_width=2, line_color='red')
p.line(x, cpu_case, legend="Case (above CPU)", line_width=2, line_color='green')

# show the results
show(p)

And that gives me a nice graph like this:

graph of baseline temperature data
graph of baseline temperature data

Neat. I then ran lots of builds and annotated the checkout and compile times but that was pretty boring so I’ll stop here. But it was nice to be able to use some of this for work. It was fun.