树莓派4BDebian Linux+DHT11温湿度传感器读取C语言版本
网上常见的有三种读取 DHT11 温湿度传感器数值的方法:
用系统功能直读、用 Adafruit 的库读取、用 Python 编程读取。
尽管方法很多,但是在不同的树莓派上、使用不同的生产商生产的 DHT11 模块时,可能出现读出的数值无效,或者直接读不出的情况。
那么这边再多提供一种亲测可用的 C 语言版本供大家选择尝试。
DHT11温湿度传感器连接
VCC - 引脚接 5V
DATA - 引脚接 WiringPi 编码 7
GND - 引脚接树莓派的 GND
下图亦可通过树莓派终端命令获取:
$ gpio readall
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | OUT | 1 | 7 || 8 | 1 | IN | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | IN | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
安装 WiringPi
这里使用了 WiringPi 库,如果已经安装过可以跳过这一步。
sudo apt-get update
#如果之前没有安装过 git,执行下面的命令安装一下
sudo apt-get install git-core
#下载 wiringPi
git clone https://github.com/WiringPi/WiringPi
#编译 wiringPi
cd WiringPi
sudo ./build
编写代码
源代码如下,保存为 dht11.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;
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;
}
编译程序并运行
gcc -Wall -o dht11 dht11.c -lwiringPi
sudo ./dht11
忽略编译过程中的警告。运行之后将出现温湿度读数。
运行结果如下: