Raspberry Pi Projects

Raspberry Pi With Humidity Sensor Tutorial

Introduction

A Raspberry Pi With Humidity Sensor is a great way to keep track of the moisture in your home. By reading the humidity level, you can adjust the amount of time you spend watering your plants or adjust the temperature in your home to ensure that your family is comfortable. By using a humidity sensor, you can also monitor the amount of moisture in the air. We will also show you how to write a Python script to read data from the sensor and print it to the terminal.

What is a Raspberry Pi?

Raspberry Pi With Humidity Sensor

A Raspberry Pi is a credit card-sized computer that was created with the intention of teaching people about computer programming. It has since been used for a variety of projects, from creating a homemade security system to building a robot.

What is a humidity sensor?DHT22 Sensor

A humidity sensor is a device that measures the amount of water vapor in the air. There are many different types of humidity sensors but most work by measuring the amount of water vapor in the air using a hygrometer. Humidity sensors are used in many different applications, such as weather stations, greenhouses, and incubators. If you need to measure humidity in the range of 20% – 90% (at 25 ° C) close humidity – DHT11 is good.

Specification:

  • Dimensions 13 x 29 x 8 mm
  • 3 – 5.5V power supply
  • Average power consumption 0.2 mA
  • Communication: 1-Wire (OneWire)
  • Temperature measurement:   * Temperature measurement range 0 – 50 ° C    * 8-bit resolution (1 ° C)    * Accuracy 1 ° C    * Response time 6 – 15 s (typically 10 s)
  • Moisture measurement    * Humidity measurement range 20% – 90% RH (at 25 ° C) – in the full temperature range – 30% -80%    * 8-bit resolution (± 1% RH *)    * Accuracy ± 4 RH * (at 25 ° C)    * Measuring range 6 – 30 s    * Stability ± 1% RH / year

How to set up a Raspberry Pi With Humidity Sensor

A Raspberry Pi humidity sensor is a great way to measure the humidity in your environment. There are many different types of humidity sensors, but the most popular type is the DHT11. This sensor is relatively easy to set up, and it is very accurate. Here are the steps to setting up your own Raspberry Pi humidity sensor:

Connect the Raspberry Pi With Humidity Sensor.

connect the sensor to your Raspberry Pi. The sensor has three pins: power, ground, and data. Connect the power and ground pins to the corresponding pins on your Raspberry Pi, and connect the data pin to a GPIO pin.

DHT Pin Signal Pi Pin
1  Data/Out 23 (GPIO22)
2 3.3v 1
3 GND 6 or 9

Python Library

The DHT11 needs a specific protocol to be used for the data pin. In order to save time trying to implement this yourself, it’s far easier to use the Adafruit DHT library. This library will allow you to read data from the sensor

Software Setup

To start with update your package lists and install a few Python libraries :

Ad
sudo apt-get update
sudo apt-get install build-essential python-dev

Then clone the Adafruit library from their repository :

git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT

Then install the library for Python 2 and Python 3 :

sudo python setup.py install
sudo python3 setup.py install

Hopefully, at this topic, the library is installed and watchful to be used within a Python script.

Adafruit Example Raspberry Pi With Humidity Sensor Python Script

Adafruit provides an example script that you can use to check whether your sensor is working correctly or not.

cd ~
cd Adafruit_Python_DHT
cd examples

Then :

python AdafruitDHT.py 11 16

The example script takes two parameters. The first is the sensor type so is set to “11” to represent the DHT11.

The second is the GPIO number so for my example I am using “16” for GPIO16. You can change this if you are using a different GPIO pin for your data/out wire.

You should see an output similar to this :

Temp=22.0* Humidity=68.0%

Using the Library In Other Python Scripts

Once the Adafruit library is installed and you’ve got the example script to work you can use the sensor in your own scripts. Simply import the module, set up a few variables, and call the “read_retry” function :

import Adafruit_DHT
 
# Set sensor type : Options are DHT11,DHT22 or AM2302
sensor=Adafruit_DHT.DHT11
 
# Set GPIO sensor is connected to
gpio=17
 
# Use read_retry method. This will retry up to 15 times to
# get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
 
# Reading the DHT11 is very sensitive to timings and occasionally
# the Pi might fail to get a valid reading. So check if readings are valid.
if humidity is not None and temperature is not None:
  print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
  print('Failed to get reading. Try again!')

This script can be downloaded directly to your Pi using :

wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/dht11.py

and run using :

python dht11.py

Conclusion

DHT11 Sensor with the Raspberry pi to find humidity and temperature

Read Similar Articles:

Related Articles

Leave a Reply

Your email address will not be published.

Ad
Back to top button