查看: 5328|回复: 1
收起左侧

DHT11温湿度传感器引脚图

2013-8-19 20:12:10 | 显示全部楼层 |阅读模式
dht2.jpg

如图 vcc接3.3v  Dout接GPIO0, NC留空不接, GND接地
注意正反面
创建脚本mydht11.c  本脚本非原创, 来自http://blog.csdn.net/liang890319/article/details/8739683
//
    //mydht11.c
    //
    #include <wiringPi.h>
    #include <stdio.h>
    #include <stdlib.h>

    typedef unsigned char uint8;
    typedef unsigned int  uint16;
    typedef unsigned long uint32;

    #define HIGH_TIME 32

    int pinNumber =1;  //use gpio1 to read data
    uint32 databuf;



    uint8 readSensorData(void)
    {
        uint8 crc;
        uint8 i;

        pinMode(pinNumber,OUTPUT); // set mode to output
        digitalWrite(pinNumber, 0); // output a high level
        delay(25);
        digitalWrite(pinNumber, 1); // output a low level
        pinMode(pinNumber, INPUT); // set mode to input
        pullUpDnControl(pinNumber,PUD_UP);

        delayMicroseconds(27);
        if(digitalRead(pinNumber)==0) //SENSOR ANS
           {
             while(!digitalRead(pinNumber)); //wait to high

          for(i=0;i<32;i++)
           {
           while(digitalRead(pinNumber)); //data clock start
           while(!digitalRead(pinNumber)); //data start
              delayMicroseconds(HIGH_TIME);
              databuf*=2;
               if(digitalRead(pinNumber)==1) //1

{
                    databuf++;
               }
            }

          for(i=0;i<8;i++)
           {
           while(digitalRead(pinNumber)); //data clock start
           while(!digitalRead(pinNumber)); //data start
              delayMicroseconds(HIGH_TIME);
              crc*=2;
              if(digitalRead(pinNumber)==1) //1
               {
                    crc++;
               }
            }
        return 1;
           }
       else
            {
            return 0;
             }
    }

    int main (void)
    {

      printf("Use GPIO1 to read data!\n");

      if (-1 == wiringPiSetup()) {
        printf("Setup wiringPi failed!");
        return 1;
      }

      pinMode(pinNumber, OUTPUT); // set mode to output
      digitalWrite(pinNumber, 1); // output a high level
printf("Enter OS-------\n");
      while(1) {
        pinMode(pinNumber,OUTPUT); // set mode to output
        digitalWrite(pinNumber, 1); // output a high level
        delay(3000);
        if(readSensorData())
        {
           printf("Congratulations ! Sensor data read ok!\n");
           printf("RH:%d.%d\n",(databuf>>24)&0xff,(databuf>>16)&0xff);
           printf("TMP:%d.%d\n",(databuf>>8)&0xff,databuf&0xff);
           databuf=0;
         }
        else
         {
            printf("Sorry! Sensor dosent ans!\n");
           databuf=0;
          }
      }
      return 0;
    }

编译
gcc -o mydht11 mydht11.c -L/usr/local/lib-lwiringPi
运行
./mydht11
结果
Use GPIO1 to read data!
Enter OS-------
Congratulations ! Sensor data read ok!
RH:35.0
TMP:25.0


参考文章
http://blog.csdn.net/liang890319/article/details/8739683
回复

使用道具 举报

2014-7-29 11:29:47 | 显示全部楼层
谢谢分享,尝试着弄一下!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们,了解更多

官方微信

服务时间:10:00-16:00

13714503811

公司地址:深圳市龙岗区南湾街道东门头路8号

Copyright © 2012-2020 Powered by 树莓派论坛 2019.4  粤ICP备15075382号-1
快速回复 返回列表 返回顶部