# DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging

## Overview

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/001/866/medium800/raspberry_pi_dht22wiring.gif?1447864319)

Time to start exploring more sensors with the Raspberry Pi and Beaglebone Black! Today we'll be checking out the [DHT11](https://www.adafruit.com/products/386), [DHT22](https://www.adafruit.com/products/385) and [AM2302](https://www.adafruit.com/products/393 "Link: https://www.adafruit.com/products/393") humidity and temperature sensors available from Adafruit  
  
In this tutorial we'll be showing how to install a DHT sensor Python library which utilizes C for high-speed GPIO polling to handle bit-banged sensor output. Many low cost sensors have unusual output formats, and in this case, a "Manchester-esque" output that is not SPI, I2C or 1-Wire compatible must be polled continuously by the Pi to decode. Luckily, the C GPIO libraries are fast enough to decode the output.   
  
Once we have that working, we add the fun of Python to update a google spreadsheet live with the temperature/humidity data. This project would be the great basis for home or garden automation!  
  
 [You can check out our spreadsheet here, it wont be updated live after Aug 24 2012 but it will show you the format of data you get](https://docs.google.com/spreadsheet/ccc?key=0AlwXpwqqd84DdFVobHFCWXZLU2l2V212WnFrS3QwdkE#gid=0 "Link: https://docs.google.com/spreadsheet/ccc?key=0AlwXpwqqd84DdFVobHFCWXZLU2l2V212WnFrS3QwdkE#gid=0")

http://youtu.be/Skr2uPZzviM

# DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging

## Wiring

# Wiring up DHT humidity sensors
  

## Raspberry Pi
Its easy to connect these sensors to your Raspberry Pi. Our code can use any GPIO pin, but we'll be using GPIO #4 for our diagrams and code. Once you have it working, you can simply adapt the code to change to any other GPIO pin (e.g. pin # **18** ). You can also have as many DHT sensors as you want **but** they cannot share the data pin - each sensor needs a unique data pin!  
  
**For DHT11 and DHT22 sensors, don't forget to connect a 4.7K - 10K resistor from the data pin to VCC**  
  
& if 4.7K doesnt work, try 10K  
![](https://cdn-learn.adafruit.com/assets/assets/000/001/860/medium800/raspberry_pi_dht11wiring.gif?1447864313)

![](https://cdn-learn.adafruit.com/assets/assets/000/001/861/medium800/raspberry_pi_dht22wiring.gif?1447864317)

If your sensor has a white wire, leave it disconnected

![](https://cdn-learn.adafruit.com/assets/assets/000/001/862/medium800/raspberry_pi_am2302wiring.gif?1447864317)

## Beaglebone Black
Connecting a DHT sensor to the Beaglebone Black is just as simple as connecting to a Raspberry Pi. In this example pin P8\_11 will be used, but you can use any free digital GPIO pin. If you aren't familiar with how pins are numbered on the Beaglebone Black, check out the [tutorial on Beaglebone Black GPIO](https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/using-the-bbio-library).  
  
**Don't forget to connect the 4.7k-10k resistor from the data pin to VCC, like with the Raspberry Pi wiring above.**  
  
![](https://cdn-learn.adafruit.com/assets/assets/000/017/249/medium800/raspberry_pi_beagleboneblack_dht11.png?1402350860)

![](https://cdn-learn.adafruit.com/assets/assets/000/017/250/medium800/raspberry_pi_beagleboneblack_dht22.png?1402350882)

![](https://cdn-learn.adafruit.com/assets/assets/000/017/251/medium800/raspberry_pi_beagleboneblack_am2302.png?1402350896)

# DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging

## Python Setup

We're going to use a special library called&nbsp;[**adafruit\_blinka**](https://pypi.org/project/Adafruit-Blinka/)&nbsp;([named after Blinka, the CircuitPython mascot](https://www.adafruit.com/?q=blinka)) to provide the layer that translates the CircuitPython hardware API to whatever library the Linux board provides.&nbsp;

# Installing CircuitPython Libraries on Raspberry Pi or BeagleBone Black

If you haven't set up your Raspberry Pi or BeagleBone Black for running CircuitPython libraries yet, follow our guide and come back to this page when you've completed the steps listed on the page and verified that your setup is working:

- [Setup your Linux Board for using CircuitPython Libraries](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi)

## Installing the CircuitPython-DHT Library

You'll also need to install a library to communicate with the DHT sensor. Since we're using Adafruit Blinka (CircuitPython), we can install CircuitPython libraries straight to our small linux board. In this case, we're going to&nbsp;install the CircuitPython\_DHT library. This library works with both the DHT22 and DHT11 sensors.

Run the following command to&nbsp;**install the [CircuitPython-DHT library](https://github.com/adafruit/Adafruit_CircuitPython_DHT)**:

`pip3 install adafruit-circuitpython-dht`

`sudo apt-get install libgpiod2`

## Testing the CircuitPython DHT Library

To make sure you've installed everything correctly, we're going to test that we can read values from the DHT sensor connected to your device.

Create a new file called&nbsp; **dht\_simpletest.py** &nbsp;with&nbsp; **nano** &nbsp;or your favorite text editor and put the following in:

https://github.com/adafruit/Adafruit_CircuitPython_DHT/blob/main/examples/dht_simpletest.py

Next, you're going to need to modify a line of code in this file with information about the pin the DHT sensor is connected to and the type of DHT sensor you're using.

If you're using a&nbsp; **Raspberry Pi** with a **DHT22** &nbsp;(or an **AM2302** ) sensor connected to **Pin 4** , **change the following line from:**

`dhtDevice = adafruit_dht.DHT22(board.D18)`

**to**

`dhtDevice = adafruit_dht.DHT22(board.D4)```

If you're using a **BeagleBone Black** with a **DHT22 (or an AM2302)** sensor **connected to Pin&nbsp;P8\_11** , **change the following line from**

`dhtDevice = adafruit_dht.DHT22(board.D18)`

**to&nbsp;**

`dhtDevice = adafruit_dht.DHT22(board.P8_11)`

**If you're using a DHT11 sensor** , you can change the sensor type by **renaming the&nbsp;DHT22 class&nbsp;to DHT11** :

`dhtDevice = adafruit_dht.DHT11(board.D18)`

Then, **save the example**. Next,&nbsp; **run the example by typing the following command into the terminal:**

`python3 dht_simpletest.py`

 **Press enter to run the example**. You should see the temperature (in Fahrenheit and Celsius) and humidity values displayed in the terminal:

```auto
Temp: 73.4 F / 23.0 C    Humidity: 40.3%
Temp: 73.4 F / 23.0 C    Humidity: 40.3%
```

If your output looks like the output above - your linux board is set up for use with CircuitPython libraries and can read values from a connected DHT sensor.

### 

Check that the wiring is correct (Are ground and power connected? is the correct DHT pin pulled up to 3.3v?). Then, make sure you provided the&nbsp;`dhtDevice`&nbsp;with the correct physical pin number your board is connected to.

### 

Make sure you installed the CircuitPython DHT library with **pip_3_** and are running the command with **python_3_**.&nbsp;

### 

Make sure you're running the script with python3. If you're still receiving this error, [follow the setup guide for installing CircuitPython libraries](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/installing-circuitpython-on-raspberry-pi) again.

# DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging

## Connecting to Google Docs

Danger: 

Danger: 

[RE: DHT 22 Temperature and Humidity sensor with adafruit code](https://forums.adafruit.com/viewtopic.php?f=19&t=53299&p=271580#p271310)

# Create and prepare spreadsheet

First up you will need to [sign up for Google Docs](http://sheets.google.com) and create a spreadsheet. We're going to call ours DHT Humidity Logs.  
  
Once you've created it, delete all but one line (since we don't want 1000 empty rows):

![](https://cdn-learn.adafruit.com/assets/assets/000/017/252/medium800/raspberry_pi_deleteros.gif?1448046998)

Then make the one remaining line a header with row names:

![](https://cdn-learn.adafruit.com/assets/assets/000/017/253/medium800/raspberry_pi_title.gif?1448046993)

# Get OAuth2 credentials

As of April 2015 Google has deprecated the older simple authentication interface for accessing Google spreadsheet data. &nbsp;You must carefully follow the steps below to enable OAuth2 access to your Google spreadsheet. &nbsp;Unfortunately these steps are somewhat complex, so go through them very carefully to make sure you don't miss a step. &nbsp;If you run into problems try consulting the [gspread python library](https://github.com/burnash/gspread)&nbsp;that this script uses.

To get your OAuth2 credentials follow the steps on this page:

- [gspread - Using OAuth2 for Authorization](http://gspread.readthedocs.org/en/latest/oauth2.html)

After you follow the steps in the document above you should have downloaded a .json file, like `SpreadsheetData-(gibberish).json`. &nbsp; **Place this .json file in the same directory as the `google_spreadsheet.py` example.** &nbsp;If you don't place this file in the same directory then authentication will fail and you will not be able to update your spreadsheet!

One last step that **must be completed** is to share your Google spreadsheet to the email address associated with the OAuth2 credentials. &nbsp;Open the .json file and search for the **"client\_email":** line that looks like this (but with a different email address):

```auto
"client_email": "149345334675-md0qff5f0kib41meu20f7d1habos3qcu@developer.gserviceaccount.com",
```

Take note of that email address value and go to your Google spreadsheet in a web browser. &nbsp;Using the **File -\> Share...** menu item share the spreadsheet with **read and write access** to the email address found above. &nbsp; **Make sure to share your spreadsheet or you will not be able to update it with the script!**

# Run Python Code
First up we will have to install the **gspread** python library, which will do the heavy lifting of connecting to google docs and updating the spreadsheet! With your board connected and online, run the following:  
```auto
sudo pip3 install gspread oauth2client pyasn1 pyasn1-modules
```

Create a new file called&nbsp; **google\_spreadsheet.py** &nbsp;with&nbsp; **nano** &nbsp;or your favorite text editor and put the following in:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/DHT_Google_Spreadsheet/google_spreadsheet.py

Next, in the **examples** directory again, edit **google\_spreadsheet.py** and adjust the configuration values towards the top of the file:

```auto
# Type of sensor, can be adafruit_dht.DHT11 or adafruit_dht.DHT22.
# For the AM2302, use the adafruit_dht.DHT22 class.
DHT_TYPE = adafruit_dht.DHT22

# Example of sensor connected to Raspberry Pi Pin 23
DHT_PIN  = board.D4
# Example of sensor connected to Beaglebone Black Pin P8_11
# DHT_PIN  = 'P8_11'

# Google Docs OAuth credential JSON file.  Note that the process for authenticating
# ...
GDOCS_OAUTH_JSON       = 'your SpreadsheetData-*.json file name'

# Google Docs spreadsheet name.
GDOCS_SPREADSHEET_NAME = 'your google docs spreadsheet name'
```

Make sure `DHT_TYPE` is set to the type of sensor you are using (either `adafruit_dht.DHT11`or`adafruit_dht.DHT22`), and `DHT_PIN` is set to the GPIO pin number which is connected to your DHT sensor. If you're using an&nbsp; **AM2302,&nbsp;** use the `adafruit_dht.DHT22` class.  
  
In the example above a Raspberry Pi GPIO pin #23 is shown, however commented below it is an example of a Beaglebone Black using GPIO pin P8\_11.  
  
Next make sure to set the **`GDOCS_OAUTH_JSON`&nbsp;** to the name of the `SpreadsheetData-*.json` file in the same directory as the `google_spreadsheet.py` file. &nbsp;If you don't have a `SpreadsheetData-*.json` file then you accidentally missed the steps above. &nbsp;**Go back and carefully follow the [OAuth2 credential steps](../../../../dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/connecting-to-googles-docs-updated#get-oauth2-credentials)&nbsp;to get an OAuth2 credential .json file before continuing!**  
  
Finally **set**  **`GDOCS_SPREADSHEET_NAME` to the name of your spreadsheet** , like '_DHT Humidity Logs_'.  
  
**Save the file** and **execute the Python script by running** :

> **python3 google\_spreadsheet.py**

You should see the program run and after about 30 seconds a humidity and temperature measurement is displayed and written to the spreadsheet. The program will continue to run and log a measurement every 30 seconds until you force it to quit by pressing `Ctrl-C`.   
  
The measurement frequency can be adjusted by changing the `FREQUENCY_SECONDS` configuration in the python code.  
  
Open the spreadsheet on Google's site and you should see measurements added in real time!

https://www.youtube.com/watch?v=Skr2uPZzviM

[You can also see our spreadsheet here, it wont be running live after Aug 24, 2012 but it gives you an idea of the data format](https://docs.google.com/spreadsheet/ccc?key=0AlwXpwqqd84DdFVobHFCWXZLU2l2V212WnFrS3QwdkE#gid=0)
## Featured Products

### Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi

[Adafruit Assembled Pi Cobbler Breakout + Cable for Raspberry Pi](https://www.adafruit.com/product/914)
Now that you've finally got your hands on a [Raspberry Pi® Model B](http://www.raspberrypi.org/), you're probably itching to make some fun embedded computer projects with it. What you need is an add on prototyping Pi Cobbler from Adafruit, which can break out all those...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/914)
[Related Guides to the Product](https://learn.adafruit.com/products/914/guides)
### BeagleBone Black Rev C - 4GB Flash - Pre-installed Debian

[BeagleBone Black Rev C - 4GB Flash - Pre-installed Debian](https://www.adafruit.com/product/1876)
Note: As of May 12, 2014 Adafruit is shipping Rev C. We have discontinued selling Rev B. There are no exchanges or "upgrades" for Rev B to Rev C.  
  
If you liked the BeagleBone Black Rev B, you will love the Rev C! The Rev C still has a blistering 1GHz processor and 512MB...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1876)
[Related Guides to the Product](https://learn.adafruit.com/products/1876/guides)
### BeagleBone Black - Rev B

[BeagleBone Black - Rev B](https://www.adafruit.com/product/1278)
**[Adafruit is no longer shipping the BeagleBone Black Rev B, it has been replaced with the Rev C as of 5/12/14](https://www.adafruit.com/products/1876) - the Rev C now has 4G flash and also comes with Debian, it also costs slightly more. There are no exchanges or...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1278)
[Related Guides to the Product](https://learn.adafruit.com/products/1278/guides)
### Miniature WiFi (802.11b/g/n) Module: For Raspberry Pi and more

[Miniature WiFi (802.11b/g/n) Module: For Raspberry Pi and more](https://www.adafruit.com/product/814)
Make your Internet of Things device cable-free by adding WiFi. Take advantage of the Raspberry Pi and Beagle Bone's USB port to add a low cost, but high-reliability wireless link. We tried half a dozen modules to find one that works well with the Pi and Bone without the need of recompiling...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/814)
[Related Guides to the Product](https://learn.adafruit.com/products/814/guides)
### AM2302 (wired DHT22)  temperature-humidity sensor

[AM2302 (wired DHT22)  temperature-humidity sensor](https://www.adafruit.com/product/393)
Discontinued - [**you can grab** AM2301B - Wired Enclosed AHT20 - Temperature and Humidity Sensor&nbsp; **instead!**](https://www.adafruit.com/product/5181)

The AM2302 is a wired version of the [DHT22](http://www.adafruit.com/products/385), in a large plastic...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/393)
[Related Guides to the Product](https://learn.adafruit.com/products/393/guides)
### DHT11 basic temperature-humidity sensor + extras

[DHT11 basic temperature-humidity sensor + extras](https://www.adafruit.com/product/386)
 **Discontinued -**  **you can grab the&nbsp;** [DHT20 - AHT20 Pin Module - I2C Temperature and Humidity Sensor](https://www.adafruit.com/product/5183)&nbsp; **instead!&nbsp;**

The DHT11 is a basic, ultra low-cost digital temperature and humidity sensor. It uses...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/386)
[Related Guides to the Product](https://learn.adafruit.com/products/386/guides)
### DHT22  temperature-humidity sensor + extras

[DHT22  temperature-humidity sensor + extras](https://www.adafruit.com/product/385)
The DHT22 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and spits out a digital signal on the data pin (no analog input pins needed). It's fairly simple to use but requires careful timing...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/385)
[Related Guides to the Product](https://learn.adafruit.com/products/385/guides)
### BeagleBone Black Rev C - 4GB - Pre-installed Debian

[BeagleBone Black Rev C - 4GB - Pre-installed Debian](https://www.adafruit.com/product/1996)
If you liked the BeagleBone Black Rev B, you will love the Rev C! The Rev C has a blistering 1GHz AM3358 processor and 512MB onboard DDR3 RAM, two 46-pin headers, micro HDMI for audio/video output, USB ports, 10/100 Ethernet and other I/O features. The Rev C is an ultra-powered embedded...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1996)
[Related Guides to the Product](https://learn.adafruit.com/products/1996/guides)

## Related Guides

- [Raspberry Pi Physical Dashboard](https://learn.adafruit.com/raspberry-pi-physical-dashboard.md)
- [3D Printed Raspberry Pi A+ Case](https://learn.adafruit.com/3d-printed-raspberry-pi-a-plus-case.md)
- [Adafruit BMP280 Barometric Pressure + Temperature Sensor Breakout](https://learn.adafruit.com/adafruit-bmp280-barometric-pressure-plus-temperature-sensor-breakout.md)
- [Adafruit MCP9600 I2C Thermocouple Amplifier](https://learn.adafruit.com/adafruit-mcp9600-i2c-thermocouple-amplifier.md)
- [Run an X-Carve CNC Machine Wirelessly with a Raspberry Pi](https://learn.adafruit.com/control-an-xcarve-cnc-machine-wirelessly-with-a-raspberry-pi.md)
- [FONA Tethering to Raspberry Pi or BeagleBone Black](https://learn.adafruit.com/fona-tethering-to-raspberry-pi-or-beaglebone-black.md)
- [Processing on the Raspberry Pi & PiTFT](https://learn.adafruit.com/processing-on-the-raspberry-pi-and-pitft.md)
- [Adafruit ESP32-S2 Feather](https://learn.adafruit.com/adafruit-esp32-s2-feather.md)
- [Adafruit Pi Box Plus](https://learn.adafruit.com/adafruit-pi-box-plus.md)
- [SSH to BeagleBone Black over USB](https://learn.adafruit.com/ssh-to-beaglebone-black-over-usb.md)
- [AD8495 Analog Output K-Type Thermocouple Amplifier](https://learn.adafruit.com/ad8495-thermocouple-amplifier.md)
- [MCP9808 Temperature Sensor Python Library](https://learn.adafruit.com/mcp9808-temperature-sensor-python-library.md)
- [Nokia 5110/3310 LCD Python Library](https://learn.adafruit.com/nokia-5110-3310-lcd-python-library.md)
- [How we designed an injection-molded case](https://learn.adafruit.com/how-we-designed-an-injection-molded-case-for-raspberry-pi.md)
- [Modern Replacements for DHT11 and DHT22 Sensors](https://learn.adafruit.com/modern-replacements-for-dht11-dht22-sensors.md)
