Introduction: Raspberry PI Weather Station
In this tutorial we explain how to give the Raspberry PI sensor temperature, humidity and brightness. We can easily turn around our Raspberry in a weather station.
Hardware requirements:
- Temperature and humidity sensor SHT21 I2C. Alternatively, you can use a 1-wire temperature sensor
- Brightness sensor YL-40 I2C
- 4 cables
- SPI Display ILI9341 (320x240px TFT Color)
- (Optional) IRP-102 HAT Board. This board makes us easy the work of connection all devices for this project, and also allows us to have a RTC clock.
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.
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:
sudo apt-get update
And if you want to perform an upgrade of already installed packages you run:
sudo apt-get -y upgrade
If you have not yet installed you should run Python (Python 2 version):
sudo apt-get install python-dev
1. Enable I2C
We have to check that I2C bus is enabled to be used by sensors. If not, we must to switch it on. Run the de configuration assistant this way.
sudo raspi-config
RTC Config 1 image
Select option 9: Advanced options. Then choose option A7: I2C.
RTC Config 2 image
Then you be asked if we want to enable I2C. Choose YES.
RTC Config 3 image
Get out of raspi-config. In the next restart of the system i2c modules will be loaded by default.
2. Install i2c-tools
This is an utility package for I2C managing.
sudo apt-get install i2c-tools
When the installation process have finished, type:
i2cdetect -y 1
This command scans all devices connected to I2C bus. The second parameter means the BUS ID, we must use 1 for Raspberry PI B+, B(rev 2) and A+, For the old model B (rev 1) we use 0 as bus ID. We’ll get something like that
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- 6f
70: -- -- -- -- -- -- -- --
You have to check device in position 6F is showed. This means RTC have been detected.
3. Module loading at startup
Edit the file rc.local
sudo nano /etc/rc.local
and add the following lines just before the exit 0
# I2C devices
modprobe i2c-dev
If the project we are carrying out with a probe 1-wire we further add the following lines in this file:
# Temperature Sensor
modprobe w1-gpio pullup=1
modprobe w1-therm strong_pullup=1
Get out the editor saving changes and restart system using
sudo reboot
Now we install the controller to manage YL-40 (Brightness sensor) from repositories:
sudo apt-get install python-smbus
If success, when we try again
i2cdetect -y 1
we should get:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- UU
70: -- -- -- -- -- -- -- --
UU characters means that device at this position is in use.
4. 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
5. 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:http://ingeniapp.com/sources/weather_station/
or just
wget http://ingeniapp.com/sources/weather_station/iRP102-weather-station.zip
unzip iRP102-weather-station.zip
cd iRP102-weather-station
sudo python weather-station.py
from shell.
6. Application modules description
gy21.py: Module for reading I2C sensors (temperature and humidity).+
Function to get humidity value:
def read_humidity(self):
Function to get temperature value:
def read_temperature(self):
Function to get brightness value:
def read_ain()
weather_station.py: Main application. It is important to remember that you can use the display and sensors must be imported modules:
import Adafruit_ILI9341 as TFT
import RPi.GPIO as RPGPIO
import Adafruit_GPIO.SPI as SPI
import gy21
from smbus import SMBus
Function to showing temperature and humidity values. The current date and time are also displayed. Depending on the sensors detected during application startup, you can take the value of 1-wiretemperature probe if SHT21 sensor not connencted.
def show_time_temp():
We define this module as the main application and its contents contain the main thread:
if __name__ == "__main__":
NOTE: 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.
def draw_rotated_text(image, text, position, angle, font, fill=(255,255,255), bgtext=(255,255,255,255)):
Step 1: Updated Application!
These images show the update I made the application.
Now, this shows the time of sunrise and sunset, lunar calendar and day of week. The display birghtness level is auto adjusting depending on the ambient light level.
You can read it on http://ingeniapp.com/en


