Raspberry Pi Connection DHT11 temperature and humidity sensor (python)

Source: Internet
Author: User

    • Introduced
    The DHT11 is a cheap accessory that includes temperature and humidity sensors, and the code is easy to use. This article describes if you use DHT11 in a Raspberry Pi, the code is python. If you have any questions, please leave a comment below.
    • Wiring
  • VCC Connection 5V or 3.3V
  • Data is connected to the Gpio (I'm picking up the BCM pin 16th, the physical pin is 36)
  • GND Connection Gnd
    • Code
    [Codesyntax lang= "Python"]
  • [Python]View PlainCopy
    1. #!/usr/bin/python
    2. Import Rpi.gpio as GPIO
    3. Import time
    4. Channel = #引脚号16
    5. data = [] #温湿度值
    6. j = 0 #计数器
    7. Gpio.setmode (GPIO). BCM) #以BCM编码格式
    8. Time.sleep (1) #时延一秒
    9. Gpio.setup (channel, GPIO.) Out)
    10. Gpio.output (channel, GPIO.) Low)
    11. Time.sleep (0.02) #给信号提示传感器开始工作
    12. Gpio.output (channel, GPIO.) High)
    13. Gpio.setup (channel, GPIO.) IN)
    14. While Gpio.input (channel) = = GPIO. Low:
    15. Continue
    16. While Gpio.input (channel) = = GPIO. High:
    17. Continue
    18. While J < :
    19. K = 0
    20. While gpio.input (channel) = = GPIO. Low:
    21. Continue
    22. While gpio.input (channel) = = GPIO. High:
    23. K + = 1
    24. if k > :
    25. Break
    26. If K < 8:
    27. Data.append (0)
    28. Else:
    29. Data.append (1)
    30. J + = 1
    31. Print "sensor is working."
    32. Print Data #输出初始数据高低电平
    33. Humidity_bit = data[0:8] #分组
    34. Humidity_point_bit = data[8: +]
    35. Temperature_bit = data[:
    36. Temperature_point_bit = data[: +]
    37. Check_bit = data[:
    38. Humidity = 0
    39. Humidity_point = 0
    40. Temperature = 0
    41. Temperature_point = 0
    42. Check = 0
    43. For I in Range (8):
    44. Humidity + = humidity_bit[i] * 2 * * (7-i) #转换成十进制数据
    45. Humidity_point + = humidity_point_bit[i] * 2 * * (7-i)
    46. Temperature + = Temperature_bit[i] * 2 * * (7-i)
    47. Temperature_point + = temperature_point_bit[i] * 2 * * (7-i)
    48. Check + = check_bit[i] * 2 * * (7-i)
    49. TMP = humidity + humidity_point + temperature + temperature_point #十进制的数据相加
    50. If check = = tmp: #数据校验, equal output
    51. print "Temperature:", temperature, ", Humidity:", humidity
    52. else: #错误输出错误信息, and validate data
    53. print "wrong"
    54. print "Temperature:", temperature, ", Humidity:", humidity, "check:", check, "tmp:", TMP /c8>
    55. Gpio.cleanup () #重置针脚

      • Test
    Execution: sudo chmod 777 program name. PY
Python program name. py

Raspberry Pi Connection DHT11 temperature and humidity sensor (python)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.