Difference between revisions of "DHT11"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
(Created page with "The DHT11 is a temperature and humidity sensor. ==External links== * Arduino Library for the sensor https://www.arduino.cc/reference/en/libraries/dht11/")
 
Line 1: Line 1:
 
The DHT11 is a temperature and humidity sensor.
 
The DHT11 is a temperature and humidity sensor.
 +
 +
== Reading Values using MicroPython ==
 +
 +
<syntaxhighlight lang="python" line='line'>
 +
import machine
 +
import time
 +
import dht
 +
 +
DHT_PIN = 25
 +
sensor = dht.DHT11(machine.Pin(DHT_PIN))
 +
 +
while (True):
 +
    sensor.measure()
 +
    humidity = sensor.humidity()
 +
    temperature_C = sensor.temperature()
 +
 +
    print("Humidity: {:.1f}%  |  Temperature: {:.1f}°C".format(humidity, temperature_C))
 +
   
 +
    except OSError as e:
 +
        print("Failed to read from DHT sensor! {}".format(e))
 +
 +
    time.sleep(2)
 +
</syntaxhighlight>
  
 
==External links==
 
==External links==
 
* Arduino Library for the sensor https://www.arduino.cc/reference/en/libraries/dht11/
 
* Arduino Library for the sensor https://www.arduino.cc/reference/en/libraries/dht11/

Revision as of 14:36, 12 June 2024

The DHT11 is a temperature and humidity sensor.

Reading Values using MicroPython

 1 import machine
 2 import time
 3 import dht
 4 
 5 DHT_PIN = 25
 6 sensor = dht.DHT11(machine.Pin(DHT_PIN))
 7 
 8 while (True):
 9     sensor.measure()
10     humidity = sensor.humidity()
11     temperature_C = sensor.temperature()
12 
13     print("Humidity: {:.1f}%  |  Temperature: {:.1f}°C".format(humidity, temperature_C))
14     
15     except OSError as e:
16         print("Failed to read from DHT sensor! {}".format(e))
17 
18     time.sleep(2)

External links