Simple BealgeBone temperature logger

I’ve decided to make a very simple temperature logger using the BeagleBone black and a readily available and Texas Instrument’s LM74 temperature sensor. The sensor is not the most precise one out there but it is easy to interface to and costs under $2. LM74 communicates over SPI, a popular and well supported interface. To hook up the sensor only 5 wires are needed:

BeagleBone Black to LM74

BB signal

BB Header_Pin

SPI signal

LM74 signal

LM74 pin

SPI0_d1

P9_18

Master Data Out

N/A

N/A

SPI0_d0

P9_21

Master Data In

SI/O

1

SPI0_SCLK

P9_22

Clock

SC

2

SPI0_CS

P9_17

Chip Select

CS

7

GND

P9_1

N/A

GND

4

3.3V

P9_3

N/A

V+

8

Since we don’t care about sending data to the sensor, only reading data back, the SPI Data Input line of the beagle bone is left unconnected.

Here is a picture of my setup. I’ve put two temperature sensor on the same perf-board just because I had space and to do some more testing later.According to the datasheet all we need to do to get data out of this guy is to power it with 3 to 5.5v volts, set the chip select low and send 16 clock pulses (2 bytes). LM74 will transmit 16bits of data (only 13 of which actually contain useful info but more on that later)

I’ve chosen to write my data logging application in Python since there are tons of libraries and support available for it.

Overall goal is to read the SPI data, convert it to a meaningful temperature value and write to a file along with a time stamp.

At initialization Adafruit’s SPI library automatically loads the “ADAFRUIT-SPI0” cape (the library needs to be installed beforehand). “main” function first configures logging and then enters a forever loop that reads the LM74 data and writes it to a file. Python’s logging library is very useful, alternatively I could have written the data to a file of my choosing manually. Benefits of using is that it creates the log file for you if it doesn’t already exist, adds the time stamp and there are ways of limiting the size of the file (not implemented in this code).

Reading the SPI data is done in the “spi_read_LM74” function. First we transmit two dummy bytes [0,0] just to generate 16 clock pulses. As prevsiously mentioned the Master Out Slave In (MOSI) line is not even connected. At the same time the slave (LM74) transmits its 16 bits of data back to the beagle bone. Note that the Chip Select (CS) line must stay low for the entire 16bit transmission, that’s why spi.xfer2 function is used. This data is captured in the “read_val_list” variable as a list of two bytes. read_val_list[0] is the first byte received and contains the higher 8 bits, read_val_list[1] contains the lower 8 bits. Per LM74’s datasheet bits 15:3 contain temperature data encoded as two’s compliment value with one LSB = 0.0625 °C. So we first concatenate the two 8 bit values, then bit-wise shift to the right and mask with 0x1FFF. At this point we have the 13 bits of data but since it is in two’s complement format we need to “decode it” using the “twos_comp” function. The function returns a signed integer which is the temperature data we need just scaled by 0.0625. Last step is to multiply by this value and the result is a floating point decimal value in °C

Logging function takes care of adding this data to a file and the process starts all over again. The resulting temp.log is a simple text file that contains date&time “;” temperature and looks something like: