DHT22 and a ATTiny85
Moderators: adafruit_support_bill, adafruit

DHT22 and a ATTiny85

by EVRE on Tue Sep 24, 2013 2:16 pm

I built a project a little while back that used an Arduino micro, DHT22, display and batteries.
The project was successful, it ran for 3 days on 3 AA's with use of 8 second WDT. But, I wanted to make it even more battery friendly.
I transitioned the project to my ATTiny85's I've had stored away for such a project. (I've had these before Adafruit came out with the very cool Trinket!).

The trouble is the ATTiny85 and the DHT22 are not playing well with each other.
It simply reads 0.00C and 0.00 or Nan for humidity.

I think it has something to do with the clock rates of the Tiny. At 1mhz it updates a lot faster than at 8mhz. I checked this by also uploading a sketch to an Arduino telling the compiler it was a 3.3v micro @ 8mhz, had to slow its serial.begin to 4800 and run the viewer at 9600 to receive serial communication. Could the same thing be happening within the library for the micro? There are a few places with delay and delayMicroseconds. I have doubled, and played around with those values, but to no avail.

Does Adafruit have any trouble with the ATTiny running at 1 or 8mhz using the DHT22 sensor and provided library?

2013-09-24 11-13-22.448.jpg
2013-09-24 11-13-22.448.jpg (808.33 KiB) Viewed 1088 times

Code: Select all | TOGGLE FULL SIZE
Tevis Cox =^;~;^=

Temp and humidity


#include <TinyWireM.h>                  // I2C Master lib for ATTinys which use USI
#include <LiquidCrystal_I2C.h> 
#include <DHT.h>
#include <stdio.h>

#define DHTPIN 3     // what pin we're connected to
#define DHTTYPE DHT22

float fltTemperature, fltHumidity;

 LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  delay(1000);  //give some time for other components to wake up
  //lcd setup
  lcd.print("Temp        RH");

void loop(){

void updateDisplay(){
  lcd.print("    ");

void ReadDHT(){
  fltHumidity = dht.readHumidity();
  fltTemperature = ((dht.readTemperature())* 9.0/5.0 + 32);

Posts: 57
Joined: Sat Feb 18, 2012 11:21 pm

Re: DHT22 and a ATTiny85

by EVRE on Tue Sep 24, 2013 3:30 pm

To be more specific, I have toyed with the insides of the DHT.h and DHT.cpp
Been thinking about it a bit..
Next I'm going to try it without the TinyWire library, it has some stuff in there that needs to be edited for 1 or 8mhz operation. It may have created a conflict somehow. The TinyWire is for the display, oddly the display could care less how I configure the TinyWire library for mhz... set to 1 mhz runing at 8.. set at 8 running at 1, set to 1 and running at 1...... It may be that it wants cpu speed specified so it can modify an internal timer, that modification maybe whats hosing the DHT22's communication.
Posts: 57
Joined: Sat Feb 18, 2012 11:21 pm

Re: DHT22 and a ATTiny85

by EVRE on Tue Sep 24, 2013 6:20 pm

Software serial isnt working either =/ I'm at a loss..
I can connect it to a regular arduino and have it read no problem.

2013-09-24 15-13-09.952.jpg
2013-09-24 15-13-09.952.jpg (111.89 KiB) Viewed 1059 times
Posts: 57
Joined: Sat Feb 18, 2012 11:21 pm

Re: DHT22 and a ATTiny85

by EVRE on Tue Sep 24, 2013 11:24 pm

This library works! :)


Hope this is helpful to 'adafruit'

Posts: 57
Joined: Sat Feb 18, 2012 11:21 pm

Re: DHT22 and a ATTiny85

by adafruit_support_mike on Tue Sep 24, 2013 11:35 pm

It's almost certainly a timing problem.

I had to check the datasheet (http://www.adafruit.com/datasheets/DHT22.pdf) to refresh my memory, but remembered correctly that the DHT22 uses a timing-dependent serial protocol. There's no clock signal like you'd have for I2C or SPI, so you have to measure the duration of the pulses.

For each bit, the DHT22 starts by taking the DATA pin LOW for 50 microseconds. Then it sends the DATA line HIGH to send the bit value. Both a 0 and a 1 are represented by a HIGH voltage, so the bit's actual value is encoded in the amount of time the DATA pin remains HIGH.

A HIGH pulse 26-28 microseconds long corresponds to a bit-value of 0. A HIGH pulse 70 microseconds long corresponds to a bit-value of 1.

If your ATTiny85 is running at a different clock rate than a stock Arduino, those values will get skewed.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.

Posts: 15971
Joined: Thu Feb 11, 2010 2:51 pm

Re: DHT22 and a ATTiny85

by EVRE on Wed Sep 25, 2013 1:00 pm

Thanks for the reply :)

I'll take some time tonight and compare the way the two libraries work so I can understand why 1 works and not the other.
Posts: 57
Joined: Sat Feb 18, 2012 11:21 pm