This step by step guide to install a temperature&Humidity sensor on your Raspberry Pi (Revision 2 type) is written by Bernat Torres and myself. It was an amazing experience for us!. The prototype build in this hands-on is part of the scientific project of the secondary school in our town. It is an execellent example to show the diverse areas that comprise the current technological scenario. We didn’t found any tutorial that could be followed by a secondary school students in order to build a sensor prototype like this. For this reason Bernat and I think that it will be useful for other students to sumarize our knowledge and share this hands-on.
This tutorial is a taste of various current technologies in a holistic way: Electronics, Hardware, System Software, Applications, Programming Language, Internet of Services, etc. We hope that you enjoy it!
1- Set up your Raspberry Pi
For this hands-on we assume that you have an operative Raspberry Pi (Rev 2 type). If you need to set up your Raspberry Pi we suggest to follow this quick start guide.
The Raspberry Pi is a fully featured computer, and you can do almost anyting with it that you can do with a desktop computer. There are two versions af the Rasberry Pi (we will use a revision 2 type). The Rasberry Pi was made possible in part by the advances in mobile computer chips that have happened in recent years (at its heart is a Broadcom BCM2835 chip that contains an ARM CPU). One example of this is the European research project MONTBLANC coordinated by Barcelona Supercomputing Center (BSC).
2- Exploring electronics
This project offers to you a taste of electronics without soldering. Your teacher will help you in this part.
First of all you need some electronics stuff. In our case it was provided by Jaume, the technology teacher of our secondary school, also involved as a coach in the project. You can purchase them relatively inexpensively from any electronics supplier in your area. The first one is a Breadboard (or Protoboard):
You will need to plug your sensor into breadboards to make the necessary circuits. In our case (as shown in the picture) the pin holes are grouped into segments of 5 linked together, but the segments are not connected to one another.
You also will need wires to plug the sensor into the breadboard . You can buy a package of different coloured (recommended).
We will used one LED to indicate data transfer.
You will need several resistors of different values to protect the LED and to control what voltage the sensors receive from the Rasberry Pi. In our case you will need a 10KΩ and a 600Ω resistors. The electronic color code is used to indicate the values of the transistor.
In order to connect the Pi to your breadboards you can use female-to-male jumper leads.
Either via Adafruit’s spectacular Cobbler or what we used in our first prototype, an old IDE ATA Internal Cable (One of the best researchers at BSC help us providing this brilliant idea). We decided to use a connector that we found at our electronics supplier in our area as shown in the next picture (before and after separate the connectors by hand in order to obtain a male-to-famele jumpers).
Finally you need a Humidity & Temperature sensor. We suggest the DHT22 sensor (datasheet) available from Proto-Pic. You could find other sellers on eBay for less than half the cost, but with longer shipping times. It measures both relative humidity and temperature, however AirPi project (project that we used to start this one) discovered that it does not measure temperatures below 0 degrees Celsius (for this reason, they recommend using the BMP085’s temperature sensor). For us, in Barcelona, the weather is wonderful and … the BMP085 is not necessary! :- )
3- Exploring hardware
This project ofers to you a taste of computer hardware also. The Raspberry Pi was made from a BCM2835 system on chip. The Raspberry Pi has a 26-pin General Purpose Input/Output (GPIO) connector and this carries a set of signals and buses. There are 8 general purpose digital I/O pins. These can be programmed as either digital outputs or inputs. Also there are power supply pins: 3.3V, 5.0Vand 0V (ground / GND). Being aware of Raspberry Pi board revisions, with the GPIO signals going to different pins (we are working in this project with Revision 2).
The drawback with using Male to Female jumpers direct onto the GPIO is that you then have to count down the pins trying to find the pin you need, because nothing is labelled. Matching a diagram of the pinout to the actual pins is not easy and mistakes can be made. In order to solve this problem whe used a “bit of paper” with all the pin labels on obtained from Dr Monk’s DIY Electronics Blog.
First, proceed to connect the 3.3V, and ground outputs from the Raspberry Pi’s GPIO to the rails on the breadboard. We suggest to use different color wire for each connection. In our case we used red (3.3V) and black (0V – GND).
To use the DHT22 sensor you will need a 10KΩ resistor (that has a resistance of 10,000 Ohms).
While holding the sensor facing you (so you can see the grating), the pins are numbered from left to right 1, 2, 3 and 4. Connect them up like this:
- Pin 1 → 3.3V
- Pin 2 → 3.3V via the 10KΩ resistor
- Pin 2 → GPIO #4
- Pin 4 → GND
Warning: Leave Pin 3 unconnected. And Pin 2 is connected to two places, that’s not a typo!.
5- Installing and configure required system software
The Raspberry Pi uses a Linux as a Operating System. We assume that you have some skill using linux.
Linux is a computer operating system assembled under the model of free and open source software development and distribution. Linux was originally developed as a free operating system for Intel x86-based personal computers. It has since been ported to more computer hardware platforms than any other operating system. For example the Android system in wide use on mobile devices is built on the Linux kernel. Also supercomputers as the Marenostrum at Barcelona Supercomputing Center uses Linux.
To use the code we’ve written you need to install several dependencies. We have written detailed instructions on how to download and install them. We created a github project as a fork of AirPi project.
sudo apt-get update sudo apt-get install git-core python-dev python-pip python-smbus
To install the inferfacace with the Pi’s GPIO pins, RPi.GPIO, run this command :
sudo pip install rpi.gpio
6- Enabling I2C ports
Finally we need to Enabling I2C. I2C is a very commonly used standard designed to allow one chip to talk to another. So, since the Raspberry Pi can talk I2C we can connect it to a variety of I2C capable chips and modules. The I2C ports need to be enabled in Raspbian before they can be used. Make sure your Raspberry Pi is connected to the internet when installing the drivers. The new Raspbian distro already have the I2C driver installed but they are disabled by default.
To enable it all you need to do is comment out a line by putting # in front.
sudo nano /etc/modprobe.d/raspi-blacklist.conf
In the file you will see two lines, you need to disable the blacklisting of these by adding a # character before each line:
Press CTRL X then y to save and exit.
Next edit the modules file by:
sudo nano /etc/modules
Use your cursor keys to move to the last line and then add:
Press CTRL X then y to save and exit.
Now install the i2c-tools package by:
sudo apt-get install i2c-tools
If you get a 404 error do an update first:
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade
then run the install of i2c-tools again.
Note : The installation could take a few minutes to do, depend on how busy the server is.
Now add a new user to the i2c group to avoid having to run the I2C tools at root:
sudo adduser pi i2c
Reboot the machine by:
sudo shutdown -r now
After the reboot test to see any device connected by:
sudo i2cdetect -y 1
You should see something like this:
Now you are ready to use the i2c with python.
7- The Program: Download required code
To download the main program for the Sensor project (the code have been adapted from the project AirPi project.), you need to run these commands.
cd ~ git clone https://github.com/jorditorresBCN/temperature-humidity-sensor.git cd temperature-humidity-sensor
The main program is Upload. py written in Python.
Python is a widely used programming language. You will need Python on your computer, but you may not need to download it. Nowadays many Linux distributions include a recent Python. You can check that you already have Python installed by entering python in a command line. You see a response from a Python interpreter including a version number in its initial display. In you want to know more about Python you can start with this Python Quick Start.
8- Upload Data
The code included in this project prints the values to the screen. However, we decided to create a feed on Xively, set it up, and get your sensor listed there.
Internet of Services and Internet of Things
Xively is an on-line database service allowing developers to connect sensor-derived data (e.g. energy and environment data) to the Cloud and to build their own applications based on that data. Data can be pushed from the sensor or device end, with the data feeds then made available in multiple formats. The service allows people to embed real-time graphs & widgets in websites; analyse and process historical data pulled from any public Xively feed; send real-time alerts from any datastream to control scripts, devices & environments. Xively manages millions of datapoints per day that integrates real and virtual worlds and is an exemple of the convergence of the Internet of Services (IoS) with the Internet of Things (IoT). One example of this COMPOSE, an European project that the Barcelona Supercomputing Center participates. COMPOSE will achieve this through the provisioning of an open and scalable marketplace infrastructure, in which objects (like our sensor) are associated to services that can be combined, managed, and integrated in a standardised way to easily and quickly build innovative applications.
To connect the Pi to Xively we need to install python-eeml. Run these commands inside a terminal window.
cd sudo apt-get install libxml2-dev libxslt1-dev python-lxml git clone https://github.com/petervizi/python-eeml.git cd python-eeml sudo python setup.py install
Then, go to Xively and create a new account. Once you’ve done that, add a device, and set the privacy to Public. You should then be taken to a page with your feed ID (a 10 digit number) and API key (a very long string of numbers and letters)
Next, run the following commands to open the Sensors’s configuration file and then edit it:
cd ~/temperature-humidity-sensor sudo nano sensor.cfg
Once you’ve opened it up, add your API key and feed ID to the file.
After finishing editing the file, type
Ctrl-X followed by
Y to save and quit.
9- Run the program
Now, to start the program uploading data to Xively, type the following command into your terminal window:
sudo python Upload.py
Open your a browser and visit the page “https://xively.com/feeds/<yourID>” (<yourID> indicates the 10 digit number of your feed ID). Select the “graph” button to see a graphical view. You can edit these views.
THAT IS ALL! CONGRATULATIONS!
10 – Problems?
Sometimes it is required update the software of your Pi (e.g. in our case we had to do a firmware update). There are two different updates:
rpi-updateis a tool to upgrade the firmware for your Raspberry Pi. In the case of the Raspberry Pi, the firmware will live on the first partition of the SD card. You can executed it using “sudo rpi-update ” command (it is included in the Raspbian distribution) to make sure your devices have the latest firmware.
- Raspbian is an operating system or the core software for your Raspberry Pi. Software (including the OS) lives on second partition of the SD card and is all the stuff that gets executed when you use your device. In order to keep your software up to date you can use the standard Debian software management tools like
sudo apt-get update && sudo apt-get upgrade.
Information Disclaimer: Feel free to use this hands-on for educational purposes. If you detect any error or bug we will be very grateful that you inform us. We would also like to know how you are using this hands-on, so please send us emails with comments or suggestions at firstname.lastname@example.org. This hand-on is a living project that could evolve if necessary.Errors and bugs are most likely contained in this tutorial. We might be responsible for some of them. Using this hands-on you understand and agree that your use it at your own risk and under supervision of your teacher if you are under age.
Last update: 30/12/2013