接线

  • vcc接入派3.3v或5v输出。
  • GND接派GND
  • DATA接派任意GPIO控制口。

wiringPi 测试代码

//  
//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;  
}  

代码中以GPIO 1 口,物理pin 12口为例。

编译运行

$gcc -Wall -o mydht11 mydht11.c -lwiringPi
$./mydht11

Use GPIO1 to read data!
Enter OS-------
Sorry! Sensor dosent ans!
Congratulations ! Sensor data read ok!
RH:49.0
TMP:22.9
Congratulations ! Sensor data read ok!
RH:49.0
TMP:23.0

标签: 树莓派

添加新评论