Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 027d2de

Browse files
committed
first commit version 0.3
0 parents  commit 027d2de

File tree

5 files changed

+354
-0
lines changed

5 files changed

+354
-0
lines changed

DHT22.cpp

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*
2+
DHT22.cpp - DHT22 sensor library
3+
Developed by Ben Adams - 2011
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
19+
20+
Humidity and Temperature Sensor DHT22 info found at
21+
http://www.sparkfun.com/products/10167
22+
23+
Version 0.3: 17-Jan-2011 by Ben Adams
24+
This version reads data
25+
Needs check sum code added at the end of readData
26+
27+
Version 0.2: 16-Jan-2011 by Ben Adams
28+
Changed coding style to match other Arduino libraries.
29+
This version will not read data either!
30+
31+
Version 0.1: 10-Jan-2011 by Ben Adams nethoncho AT gmail.com
32+
First Version is a skeleton. This version will not read data!
33+
Code adapted from the following sources:
34+
The Arduino OneWire lib
35+
http://sheepdogguides.com/arduino/ar3ne1humDHT11.htm
36+
37+
*/
38+
39+
#include "DHT22.h"
40+
#include "pins_arduino.h"
41+
42+
extern "C" {
43+
#include "WConstants.h"
44+
#include <avr/io.h>
45+
#include <avr/interrupt.h>
46+
#include <avr/pgmspace.h>
47+
}
48+
49+
#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
50+
#define DIRECT_MODE_INPUT(base, mask) ((*(base+1)) &= ~(mask))
51+
#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) |= (mask))
52+
#define DIRECT_WRITE_LOW(base, mask) ((*(base+2)) &= ~(mask))
53+
//#define DIRECT_WRITE_HIGH(base, mask) ((*(base+2)) |= (mask))
54+
55+
// This should be 40, but the sensor is adding an extra bit at the start
56+
#define DHT22_DATA_BIT_COUNT 41
57+
58+
DHT22::DHT22(uint8_t pin)
59+
{
60+
_bitmask = digitalPinToBitMask(pin);
61+
_baseReg = portInputRegister(digitalPinToPort(pin));
62+
_lastReadTime = millis();
63+
_lastHumidity = DHT22_ERROR_VALUE;
64+
_lastTemperature = DHT22_ERROR_VALUE;
65+
}
66+
67+
//
68+
// Read the 40 bit data stream from the DHT 22
69+
// Store the results in private member data to be read by public member functions
70+
//
71+
DHT22_ERROR_t DHT22::readData()
72+
{
73+
uint8_t bitmask = _bitmask;
74+
volatile uint8_t *reg asm("r30") = _baseReg;
75+
uint8_t retryCount;
76+
uint8_t bitTimes[DHT22_DATA_BIT_COUNT];
77+
int currentHumidity;
78+
int currentTemperature;
79+
uint8_t checkSum;
80+
unsigned long currentTime;
81+
int i;
82+
83+
currentHumidity = 0;
84+
currentTemperature = 0;
85+
checkSum = 0;
86+
currentTime = millis();
87+
for(i = 0; i < DHT22_DATA_BIT_COUNT; i++)
88+
{
89+
bitTimes[i] = 0;
90+
}
91+
92+
if(currentTime - _lastReadTime < 2000)
93+
{
94+
// Caller needs to wait 2 seconds between each call to readData
95+
return DHT_ERROR_TOOQUICK;
96+
}
97+
_lastReadTime = currentTime;
98+
99+
// Pin needs to start HIGH, wait until it is HIGH with a timeout
100+
cli();
101+
DIRECT_MODE_INPUT(reg, bitmask);
102+
sei();
103+
retryCount = 0;
104+
do
105+
{
106+
if (retryCount > 125)
107+
{
108+
return DHT_BUS_HUNG;
109+
}
110+
retryCount++;
111+
delayMicroseconds(2);
112+
} while(!DIRECT_READ(reg, bitmask));
113+
// Send the activate pulse
114+
cli();
115+
DIRECT_WRITE_LOW(reg, bitmask);
116+
DIRECT_MODE_OUTPUT(reg, bitmask); // Output Low
117+
sei();
118+
delayMicroseconds(1100); // 1.1 ms
119+
cli();
120+
DIRECT_MODE_INPUT(reg, bitmask); // Switch back to input so pin can float
121+
sei();
122+
// Find the start of the ACK Pulse
123+
retryCount = 0;
124+
do
125+
{
126+
if (retryCount > 25) //(Spec is 20 to 40 us, 25*2 == 50 us)
127+
{
128+
return DHT_ERROR_NOT_PRESENT;
129+
}
130+
retryCount++;
131+
delayMicroseconds(2);
132+
} while(!DIRECT_READ(reg, bitmask));
133+
// Find the end of the ACK Pulse
134+
retryCount = 0;
135+
do
136+
{
137+
if (retryCount > 50) //(Spec is 80 us, 50*2 == 100 us)
138+
{
139+
return DHT_ERROR_ACK_TOO_LONG;
140+
}
141+
retryCount++;
142+
delayMicroseconds(2);
143+
} while(DIRECT_READ(reg, bitmask));
144+
// Read the 40 bit data stream
145+
for(i = 0; i < DHT22_DATA_BIT_COUNT; i++)
146+
{
147+
// Find the start of the sync pulse
148+
retryCount = 0;
149+
do
150+
{
151+
if (retryCount > 35) //(Spec is 50 us, 35*2 == 70 us)
152+
{
153+
return DHT_ERROR_SYNC_TIMEOUT;
154+
}
155+
retryCount++;
156+
delayMicroseconds(2);
157+
} while(!DIRECT_READ(reg, bitmask));
158+
// Measure the width of the data pulse
159+
retryCount = 0;
160+
do
161+
{
162+
if (retryCount > 50) //(Spec is 80 us, 50*2 == 100 us)
163+
{
164+
return DHT_ERROR_DATA_TIMEOUT;
165+
}
166+
retryCount++;
167+
delayMicroseconds(2);
168+
} while(DIRECT_READ(reg, bitmask));
169+
bitTimes[i] = retryCount;
170+
}
171+
// Now bitTimes have the number of retries (us *2)
172+
// that were needed to find the end of each data bit
173+
// Spec: 0 is 26 to 28 us
174+
// Spec: 1 is 70 us
175+
// bitTimes[x] <= 11 is a 0
176+
// bitTimes[x] > 11 is a 1
177+
// Note: the bits are offset by one from the data sheet, not sure why
178+
for(i = 0; i < 16; i++)
179+
{
180+
if(bitTimes[i + 1] > 11)
181+
{
182+
currentHumidity |= (1 << (15 - i));
183+
}
184+
}
185+
for(i = 0; i < 16; i++)
186+
{
187+
if(bitTimes[i + 17] > 11)
188+
{
189+
currentTemperature |= (1 << (15 - i));
190+
}
191+
}
192+
for(i = 0; i < 8; i++)
193+
{
194+
if(bitTimes[i + 33] > 11)
195+
{
196+
checkSum |= (1 << (7 - i));
197+
}
198+
}
199+
200+
_lastHumidity = (float(currentHumidity & 0x7FFF) / 10.0);
201+
if(currentTemperature & 0x8000)
202+
{
203+
// Below zero, non standard way of encoding negative numbers!
204+
currentTemperature &= 0x7FFF;
205+
_lastTemperature = (float(currentTemperature) / 10.0) * -1.0;
206+
}
207+
else
208+
{
209+
_lastTemperature = float(currentTemperature) / 10.0;
210+
}
211+
212+
// TODO: Test the checksum and return DHT_ERROR_CHECKSUM if bad
213+
214+
return DHT_ERROR_NONE;
215+
}
216+
217+
float DHT22::getHumidity()
218+
{
219+
return _lastHumidity;
220+
}
221+
222+
float DHT22::getTemperatureC()
223+
{
224+
return _lastTemperature;
225+
}
226+
227+
//
228+
// This is used when the millis clock rolls over to zero
229+
//
230+
void DHT22::clockReset()
231+
{
232+
_lastReadTime = millis();
233+
}

