【手把手教你树莓派3 (五)】DHT11传感器

连线方式:

先贴一张树莓派3b的引脚图:



在连线的时候注意编码方式,注意树莓派引脚图的方向,如上图所示。我用的是三个头的dht11传感器,因此连线方式是:

VCC(或正极) --- 树莓派的3v电源

GND (地线或者负极)--- 树莓派的gnd接口

DATA (D或者out) ---树莓派的GPIO引脚

注意自己的传感器的接口的顺序。

python脚本

python使用的是PRi.gpio库来控制引脚,这个库在树莓派里面已经导入了,所以我们直接写代码就好,我用的是下面的代码:

import RPi.GPIO as GPIO
import time

channel =4 
data = []
j = 0

GPIO.setmode(GPIO.BCM)

time.sleep(1)

GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)
GPIO.output(channel, GPIO.HIGH)
GPIO.setup(channel, GPIO.IN)

while GPIO.input(channel) == GPIO.LOW:
  continue
while GPIO.input(channel) == GPIO.HIGH:
  continue

while j < 40:
  k = 0
  while GPIO.input(channel) == GPIO.LOW:
    continue
  while GPIO.input(channel) == GPIO.HIGH:
    k += 1
    if k > 100:
      break
  if k < 8:
    data.append(0)
  else:
    data.append(1)

  j += 1

print "sensor is working."
print data

humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]

humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0

for i in range(8):
  humidity += humidity_bit[i] * 2 ** (7-i)
  humidity_point += humidity_point_bit[i] * 2 ** (7-i)
  temperature += temperature_bit[i] * 2 ** (7-i)
  temperature_point += temperature_point_bit[i] * 2 ** (7-i)
  check += check_bit[i] * 2 ** (7-i)

tmp = humidity + humidity_point + temperature + temperature_point

if check == tmp:
  print "temperature :", temperature, "*C, humidity :", humidity, "%"
else:
  print "wrong"
  print "temperature :", temperature, "*C, humidity :", humidity, "% check :", check, ", tmp :", tmp

GPIO.cleanup()
这里要注意,编码方式和引脚号

(1)编码方式:PR库有两种方式,一种是bcm另一种是wiringPi的方式,两者的区别大致是bcm的的pin的序号0,1,3,。。。是与每一个gpio引脚对应的,不包含gnd和电源之类的在内,而wiringPi则是从电源开始为0,然后按增序排。使用python的脚本,只需要在setmode函数里面制定编码方式即可。

(2)引脚号:程序里面用的是bcm的4号(即channel变量),注意连线方式是否与代码匹配

C语言脚本:

要先安装支持GPIO的wiringPi库,这个并没有在系统中预先装好,步骤如下:

sudo apt-get install git-core
git clone git://git.drogon.net/wiringPi
cd wiringPi
git pull origin
cd wiringPi
./build

安装好以后测试:

输入命令:

gpio -v
可以显示版本,如下图


也可以输入:

gpio readall
来查看树莓派的引脚编码:



以上安装命令参见官网:

http://wiringpi.com/download-and-install/

下面是c代码:

/*
 *  dht11.c:
 *	Simple test program to test the wiringPi functions
 *	DHT11 test
 */
 
#include <wiringPi.h>
 
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define MAXTIMINGS	85
#define DHTPIN		7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };
 
void read_dht11_dat()
{
	uint8_t laststate	= HIGH;
	uint8_t counter		= 0;
	uint8_t j		= 0, i;
	float	f; /* fahrenheit */
 
	dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
 
	/* pull pin down for 18 milliseconds */
	pinMode( DHTPIN, OUTPUT );
	digitalWrite( DHTPIN, LOW );
	delay( 18 );
	/* then pull it up for 40 microseconds */
	digitalWrite( DHTPIN, HIGH );
	delayMicroseconds( 40 );
	/* prepare to read the pin */
	pinMode( DHTPIN, INPUT );
 
	/* detect change and read data */
	for ( i = 0; i < MAXTIMINGS; i++ )
	{
		counter = 0;
		while ( digitalRead( DHTPIN ) == laststate )
		{
			counter++;
			delayMicroseconds( 1 );
			if ( counter == 255 )
			{
				break;
			}
		}
		laststate = digitalRead( DHTPIN );
 
		if ( counter == 255 )
			break;
 
		/* ignore first 3 transitions */
		if ( (i >= 4) && (i % 2 == 0) )
		{
			/* shove each bit into the storage bytes */
			dht11_dat[j / 8] <<= 1;
			if ( counter > 16 )
				dht11_dat[j / 8] |= 1;
			j++;
		}
	}
 
	/*
	 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
	 * print it out if data is good
	 */
	if ( (j >= 40) &&
	     (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )
	{
		f = dht11_dat[2] * 9. / 5. + 32;
		printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
			dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
	}else  {
		printf( "Data not good, skip\n" );
	}
}
 
int main( void )
{
	printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );
 
	if ( wiringPiSetup() == -1 )
		exit( 1 );
 
	while ( 1 )
	{
		read_dht11_dat();
		delay( 1000 ); /* wait 1sec to refresh */
	}
 
	return(0);
}
(1)这里也要注意编码方式和引脚号,

如果采用wiringPIDE编码,那么程序中用函数

wiringPiSetup()
反之用bcm则使用函数

wiringPiSetupGpio()
以上信息可查看:

http://raspberrypi.stackexchange.com/questions/40067/using-bcm-gpio-pin-numbers-with-wiringpi-c-library

最后就是检查引脚号和程序是否匹配。

如果不匹配,改程序会一直提示:”Data not good,skip“,如果一直出现这个信息,说明引脚连接方式不对。

(2)注意编译方式:需要-l参数引入wiringPi库,

gcc dht11_c -o dht11_c -l wiringPi
执行:

sudo ./dht11_c


结果,目前我的结果不太正确,温度和湿度一直是66,我怀疑是我的传感器有问题,新买一个周三到,到时候试了再贴图。


事实证明确实我的传感器有问题,换了一个成功了。

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值