Embedded DHT11, temperature turns to 0 after first print

dht11 datasheet
dht11 sensor
dht11 library
dhtlib
how to install dht11 library in arduino
dht11 proteus library download zip
humidity sensor arduino
dht11 temperature and humidity sensor

So, I am trying to set up a DHT11 in a C environment to try to learn some more embedded software programming.

I've taken help from other sources and the documentation to create this and the temperature shows only once and then defaults to 0.

I am using an Arduino as microcontroller (because it is the only one I have) and use a Linux Ubuntu environment to load up the program in to the microcontroller.

In the end, the code looks like this:

#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>

#include "dht.h"

// Trying to set up the DHT11 so it can be used in this project
void read() {

  // The bits are for to get the temperature values in.
  // reqCounter is for the request later on.
  uint8_t bits[5];

  // Setting up the DDRD as OUTPUT and PORTD as HIGH
  DDRD |= (1 << DDD5);
  PORTD |= (1 << PORTD5);
  _delay_ms(100);

  /*
   * Makes an request to get the data from the DHT11
   * Set the the PORTD5 as LOW
  */
  PORTD &= ~(1 << PORTD5);
  _delay_ms(18);
  // Setting it back to high and DDRD as an input
  PORTD |= (1 << PORTD5);
  _delay_us(1);
  DDRD &= ~(1 << DDD5);

  /*
   * Checking to see if the ACK is happening
  */
  _delay_us(39);
  if ((PIND & (1 << PIND5))) {
    return;
  }
  _delay_us(80);
  if (!(PIND & (1 << PIND5))) {
    return;
  }
  _delay_us(80);

  // Holds the temperature 
  uint8_t temp;

  // Reading the data
  for (uint8_t i = 0; i < 5; i++) {
    // Create a for loop that looks in to every 8 bits
    for (uint8_t j = 0; j < 8; j++) {
      // While it isn`t high, loop this while loop
      while (!(PIND & (1 << PIND5)))
              ;
        _delay_us(30);
        // Looking if the input is still high after 30us
        if (PIND & (1 << PIND5)) {
          temp |= (1 << (7-i));
        }
        // Wait until the input is low.
        while (PIND & (1 << PIND5))
          ;
    }
      // Put the temperature in the bit array
      bits[i] = temp;
  }

  // Resets the pins
  DDRD |= (1 << DDD5);
  PORTD |= (1 << PORTD5);
  _delay_ms(100);

  uint8_t temperature ;
  //checks the sum and gets the temperature 
  if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
    temperature = bits[2];
  }

  printf("temperature : %d\n", temperature );
  /*printf("%d", bits[0]);
  printf("%d", bits[1]);
  printf("%d", bits[2]);
  printf("%d", bits[3]);
  printf("%d\n", bits[4]);*/
}

What this produces is just 15 once and then noting more. I've tried to blow on it to rise the temperature, but nothing happened.

What might be the issue here?

Thanks in advance


Here:

  uint8_t temperature ;
  //checks the sum and gets the temperature 
  if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
    temperature = bits[2];
  }

  printf("temperature : %d\n", temperature );

If bits[0] + bits[1] + bits[2] + bits[3] == bits[4] is false, then temperature is initialised (and may have any value - including zero or 15, one of those values is junk).

Perhaps:

static uint8_t temperature = 0 ;

So that when bits[0] + bits[1] + bits[2] + bits[3] != bits[4], the last valid temperature will be used.

Or more simply, if temperature is not to be used elsewhere :

  //checks the sum and gets the temperature 
  if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) 
  {
      printf( "temperature : %d\n", bits[2] ) ;
  }

What I imagine is happening here however is the uninitialised value of temperature happend to be 15 and you print it, then thereafter bits[0] + bits[1] + bits[2] + bits[3] == bits[4] becomes true and bits[2] is always zero. In which case the error is in the input and values placed in bits[], and this solution will result in the value always being zero.

