In this new project, we will use a temperature sensor.
Even though it uses fewer components, and the circuit is straightforward, electronic projects never progress simply! This time, I also struggled quite a lot.
I challenged myself not just with the temperature acquisition, but with also with the automatic processing using cron and graph display in Google Charts. Will it turn into an easy to use thermometer, the third tool to make the office life more convenient!?
About the Temperature Sensor
Here the components we will use this time!
Temperature sensor (DS18B20)
Jumper wires (male-female) x3
This time we will use a DS18B20 temperature sensor. The DS18B20 is a component that uses an 1-Wire interface. As an 1-Wire module is already installed on the Raspberry Pi, we can use it immediately by just enabling it in the setting!
1-Wire – Wikipedia 1-Wire is a device communication bus system designed by Dallas Semiconductor Corp. that provides low-speed data, signaling, and power over a single signal.
Three pins are attached to the DS18B20 black body. I used the datasheet published on the manufacturer’s website as a reference for the direction and the pins function.
Figure 1 Pin configurations
This is the drawing published on the first page of the datasheet.
It’s written in small letters, but it is the BOTTOM VIEW. The circuit diagrams I am used to see always show the top view, so I completely misinterpreted it! If power is applied to the circuit with the sensor wrongly wired, it will become extremely hot. Be careful about the wiring!
The wiring diagram is as figure 2 (this figure is as seen from the top)
The power is connected to the VDD pin on the left (the green jumper wire). GPIO4, the 1-Wire port, is connected to the DQ pin (the blue jumper wire). Then, these two pins are connected to each other with a “pull-up resistor”.
About the “pull-up resistor”, I was at a loss even after looking it up, so I had my boss explain it to me!
Put simply, it seems that its purpose is “to prevent the input (the DQ pin) from becoming unstable”. It seems that by connecting the pin to a pull-up resistor, the circuit can be stabilized because there will always be an amount of current flowing through it.
Preparing the Raspberry Pi
We need to enable the 1-Wire in order to use the DS18B20.
First, we need to configure the /etc/modules file. By logging in as the root user and directly editing the file or by using the nano command to open the file from the LX Terminal, we will add the below two lines.
One more line is necessary when using Raspbian 2015-01-31 (NOOBS 1.3.12) or newer operating systems. The configuration process is the same as the previous one, and everything is OK just by adding this one line to the /boot/config.txt file.
That’s all for the preparation! Let’s reboot to get the configuration updated.
When 1-Wire is enabled and the temperature sensor is successfully recognized, a file will be created in /sys/bus/w1/devices. We can confirm this visually by actually opening the folder.
There is a shortcut folder named 28-000006470bec!
This file name is the Device ID of the temperature sensor.
Here are the contents of the folder! The temperature information is stored in the file called w1_slave. The file can be opened directly with a text editor. This is what I used to check the data.
It appeared like this!
Regarding the code-like character string, the temperature is the “t” value, and is 24750 in the above data. The value is multiplied by 1000, so this means it is 24.750℃. The way it works is by acquiring the temperature every time the file is read, so the temperature information will be updated even when directly opening the file.
When acquiring the temperature data from the command line, the simplest method is to directly export the contents of the file with the cat command.
By the way, if the temperature sensor was not properly recognized, the the folder will be empty, and there will be no “w1_slave” file. If that happens, you should check again the wiring and the configuration settings.
Getting the Temperature Automatically
Next, we challenge ourselves with by getting the temperature automatically!
The groundwork comes first. Let’s prepare the program that will get and save the temperature data.
After preparing the program, the next step is the configuration to get the temperature automatically.
For running it automatically at periodic intervals, I will use the cron feature.
Crontab – Wikipedia
Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system wide crontab file (usually in /etc or a subdirectory of /etc) that only system administrators can edit.
The way to configure cron is to just enter the command as a single line in the crontab file. The format of the command is introduced in both of the above websites. But Wikipedia, with many examples posted, is easier to understand.
# （The # at the top of the line indicates a comment line)
# +------------ minute (0 - 59)
# | +---------- hour (0 - 23)
# | | +-------- day of month (1 - 31)
# | | | +------ month (1 - 12)
# | | | | +---- day of week (0 - 6) (Sunday=0)
# | | | | |
# * * * * *The command to be run
Enter the command you want to run like this, by specifying the time it should be run using these five headings: minute, hour, day of month, month, and day of week.
In addition to these fixed values, special symbols have also been provided. These are useful when you want to perform repeated processing.
Comma (,): Specifies a list of values. Example: “1,3,4,7,8”
Dash (-): Specifies the range of the value: Example: “1-6” (This is the same as specifying “1,2,3,4,5,6”)
Asterisk (*): Represents all of the values that can be interpreted under that field. For example, in the hour field this means “every hour”.
Now, let’s actually perform the configuration. To enter the command into the crontab file, we run the command below.
The nano editor opens and the content of the crontab file is displayed. Lines that start with # are comment lines. Following the crontab format, we add the command below.
I configured it to run the temperature acquisition program every 10 minutes. “0-59/10” says the same thing as entering “0,10,20,30,40,50”. By putting them into practice like this, we can see the convenience of the special symbols!
If the command is entered properly, the program will be executed at the specified timing. It is not necessary to reboot. In the beginning, I recommend that you enter a command that executes the program at short intervals to check if it is working.
Making a Graph with Google Charts
Several packages can draw a graph, but I wanted to be able to easily customize it. So this time, I used Google Charts.
Google Charts Chart Gallery – Google Charts
You can check a list of the charts here. Besides the bar charts and the pie charts, it even supports a geographic chart that uses maps. There are a lot of sample sources provided, so you can easily display a graph.
To display the temperature, the standard is the line graph! This is the one we will use this time!
The Figure 6 is what we got after running the program. When the temperature starts to rise at 11:30 am, it is around the time when the room started to warm up due to the heat of my computer. This graph shows us what was the temperature of the office during that day. It would be interesting to run it in a season with large temperature amplitude; it would create a graph that would swing greatly!
As I created this file in the Apache public directory, it can be accessed from other terminals as well. Observibg the emperature of other rooms can be done easily.
The DIY Raspberry Pi thermometer is complete!
It looks as if it will work great for observing the temperature in rooms where people are not always present, such as meeting rooms. It also seems like it will be useful in the summer time as a tool to check whether the air conditioner has been left on! It would be more useful if we added a feature for notifying us depending on the temperature, such as lighting up an LED or making a noise!