Hi~ I'm irina but My nickname is "Y"
When we think of temperature/humidity sensors in IoT, DHT-11 comes to mind the most. So today, I would like to introduce the working principle of DHT11 to the source code.
STEP 1. What is DHT11?If you look for it, surprisingly, it is a temperature/humidity sensor that is often used when you want to check the temperature and humidity of products specialized for IoT such as Arduino or Raspberry Pi.
humidity : 20~95%
temperature: 0~50ºC
As explained in the figure, the pins are in order from left to right.
VCC,Signal, NC, GND
NC is an unused pin, so only 3 pins are actually used.
When using the above product, a normal value is output only when a resistor (5K) is connected between VCC and Signal.
If there is no resistor, you can also use a module.
Since the module already has a resistor connected to the signal, the wiring is simple and easy to use.
STEP 2. Operation PrincipleWhen I first dealt with it, I didn't even know how it works and didn't write it.
As I write it, I wonder what kind of principle it is,
There are two types of electrical humidity measurement methods: capacitance sensing type nd resistance sensing type, but the capacitive sensing type sensor has high precision but is expensive.
So DHT11 uses resistance sensing type.
The two electrodes are not connected to each other, but a small amount of current flows due to the moisture in the air. So, when there is more moisture in the air, more current flows, so the resulting resistance value is measured as humidity.
In the temperature measurement of DHT11, the resistance value of the material changes according to the temperature, and a temperature sensor called thermistor is included in the DHT11 and is used together with the humidity sensor!
STEP 3. Source codeNext, This code is micropython of DHT11.
Depending on the version of micropython, the corresponding code may be built-in.
If there is no error when "import DHT" enter to the code that is actually executed, you do not need to use the DHT code below.
import array
import micropython
import utime
from machine import Pin
from micropython import const
class InvalidChecksum(Exception):
pass
class InvalidPulseCount(Exception):
pass
MAX_UNCHANGED = const(100)
MIN_INTERVAL_US = const(200000)
HIGH_LEVEL = const(50)
EXPECTED_PULSES = const(84)
class DHT11:
_temperature: float
_humidity: float
def __init__(self, pin):
self._pin = pin
self._last_measure = utime.ticks_us()
self._temperature = -1
self._humidity = -1
def measure(self):
current_ticks = utime.ticks_us()
if utime.ticks_diff(current_ticks, self._last_measure) < MIN_INTERVAL_US and (
self._temperature > -1 or self._humidity > -1
):
# Less than a second since last read, which is too soon according
# to the datasheet
return
self._send_init_signal()
pulses = self._capture_pulses()
buffer = self._convert_pulses_to_buffer(pulses)
self._verify_checksum(buffer)
self._humidity = buffer[0] + buffer[1] / 10
self._temperature = buffer[2] + buffer[3] / 10
self._last_measure = utime.ticks_us()
@property
def humidity(self):
self.measure()
return self._humidity
@property
def temperature(self):
self.measure()
return self._temperature
def _send_init_signal(self):
self._pin.init(Pin.OUT, Pin.PULL_DOWN)
self._pin.value(1)
utime.sleep_ms(50)
self._pin.value(0)
utime.sleep_ms(18)
@micropython.native
def _capture_pulses(self):
pin = self._pin
pin.init(Pin.IN, Pin.PULL_UP)
val = 1
idx = 0
transitions = bytearray(EXPECTED_PULSES)
unchanged = 0
timestamp = utime.ticks_us()
while unchanged < MAX_UNCHANGED:
if val != pin.value():
if idx >= EXPECTED_PULSES:
raise InvalidPulseCount(
"Got more than {} pulses".format(EXPECTED_PULSES)
)
now = utime.ticks_us()
transitions[idx] = now - timestamp
timestamp = now
idx += 1
val = 1 - val
unchanged = 0
else:
unchanged += 1
pin.init(Pin.OUT, Pin.PULL_DOWN)
if idx != EXPECTED_PULSES:
raise InvalidPulseCount(
"Expected {} but got {} pulses".format(EXPECTED_PULSES, idx)
)
return transitions[4:]
def _convert_pulses_to_buffer(self, pulses):
"""Convert a list of 80 pulses into a 5 byte buffer
The resulting 5 bytes in the buffer will be:
0: Integral relative humidity data
1: Decimal relative humidity data
2: Integral temperature data
3: Decimal temperature data
4: Checksum
"""
# Convert the pulses to 40 bits
binary = 0
for idx in range(0, len(pulses), 2):
binary = binary << 1 | int(pulses[idx] > HIGH_LEVEL)
# Split into 5 bytes
buffer = array.array("B")
for shift in range(4, -1, -1):
buffer.append(binary >> shift * 8 & 0xFF)
return buffer
def _verify_checksum(self, buffer):
# Calculate checksum
checksum = 0
for buf in buffer[0:4]:
checksum += buf
if checksum & 0xFF != buffer[4]:
raise InvalidChecksum()
STEP 4. Example codeThere is no need for the "pin clear" part, but depending on the board, there is a part where the pin is working, so I put it in.
I made it so that the temperature and humidity were well expressed, and I made it print out once every 2 seconds.
from machine import Pin
import time
from dht import DHT11, InvalidChecksum
# Pin Clear
for i in range(0,29):
led = machine.Pin(i, machine.Pin.OUT)
led.value(0)
time.sleep(0.1) # sleep 100ms
sensor = DHT11(Pin(15, Pin.OUT, Pin.PULL_DOWN)) #DHT11 Pin15
while True:
temp = sensor.temperature
humidity = sensor.humidity
print("Temperature: {}°C Humidity: {:.0f}% ".format(temp, humidity))
time.sleep(2)
The output is as below...
I am using a tool called Thonny to easily compile and use MicroPython!!
Comments
Please log in or sign up to comment.