logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

DIY Raspberry Pi Thermometer

Device Plus Editorial Team
Published by Device Plus Editorial Team at March 1, 2016
Categories
  • Raspberry Pi
Tags
Breadboard, Thermometer sensor, resistance

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!

Breadboard, Thermometer sensor, resistance, jumper wires

Picture 1

  • Breadboard
  • Temperature sensor (DS18B20)
  • Electrical resistor
  • 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.

pin configurations

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!

Wiring Diagram

Figure 2

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.

1
nano/etc/modules

/etc/modules

1
2
w1-gpio
w1-therm

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.

1
nano /boot/config.txt

/boot/config.txt

1
dtoverlay=w1-gpio-pullup,gpiopin=4

That’s all for the preparation! Let’s reboot to get the configuration updated.

1
reboot

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.

raspberrypi18_img03

Figure 3

There is a shortcut folder named 28-000006470bec!
This file name is the Device ID of the temperature sensor.

raspberrypi18_img04

Figure 4

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.

1
2
8c 01 4b 46 7f ff 04 10 2e : crc=2e YES
8c 01 4b 46 7f ff 04 10 2e t=24750

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.

1
cat /sys/bus/w1/devices/28-000006470bec/w1_slave

raspberrypi18_img05

Figure 5

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.

/var/www/temperature.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$deviceId = '28-000006470bec';
$today = date("Ymd");
$time = date("H:i");
$sensor_path = '/sys/bus/w1/devices/'.$deviceId.'/w1_slave';
$csv_dir = '/var/www/temperature/';
$csv_file = $today.'.csv';
$t = null;
//Get temperature
exec("cat ".$sensor_path, $w1_slave);
if(isset($w1_slave[1])){
$tmp = explode('t=', $w1_slave[1]);
if(isset($tmp[1])) $t = $tmp[1] / 1000;
}
//write CSV
if(!file_exists($csv_dir)){
mkdir($csv_dir);
}
if( $handle = fopen( $csv_dir.$csv_file , 'a' ) ){
fputcsv( $handle, Array("'".$time."'", $t) );
fclose($handle);
}
return;

I created a program that gets the temperature using the cat command and saves it as a CSV file for each date. Please replace the $deviceId value with the one from your temperature sensor.

You should install php5 and apache2 packages in advance when using PHP like in the above program. I talked about the installation procedure in “Raspberry Radio Part 1 – Internet Radio Receiver and Remote Access“.

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.

CRON AND CRONTAB – Raspberry Pi Documentation
Documentation is also published on the Raspberry Pi official website.

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.

1
2
3
4
5
6
7
8
# (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.

1
crontab -e

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.

1
0-59/10 * * * * php /var/www/temperature.php

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!

Line Chart – Google Charts

Using the sample source as a foundation, I attempted to customize it so that it can display the temperature data read from the CSV file.

/var/www/index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
$today = date("Ymd");
$csv_dir = '/var/www/temperature/';
$csv_file = $today.'.csv';
$grapgh = '';
 
//Reading CSV
if (($handle = fopen($csv_dir.$csv_file, "r")) !== false) {
while (($line = fgets($handle)) !== false) {
$grapgh .= '['.rtrim($line).'],'.PHP_EOL;
}
fclose($handle);
}else{
echo 'no data';
}
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['line']});
google.setOnLoadCallback(drawChart);
 
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Time');
data.addColumn('number');
data.addRows([
 
<?php echo $grapgh; ?>
 
]);
 
var options = {
chart: {
title: 'Raspberry Pi Thermometer',
},
height: '500'
};
var chart = new google.charts.Line(document.getElementById('linechart_material'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="linechart_material"></div>
</body>
</html>

Here is the graph that was exported!

Google Charts

Figure 6

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.

Summary

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!

By the way, we introduced a project using a temperature sensor based on Arduino in a previous article Make a Stevenson Screen with Arduino Part 1. Arduino users should make sure to check it!

This time, I learned how to use cron. Next time, I would like to put this feature into practice by making an alarm clock.

Breadboard with stop button

Here are the main components! I attached this tactile switch as a “stop button” to stop the alarm!

Device Plus Editorial Team
Device Plus Editorial Team
Device Plus is for everyone who loves electronics and mechatronics.

Check us out on Social Media

  • Facebook
  • X

Recommended Posts

  • What is the difference between Raspberry Pi and Arduino?What is the difference between Raspberry Pi and Arduino?
  • Raspberry Pi DIY Case & Overview the Preinstalled SoftwaresRaspberry Pi DIY Case & Overview the Preinstalled Softwares
  • Using Crystal Signal Pi, Part 3 – Setup a Caution Light Solution made with Raspberry Pi – Creating ToolsUsing Crystal Signal Pi, Part 3 – Setup a Caution Light Solution made with Raspberry Pi – Creating Tools
  • Using Raspberry Pi GPIO Pins With the RPi.GPIO Python LibraryUsing Raspberry Pi GPIO Pins With the RPi.GPIO Python Library
  • DIY Raspberry Pi Drone: Mechanics – Part 1 (Cont.)DIY Raspberry Pi Drone: Mechanics – Part 1 (Cont.)
  • Making the GPIO Easier With GPIO ZeroMaking the GPIO Easier With GPIO Zero
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • X
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort