Temperature, Bright & Humidity Sensors with Raspberry Pi

Temperature Sensor IRP102

Temperature, Bright & Humidity Sensors with Raspberry Pi

16 February, 2016 DIY, Raspberry Pi 0

In this tutorial we explain how to give the Raspberry PI sensor indoor and outdoor temperature, humidity, brightness and air quality. With the brightness sensor we will achieve a brightness control automatic display, depending on ambient light.
We can easily make our Raspberry in a weather station that also includes the next sunrise and sunset time and the lunar calendar.

Hardware requirements:

  • IRP-102 HAT Board
  • Temperature and humidity sensor SHT21 I2C. Alternatively, you can use a 1-wire temperature sensor. If you connect 1-wire temperature sensor with temperature/humidity I2C sensor, the 1-wire sensor works like outdoor temperature in weather station setup
  • Brightness sensor LDR (photoresistor) connected to , or failing YL-40 module I2C
  • Air quality sensor MQ130
  • 4 cables
  • SPI Display, in our case 320x240px TFT Color

As an alternative to SHT21 device incorporating humidity and temperature sensor, it can be used a 1-wire temperature probe. Source code developed in this tutorial look for a 1-wire probe should not detect the SHT21.

Referring to the brightness sensor, we used an LDR photocell whose cost is very low compared to the other option available: YL-40 module that provides one of these photocells but with I2C interface. In the case of LDR, this must be connected to an input of ADC. As in this tutorial we are using our “HAT” IRP102, the ADC has a resistive divider available 1/10. This implies that the values read from the ADC will be 10 times less than the output of the connected component. In this case, a proper sensitivity of the photocell is obtained, so that the values provided are valid applying this divisor.

The application we have developed is for Python so it is necessary to install it from the repository. This tutorial has been made with Raspbian operating system. As with any installation to get the latest versions of the packages you want to install will first run the following steps:

And if you want to perform an upgrade of already installed packages you run:

If you have not yet installed you should run Python (Python 2 version):

Not all Python packages are available in the Raspbian archives, and those that are can sometimes be out of date, for this, we have install PIP (for Python 2 version):

Install the Ephem library that we will use to display information about sunrise, sunset and lunar calendar. This is a very powerful library that can be used for many more things than those used in this tutorial. For this, run:

Now activate the I2C port to be used by the sensors. The steps for doing this are described in the post ‘Configure RTC (Real Time Clock) on Raspberry PI‘.
Follow steps 2 and 3. For this tutorial, step 4 differs slightly from what we described below:

4. Module loading at startup

Edit the file rc.local

and add the following lines just before the exit 0

If the project we are carrying out with a probe 1-wire we further add the following lines in this file:

Get out the editor saving changes and restart system using.

ONLY if you’re using the YL-40 module (Brightness sensor), now we install the controller to manage it from repositories:

If success, when we try again

we should get:

UU characters means that device at this position is in use.

5. SPI Display installation to showing sensor data

To operate the display for the first time you can follow the tutorial Using 2.2″ LCD SPI in Python with Raspberry Pi

6. Download source code

Once we have everything working: Python for the application, I2C for sensors and SPI for display, we have just download the source code used to have available at the following link:
or just

from shell.

7. Application modules description

gy21.py: Module for reading I2C sensors (temperature and humidity).

Function to get humidity value:

Function to get temperature value:

adc.py: Module for reading ADC channels. On channel 0 we read the LDR brightness sensor.

Function to get LDR brightness value:

weather_station.py: Main application.

It is important to remember that you can use the display and sensors must be imported modules:

To display data sunrise, sunset and lunar calendar must be added the following modules:

We also note the available option in the source code to activate the brightness sensor according to the device that we will use. It is defined in the following lines to import
The default is set to use a LDR:

When using the YL-40 module should change the value of the variable to 1:

Function to showing temperature and humidity values. Also the next time sunrise and sunset and the date of the next new moon and full moon shown. Definitely, date, day of the week and current time is also displayed. Depending on the sensors detected during application startup, you can take the value of 1-wire temperature probe if SHT21 sensor not connencted.

We define this module as the main application and its contents contain the main thread:



We added an additional parameter called ‘bgcolor‘ in the ‘def draw_rotated_text‘ (written in the display driver function) to allow changing the background color of the text. Thus we indicate the color on which we are writing.


8. Viewing the Weather Station application

Weather Station screen (Day)

We use sunrise and sunset time to show the day or night mode as appropriate. Brightness sensor is used to adjust the screen brightness level.

Weather Station screen (Night)

About the author



Would you like to share your thoughts?

Your email address will not be published. Required fields are marked *

Leave a Reply

Uso de cookies

Este sitio web utiliza cookies para que usted tenga la mejor experiencia de usuario. Si continúa navegando está dando su consentimiento para la aceptación de las mencionadas cookies y la aceptación de nuestra política de cookies, pinche el enlace para mayor información.plugin cookies

Aviso de cookies