DHT22.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef _DHT22_H_
2+
#define _DHT22_H_
3+
4+
#include <inttypes.h>
5+
6+
#define DHT22_ERROR_VALUE -99.5
7+
8+
typedef enum
9+
{
10+
DHT_ERROR_NONE = 0,
11+
DHT_BUS_HUNG,
12+
DHT_ERROR_NOT_PRESENT,
13+
DHT_ERROR_ACK_TOO_LONG,
14+
DHT_ERROR_SYNC_TIMEOUT,
15+
DHT_ERROR_DATA_TIMEOUT,
16+
DHT_ERROR_CHECKSUM,
17+
DHT_ERROR_TOOQUICK
18+
} DHT22_ERROR_t;
19+
20+
class DHT22
21+
{
22+
private:
23+
uint8_t _bitmask;
24+
volatile uint8_t *_baseReg;
25+
unsigned long _lastReadTime;
26+
float _lastHumidity;
27+
float _lastTemperature;
28+
29+
public:
30+
DHT22(uint8_t pin);
31+
DHT22_ERROR_t readData(void);
32+
float getHumidity();
33+
float getTemperatureC();
34+
void clockReset();
35+
};
36+
37+
#endif /*_DHT22_H_*/

README

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
DHT22.cpp - DHT22 sensor library
2+
Developed by Ben Adams - 2011
3+
4+
Humidity and Temperature Sensor DHT22 info found at
5+
http://www.sparkfun.com/products/10167
6+
7+
Version 0.3: 17-Jan-2011 by Ben Adams
8+
This version reads data
9+
Needs check sum code added at the end of readData
10+
11+
Version 0.2: 16-Jan-2011 by Ben Adams
12+
Changed coding style to match other Arduino libraries.
13+
This version will not read data either!
14+
15+
Version 0.1: 10-Jan-2011 by Ben Adams nethoncho AT gmail.com
16+
First Version is a skeleton. This version will not read data!
17+
Code adapted from the following sources:
18+
The Arduino OneWire lib
19+
http://sheepdogguides.com/arduino/ar3ne1humDHT11.htm

