---
# System prepended metadata

title: 'Part 1: DHT11 and MCP9700'
tags: [TA Stuff RP2, Raspberry Pi Pico]

---

# Part 1: DHT11 and MCP9700
###### tags: `TA Stuff RP2` `Raspberry Pi Pico`

:::danger
**⚠️IMPORTANT⚠️**
+ Disconnect the power/USB cable from the Development/Expansion board before connecting sensors or changing a sensor pin. This allows us to check that everything is correct before powering the board.
+ Always check if the sensor works on 5V or 3.3V before connecting your sensor to the board. If you are unsure check the sensor datasheet or consult it with a TA.
+ We recommend using colored wires to make it easier to distinguish which wires are which.
+ We recommend using a breadboard if you have several sensors it makes the connection more visible.
:::

## DHT11 Temperature and Humidity Sensor
The DHT11 is a digital sensor measuring temprature and humidity. Raspberry Pi Pico W MicroPython has a builtin library for this sensor so you do not need to import an extra library when you write code to read its values. As you can see it the following image there are different pin settings for DHT11 based on the manufacturer. If you have one like in the left part you can recongized the pin by the sign beside each. GND presented usually by (-), VCC by (+), and Data Pin by (S). So please double check before connecting your sensor to the Raspberry Pi.

![](https://hackmd.io/_uploads/H12N_dlD2.png)


You can find DHT example in [**LNU GitHub**](https://github.com/iot-lnu/applied-iot/tree/master/Raspberry%20Pi%20Pico%20(W)%20Micropython/sensor-examples/P5_DHT_11_DHT_22). The connection looks like the following figure but always check your sensor pin setting first to make sure they match.

![](https://hackmd.io/_uploads/HJ04BveDn.png)

### Example Code
You find a sample code below which can be copied to your `main.py` and test your dht11. 

</br>

```python=
import dht                 # import the builtin library
from machine import Pin    # library for pin access      
import time                # library for making delays between reading

tempSensor = dht.DHT11(Pin(27))     # DHT11 Constructor 

while True:
    try:
        tempSensor.measure()
        temperature = tempSensor.temperature()
        humidity = tempSensor.humidity()
        print("Temperature is {} degrees Celsius and Humidity is {}%".format(temperature, humidity))
    except:
        print("Error in reading sensor values")
    time.sleep(2)
```

</br>

## MCP9700 Analog Temperature Sensor

The MCP9700 is an analog temperature sensor that can measure temperatures between -40°C to +150°C. 


### Pinout & Characteristics 

MCP9700 pinout                | MCP9700 Characteristics
:-------------------------:|:-------------------------:
![](https://hackmd.io/_uploads/rJt1mvxw3.png =300x) | ![](https://hackmd.io/_uploads/HJjhmwgw2.png =300x)

<br>

- The VDD pin is the voltage in pin, so in our case, we connect that pin to 3.3V on the Pico W.
- The GND pin is the ground pin, and we connect it to the GND pin on the Pico W. 
- The VOUT pin is the signal pin, which will output a voltage proportional to the actual temperature. We connect this pin to pin 27 on the Pico W.


### Example code 

To start measuring the temperature with your MCP9700, add the following code snippet to `main.py` and run the program (by saving the file while `development mode` is activated). 

```python=
import machine
import utime

adc = machine.ADC(27)
sf = 4095/65535 # Scale factor
volt_per_adc = (3.3 / 4095)

while True: 
    mv = adc.read_u16()

    adc_12b = mv * sf

    volt = adc_12b * volt_per_adc

    # MCP9700 characteristics
    dx = abs(50 - 0)
    dy = abs(0 - 0.5)

    shift = volt - 0.5

    temp = shift / (dy / dx)
    print(temp)
```
