Raspberry Pi Temperature Logger

186K19511

Intro: Raspberry Pi Temperature Logger

Here are the instructions to build a simple temperature logger using a $5.00 I2C temperature sensor. The data is stored to the SD card and can be easily imported into excel. Simply by changing or adding other sensors other types of data can also be collected.

The following components are used for this project:
Raspberry Pi(single board computer)
Temperature Sensor (SF-SEN-11931)

Solderless Breadboard
Hookup Wires

The Zagros Raspberry Pi 2 starter kit also includes everything needed for this project except the temperature sensor!

STEP 1: Wire Up the Temperature Sensor

First, solder header pins or lead wires on to the sensor. We used headers so it would be could simply plug the sensor into a breadboard.

Make the following connections to the Raspberry Pi GPIO port. The ADD0 connection to ground determines the device I2C address. More than one sensor can be connected to the I2C bus, but they must each have a unique address.



Sensor RPi GPIO
VCC +3.3V

SDA SDA

SCL SCL

GND GND

ADD0 GND (note this sets the I2C device address)

ALT N/C


NOTE: DO NOT CONNECT THE SENSOR VCC TO +5VDC, THIS WILL DAMAGE THE SENSOR

STEP 2: Enable the I2C Bus

*** Newer version may not require this step. If the file doesn't exist, move on to the next step.

First the I2C bus must be enabled.

There are two ways to enable the I2C bus.


The first and easiest is to do it with the raspi-config utility.

Use the command sudo raspi-config to start the utility.

Select Advanced Option to enabled the bus.

The second, but more complex way is to do it manually.

To do this, edit the configuration file /etc/modprobe.d/raspi-blacklist.conf

Use the command:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Now change the file contents from:

# blacklist spi and i2c by default (many users don't need them)

blacklist spi-bcm2708
blacklist i2c-bcm2708




To this:

# blacklist spi and i2c by default (many users don't need them)

blacklist spi-bcm2708
#blacklist i2c-bcm2708


STEP 3: Update the Config.txt

Run the following command to update the config.txt

sudo nano /boot/config.txt

Add the following lines to the file:

dtparam=i2c1=on

dtparam=i2c_arm=on

STEP 4: Set I2C Module to Load at Boot

The I2C module should be set to load when the Raspberry Pi starts up. Do this by editing the /etc/modules file.

The following command can be used to edit this file:

sudo nano /etc/modules

Add the following line to the end of the file:

i2c-bcm2708
i2c-dev



STEP 5: Install I2C Packages

Install the i2c-tools and python-smbus packages to complete the I2C setup:

The following commands can be used to install the packages:

sudo apt-get install i2c-tools

sudo apt-get install python-smbus


Finally add the pi user (or whatever login required) to the I2C access group.

The following command can be used to accomplish this:

sudo adduser pi i2c

Use the following command to see what devices are connected to the I2C bus:

i2cdetect -y 1


Note: use the following command if you are using a model A Raspberry Pi

i2cdetect -y0

The I2C address (in hexadecimal) of the temperature sensor should show up if properly connected.



STEP 6: Datalogger Program

Load and run the example program using the following command:

python temp_logger.py


The example program is very simple:

It reads the temperature from the temperature sensor every 60 seconds and logs it to a text file (Tempdata.txt)

STEP 7: Viewing Data

Use the following command to view the raw data file:

nano tempdata.txt

Copy the data to a USB drive and it can easily be imported in to Excel:

STEP 8: Background Logging

To run the logger in the background (it will keep running after you log out). Use the following command:

sudo python temp_logger.py & ( Relative Path)
There are situations whereby the command above might give an error, such as Python: can't open file 'temp_logger.py': [Errno 2] no such file or directory

This simply means you will have to use an absolute path, which means you have to specify the location of the file from the root directory. An easy way to do this is to right-click on your temp_logger.py file, copy the path and paste in your terminal and then type "python" in front of it.

This is how my command looks;
python /home/pi/Desktop/temp_logger.py

11 Comments

Don't forget to install the python-smbus python module:

sudo apt-get install python-smbus

without that I got this error on Raspian-jessie

pi@raspberrypi:~/tempsens $ python temp_logger.py
Traceback (most recent call last):
File "temp_logger.py", line 6, in <module>
import smbus
ImportError: No module named smbus

Hi RobZ314. I'm very green in the Pi business... :-) I'd like to have my Pi bootup and autorun the program without any user intervention. Ie. I'd like to apply only power and have the program run by itself logging the results for some time. Reason for this is that I need the unit to record temperature in a location far from keyboards, monitors, etc.

Is this possible, or should I make a plan to carry the peripherals with?

Thank you for your time.

Regards

I like it. Thanks. I ran into a few different troubles and think I found the solutions Has anyone else had an issue with i2cdetect finding the file? I added these steps and it started working afterwards:

If you are running a recent Raspberry Pi (3.18 kernel or higher) you will also need to update the/boot/config.txt file. Edit it with sudo nano /boot/config.txt and add the text

dtparam=i2c1=on
dtparam=i2c_arm=on



and:




  1. sudo nano /etc/modules

and add these two lines to the end of the file:

  1. i2c-bcm2708
  2. i2c-dev

I am still getting the error if I do that...do you know what else I can do

Hi. I am hoping to use the ultimate starter kit to construct an continuous data logging system for pH and temperature. I am an beginner beginner on building raspberry pis and programming!!!

Works great! But I want to connect TWO of these sensors. How do I change the address of the second one?

excuse me sir if i used thermocouple type k ,can it work using your method?? please reply as soon as possible.thx :D

How to set time, date and timezone in raspberry pi without RTC.

date in my raspberry pi is default, not date now

Thanks a very awesome tutorial, ZRob314
Could you show us how to write your logger record to online SQL database like in Windows Azure's Management Portal?

Hey i am also in Toronto. Very cool projects you have. Would you be i nterested In a paid project? Need something done right away.