Temperature and Humidity Sensor Controlling Humidifier, Find this and other hardware projects on Hackster.io. Embed the widget on your own site The first was connected to a temperature/humidity sensor (​DHT11), To turn on the heater block, a PWM signal is sent to the relay on the for communications PietteTech_DHT DHT(DHTPIN, DHTTYPE); int n=0;  DHT11 is a Temperature and humidity sensor which as the name implies is used to measure the atmospheric temperature and humidity in a particular environment or in a confined closed space. The sensor is commonly used in monitoring environmental parameters in many applications like Agriculture, Food Industries, Hospitals, Automobile, Weather


So, thank you all for your help, but I have managed to find out a way to make it work. If you want to know how, I have put it in pastebin as a way for you all to see.

But in short, I made a change so it looks a bit more like this in the for loops:

    // Reading the data and starts with bits 0 up to 4
  for (j = 0; j < 5; j++) {
        uint8_t result = 0;
    // I read every bit in the system to be able to get it in to the bits.
        for (i = 0; i < 8; i++) {
            while (!(PIND & (1 << PIND5)))
                ; //wait for an HIGH input
        // delay for 30us
            _delay_us(30);
      //if the input is HIGH then I will put the data in result
            if (PIND & (1 << PIND5))
                result |= (1 << (7 - i));
            while (PIND & (1 << PIND5))
                ; //wait until input get LOW
        }
    //Put the result in the bit
        bits[j] = result;
    }

Sorry for any inconvenience and thank you all.

Interfacing DHT11 Temperature & Humidity Sensor with , dht.begin();. Turn on the blacklight and print a welcome message and clear them after three seconds. lcd.backlight(); lcd.setCursor(0  Interface DHT11 (Humidity Sensor) Using NodeMCU: Hey Makers!In this Instructables you will learn how to set up the DHT11 Humidity and Temperature sensor on your NodeMCU. And learn about how the Humidity sensor works, and how to check output readings from the Serial monitor.


It is not clear why you have chosen to re-write (and break) the code at "other sources", and not simply used it as-is:

void read( void )
{
    printf("temperature : %d\n", (int)DHT11_GetData(DHT11_DATA_TEMPERATURE)) ;
}

DFR0067 DHT11 Temperature and Humidity Sensor, Convenient connections and special packages can be provided according to users need. Specification. Supply Voltage: +5 V; Temperature range :0-50 °C error of  Interfacing the DHT11 sensor with ESP32 is very simple as it has only three pins. Connect the VCC and GND pin of the sensor to the 3.3 V and GND pin of ESP32. Then connect the Data pin of the sensor to the D2 pin of ESP32. After making the connections my hardware looks like this: IFTTT Setup for Temperature and Humidity Measurement


Interfacing of Arduino Uno with Temperature and Humidity sensor, A definitive guide to interface the DHT 11 temperature and humidity sensor with the The full code to display the data of the sensor on a serial monitor is given below. First, we need to connect the Arduino environment with our Arduino Uno setCursor(11,1); lcd.print(i[0]); } z=0; i[0]=i[1]=i[2]=i[3]=i[4]=0; } }  DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. Today in this tutorial we will be using DHT11 sensor with STM32.


DHT11 Driver in Rust, The high pulses on the other hand should vary a lot more. We should get a higher value for a 1 and a lower value for 0. If we get a print out of the  Hello, I built my first arduino project (measuring the room temperature and humidity with the DHT11) during Christmas holidays. The readings of the values were shown on the screen of my laptop. The measured room temperature was correct, but the measured humidity was much too low (about 20%RH).


Overview | DHT11, DHT22 and AM2302 Sensors, This tutorial covers the low cost DHT temperature & humidity sensors. with 5% accuracy; Good for 0-50°C temperature readings ±2°C accuracy The Arduino has built in pullups you can turn on but they're very weak, about 20-50K First make sure you are running the latest version of Adafruit CircuitPython for your  DHT11 - Humidity and Temperature Sensor The DHT11 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). Its fairly simple to use, but requires careful timing to grab data.