hi
im trying to use dht22 sensor and connecting it to arduino uno without using dht lib
the code for sensor
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hi = dht.computeHeatIndex(f, h);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");
}
is there anyone write full code for this sensor without using dht lib
because i have parallax propeller micro controller and i want to connect dht22 sensor with it
but i need to understand how sensor code work to rewrite code for propeller MCU
Kind Regards,
robtillaart what i want is code without using #include dht.h
i want to rewrite the same code for P8X32A Propeller QuickStart (Rev.B)
i find this code
http://www.electrodragon.com/w/index.php?title=DHT11#Demo_code_1
both of demo code doesn't work?
i connect dht22 sensor to Arduino Uno via digital pin 2
also i test my connection with DHT code and lib and it's work fine
is there any modification can we do for anyone of these two demo code to make it work with DHT22
any post!
i make some change for code but it's not working
#define dht11_pin 2 //Analog port 0 on Arduino Uno
//#define dht11_pin 54 //Analog port 0 on Arduino Mega2560
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++)
{
while (!digitalRead(dht11_pin));
delayMicroseconds(30);
if (digitalRead(dht11_pin) != 0 )
bitSet(result, 7-i);
while (digitalRead(dht11_pin));
}
return result;
}
void setup()
{
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, HIGH);
Serial.begin(9600);
Serial.println("Ready");
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;// start condition
digitalWrite(dht11_pin, HIGH);
delay(250);
digitalWrite(dht11_pin, LOW);
delay(20);
digitalWrite(dht11_pin, HIGH);
delayMicroseconds(40);
pinMode(dht11_pin, INPUT);
if (digitalRead(dht11_pin))
{
Serial.println("dht11 start condition 1 not met"); // wait for DHT response signal: LOW
delay(1000);
return;
}
delayMicroseconds(1);
if (!digitalRead(dht11_pin))
{
Serial.println("dht11 start condition 2 not met"); //wair for second response signal:HIGH
return;
}
delayMicroseconds(1);// now ready for data reception
for (i=0; i<5; i++)
{ dht11_dat[i] = read_dht11_dat();} //recieved 40 bits data. Details are described in datasheet
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, HIGH);
byte dht11_check_sum = dht11_dat[0]+dht11_dat[2];// check check_sum
if(dht11_dat[4]!= dht11_check_sum)
{
Serial.println("DHT11 checksum error");
}
Serial.print("Current humdity = ");
Serial.print(dht11_dat[0], DEC);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(dht11_dat[2], DEC);
Serial.println("C ");
delay(2000); //fresh time
}
also this one didn't work, it's show fake temp 32C
#define dht11_pin 2 //Analog port 0 on Arduino Uno
//#define dht11_pin 54 //Analog port 0 on Arduino Mega2560
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
uint8_t data[6];
float f;
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++)
{
while (!digitalRead(dht11_pin));
delayMicroseconds(30);
if (digitalRead(dht11_pin) != 0 )
bitSet(result, 7-i);
while (digitalRead(dht11_pin));
}
return result;
}
void setup()
{
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, HIGH);
Serial.begin(9600);
Serial.println("Ready");
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;// start condition
digitalWrite(dht11_pin, HIGH);
delay(250);
digitalWrite(dht11_pin, LOW);
delay(20);
digitalWrite(dht11_pin, HIGH);
delayMicroseconds(40);
pinMode(dht11_pin, INPUT);
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
for ( i=0; i< 80; i++) {
counter = 0;
while (digitalRead(dht11_pin) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
laststate = digitalRead(dht11_pin);
if (counter == 255) break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > 6)
data[j/8] |= 1;
j++;
}
}
f = data[2] & 0x7F;
f *= 256;
f += data[3];
f /= 10;
if (data[1] & 0x80)
f *= -1;
f = f * 9 / 5 + 32;
Serial.print("temperature = ");
Serial.print(f);
delay(2000); //fresh time
}
At what clock speed do you run that processor?
What is the granularity of delayMicroSeconds() on that board?
On an UNO @16MHZ it is 4 micros...
I've run on an UNO successful @8MHz and with dedicated code 4Mhz worked reasonably but not perfect.
i have Arduino UNO chine clone i think it 16 MHZ
You could start with
- copying my library code (.cpp) into your sketch
- strip the class frame from it
- define the class private vars as sketch global vars
- call the now standalone functions
- have a beer1 because it works
1Or another favorit beverage
robtillaart:
You could start with
- copying my library code (.cpp) into your sketch
- strip the class frame from it
- define the class private vars as sketch global vars
- call the now standalone functions
- have a beer1 because it works
1Or another favorit beverage
i do that with this code
#define dht11_pin 2 //Analog port 0 on Arduino Uno
//#define dht11_pin 54 //Analog port 0 on Arduino Mega2560
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
uint8_t data[6];
float f;
byte read_dht11_dat()
{
byte i = 0;
byte result=0;
for(i=0; i< 8; i++)
{
while (!digitalRead(dht11_pin));
delayMicroseconds(30);
if (digitalRead(dht11_pin) != 0 )
bitSet(result, 7-i);
while (digitalRead(dht11_pin));
}
return result;
}
void setup()
{
pinMode(dht11_pin, OUTPUT);
digitalWrite(dht11_pin, HIGH);
Serial.begin(9600);
Serial.println("Ready");
}
void loop()
{
byte dht11_dat[5];
byte dht11_in;
byte i;// start condition
digitalWrite(dht11_pin, HIGH);
delay(250);
digitalWrite(dht11_pin, LOW);
delay(20);
digitalWrite(dht11_pin, HIGH);
delayMicroseconds(40);
pinMode(dht11_pin, INPUT);
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
for ( i=0; i< 80; i++) {
counter = 0;
while (digitalRead(dht11_pin) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
laststate = digitalRead(dht11_pin);
if (counter == 255) break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > 6)
data[j/8] |= 1;
j++;
}
}
f = data[2] & 0x7F;
f *= 256;
f += data[3];
f /= 10;
if (data[1] & 0x80)
f *= -1;
f = f * 9 / 5 + 32;
Serial.print("temperature = ");
Serial.print(f);
delay(2000); //fresh time
}
and optimize it to show only temperature in C and its not work 
Did you read the datasheet to learn how the protocol/handshake looks like?
Do you use a pull-up resistor?
yes i use pull up resistor also i use your lib and code and my connection work fine
but i want to write sketch for this sensor without using dht lib (put the class needed inside the sketch)