---
# System prepended metadata

title: Sensor
tags: [python, MicroPython, DHT11]

---

# Sensor	

###### tags: `DHT11` `python` `MicroPython`



> [TOC]




## Temperature and humidity sensor 


### Import Model

```pyton=
import dht      #Sensor's module
from machine import Pin
```

### DHT object 


Create it and refers to the Pin


```pytho=
#setting data pin and creating sensor object 
sensor =dht.DHT11(Pin(2))
```


To measure and read the sensor


```python=
sensor.measure()
sensor.temperature()
sensor.humidity()
```

### Code

```python=
import dht      #Sensor's module
from machine import Pin
from time import sleep


#setting data pin and creating sensor object 
sensor =dht.DHT11(Pin(2))



while True:
    try:
        
        #maximum sampling rate
        sleep(1)
        
        #Get information from the sensor
        sensor.measure()
        
        #save reading 
        temperature=sensor.temperature()
        humidity=sensor.humidity()
        
        print(f'Temperature is : {temperature} C ')
        print(f'Hunidity is : {humidity} %% ' )

    except OSError as e:
        print('Failed to read')
        

```

### Result 

![](https://i.imgur.com/bJqrgMO.png)
