These sensors are pretty cool--they are cheap to buy and surprisingly sensitive to small changes in temperature. Here’s a first attempt I made to interact with the sensors in the Wolfram Language.
For this setup I used DS18B20 temperature sensors and hooked them up to the Raspberry Pi breadboard according to Adafruit’s setup guide. The board should look like the following diagram (make sure the sensor is hooked up to a 3.3V pin--not a 5V pin or you could fry the sensor):
Once hooked up and connected to your Pi, run the following commands in the terminal:
sudo modprobe w1-gpio
sudo modprobe w1-therm
The temperatures are read from the sensor by “reading” the file that’s created in the devices directory. You can locate the file with the following commands:
cd /sys/bus/w1/devices
ls
This will show you the contents of your devices folder, where there should be a file titled 28-xxxx, where the xxxx is the serial number unique to your sensor. Once you’ve got that number, enter:
cd 28-xxxx (the xxxx should be replaced with the serial number unique to your sensor)
cat w1_slave
Two lines of data should return back to you--if the first line ends with “YES” then the 5-digit number at the end of the second line is the temperature, to be read as xx.xxx degrees Celsius.
And now that we know that the temperature sensor is working, and we know how to find it, we can copy the file path and import it using the Wolfram Language.
Import["/sys/bus/w1/devices/28-000004fe0343/w1_slave"]
Since this still returns a really long string of data that we don’t need, we can single out the temperature and then convert the string into a computable expression.
temp:=N[ToExpression[StringTake[Import["/sys/bus/w1/devices/28-000004fe0343/w1_slave"],-5]]/1000]
So now when we read the file, we just get the temperature back!
temp
(*22.312*)
For kicks, I set up a scheduled task to plot the ambient temperature of my office every 60 seconds for 6 hours. Unsurprisingly, the temperature only fluctuated a few tenths of a degree…!
t={}
RunScheduledTask[(deg=temp;AppendTo[t,deg]),{60,360}];
Dynamic[ListLinePlot[t,Joined->True,PlotRange->Automatic]]
And here’s what the graph looked like after a little bit of time--it truly is a sensitive device (the “large” dip down to 22.1 was me touching the sensor with my cold hands!):
Any suggestions for what to do next?