examples/Serial/Serial.pde

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <DHT22.h>
2+
3+
// Data wire is plugged into port 7 on the Arduino
4+
// Connect a 4.7K resistor between VCC and the data pin (strong pullup)
5+
#define DHT22_PIN 7
6+
7+
// Setup a DHT22 instance
8+
DHT22 myDHT22(DHT22_PIN);
9+
10+
void setup(void)
11+
{
12+
// start serial port
13+
Serial.begin(9600);
14+
Serial.println("DHT22 Library Demo");
15+
}
16+
17+
void loop(void)
18+
{
19+
DHT22_ERROR_t errorCode;
20+
21+
delay(2000);
22+
Serial.print("Requesting data...");
23+
errorCode = myDHT22.readData();
24+
if(errorCode == DHT_ERROR_NONE)
25+
{
26+
Serial.print("Got Data ");
27+
Serial.print(myDHT22.getTemperatureC());
28+
Serial.print("C ");
29+
Serial.print(myDHT22.getHumidity());
30+
Serial.println("%");
31+
}
32+
else
33+
{
34+
Serial.print("Error Code ");
35+
Serial.print(errorCode);
36+
Serial.println(" readData Failed");
37+
}
38+
}

keywords.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#######################################
2+
# Syntax Coloring Map For DHT22
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
DHT22 KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
readData KEYWORD2
16+
getHumidity KEYWORD2
17+
getTemperatureC KEYWORD2
18+
clockReset KEYWORD2
19+
20+
#######################################
21+
# Instances (KEYWORD2)
22+
#######################################
23+
24+
25+
#######################################
26+
# Constants (LITERAL1)
27+
#######################################

0 commit comments

Comments
 (0)