RaspBerry pi 驱动温湿度传感器DHT11
一、接线
树莓派 DHT11
3.3V –> VCC
GPIO4 –> DTA
GND –> GND
二、安装Python库
sudo pip3 install Adafruit_DHT
三、Python程序读取温度
import Adafruit_DHT
import time
humidity, temperature = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, 4) #GPIO 4
while 1 :
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
else:
print('Failed to get reading. Try again!')
四、C语言版
- 安装wiringPi
#下载 wiringPi
git clone https://github.com/WiringPi/WiringPi
#编译 wiringPi
cd WiringPi
sudo ./build
- 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 = 7; // pin7
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("PIN:%d\n", pinNumber);
if (-1 == wiringPiSetup()) {
printf("Setup wiringPi failed!");
return 1;
}
pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
printf("Starting...\n");
while (1)
{
pinMode(pinNumber, OUTPUT); // set mode to output
digitalWrite(pinNumber, 1); // output a high level
delay(3000);
if (readSensorData())
{
printf("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("Sensor dosent ans!\n");
databuf = 0;
}
}
return 0;
}
- 编译运行C文件
gcc -Wall -o dht11 dht11.c -lwiringPi
sudo ./dht11