How to Program a Temperature Sensor

For this tutorial, we’re going to be using a DHT11 temperature sensor. Temperature sensors are used for thermostats and many other recreational projects and we’re going to learn how to output the temperature and humidity values.

Requirements

  • Arduino
  • DHT 11 temperature/humidity sensor
  • DHT Library
  • 3 wires

How does it work

Temperature

The sensor measures the temperature by using a thermistor on the main sensor (NTC temperature sensor).

Humidity

The sensor has a component that holds moisture by a substrate within the main sensor. The substrate has electrodes which decrease in resistance in the presence of water vapor while resistance increases when there’s barely any water vapor in the air. These readings are calculated in relative humidity which is expressed in percentage

Schematic

Code

#include <dht.h>
dht DHT;

We’re going to be using this DHT library (link on requirements) and we’re going to instantiate the DHT library.

void setup() {
  Serial.begin(9600);
}

Instantiate the serial monitor to print out the humidity and temperature.

void loop() {
    DHT.read11(5);
    Serial.println((DHT.temperature *9/5)+32);
    Serial.println(DHT.humidity);
    delay(1000);
}

Now, in order for this to work, you need to make sure that the temperature sensor is being read in the right digital pin so make sure to input DHT.read11(pin number). Now for the temperature, normally it’ll print in Celcius but the equation I have above will allow you to print in Farenheight. Humidity will simply print in relative humidity (percentage)

Final Code

#include <dht.h>
dht DHT;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
    DHT.read11(5);
    Serial.println((DHT.temperature *9/5)+32);
    Serial.println(DHT.humidity);
    delay(1000);
}

Leave a comment

Your email address will not be published. Required fields are marked *