DHT11 temprature not showing

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey.

 

Long story short, I have this idea of a self handling heating system.

So, I am trying to program an Arduino/DHT11 in C as a way to make it show the temprature and a "heating system" that is just a led lamp.

 

Yeah, it will be simple and easy, but it is a start.

 

Now, when I try to print the temprature from the DHT11, I only get 0. I've tried it on Arduino and I do get the temprature out from it.

 

I don't exactly know why this is happening, hench why I am asking you all.

 

Here is the code for the DHT11:

#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 temprature values in.
  // reqCounter is for the request later on.
  uint8_t bits[5];
  int reqCounter = 0;

  // 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);
  DDRD &= ~(1 << DDD5);

  /*
   * Checking to see if the ACK is happening
   * If it does, add +1 to the req_counter
  */
  _delay_us(30);
  if ((PIND & (1 << PIND5))) {
    reqCounter++;
  }
  _delay_us(80);
  if (!(PIND & (1 << PIND5))) {
    reqCounter++;
  }
  _delay_us(80);

  /*
   * If the req_counter == 2, then I will run the request to get the data
  */
  if (reqCounter == 2) {

    // Holds the temprature
    uint8_t temp = 0;

    // Used to check if the pin is alright
    uint8_t loop = 0;

    // Reading the data
    for (int i = 0; i < 5; i++) {

      // Holds the temprature
      temp = 0;

      // Create a for loop that looks in to every 8 bits
      for (int 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));
        }
      }

      // Put the temprature in the bit array
      bits[i] = temp;
    }

    // set req_counter to 0 so it wont run all the time
    reqCounter = 0;
  }

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

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

  printf("%d\n", temprature);

  _delay_ms(2000);

}

Thank you for anyone who helps me.

Best Regards
Ravoos

Last Edited: Thu. Oct 3, 2019 - 03:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The code you've posted doesn't have a main() - so where's the rest of it?

 

Ravoos wrote:
I've tried it on Arduino and I do get the temprature out from it

So why not keep using the Arduino?

 

If you're not using an Arduino - what target hardware are you using ?

 

If you're not using the Arduino IDE - what IDE are you using ?

 

You realise that Atmel Studio 7 can import Arduino "sketches" ?

 

If you're not using an Arduino - why have you posted under 'Arduino' ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Oct 3, 2019 - 02:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ravoos wrote:

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

#include "dht.h"

 

I don't see where you have defined F_CPU, which is used by delay.h.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey, sorry for the late response.
To answer them in question.

1) I am only using it as a microcontroller because I want to keep using the C language because I want to work with programming in the future. So this is kind of a nice way to keep studying the language.

 

2) I am only using Atom on Linux to edit the code. I am then using some commands to compile it on the arduino microcontroller.

 

3) No. I did not know that.

 

4) I thought it would fit here as I am using the Arduino as a microcontroller but not the IDE or its language. Why am I not using another Microcontroller? It is the only one I have ATM.

 

// Patrik

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello.

 

 

Hmmmmm. I don't need to define F_CPU, because it still works without it....or am I wrong?

 

// Ravoos

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ravoos wrote:
I don't need to define F_CPU, because it still works without it.

If you don't set it yourself, it gets a default value

 

If it works for you, then you were just "lucky" that the default happened to be OK for you - but you should not rely on that!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay. Then I am really bloody lucky.

 

How would you recommend to define F_CPU?

 

Also, I will do this later as I am at work ATM.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ravoos wrote:
How would you recommend to define F_CPU?

I would recommend that you do what the documentation tells you ...

 

https://www.nongnu.org/avr-libc/user-manual/group__util__delay.html#ga43bafb28b29491ec7f871319b5a3b2f8

 

https://www.microchip.com/webdoc/AVRLibcReferenceManual/group__util__delay.html

 

EDIT

 

It is also described in delay.h:

 

/** \defgroup util_delay <util/delay.h>: Convenience functions for busy-wait delay loops
    \code
    #define F_CPU 1000000UL  // 1 MHz
    //#define F_CPU 14.7456E6
    #include <util/delay.h>
    \endcode

    \note As an alternative method, it is possible to pass the
    F_CPU macro down to the compiler from the Makefile.
    Obviously, in that case, no \c \#define statement should be
    used.

    The functions in this header file are wrappers around the basic
    busy-wait functions from <util/delay_basic.h>.  They are meant as
    convenience functions where actual time values can be specified
    rather than a number of cycles to wait for.  The idea behind is
    that compile-time constant expressions will be eliminated by
    compiler optimization so floating-point expressions can be used
    to calculate the number of delay cycles needed based on the CPU
    frequency passed by the macro F_CPU.

    \note In order for these functions to work as intended, compiler
    optimizations <em>must</em> be enabled, and the delay time
    <em>must</em> be an expression that is a known constant at
    compile-time.  If these requirements are not met, the resulting
    delay will be much longer (and basically unpredictable), and
    applications that otherwise do not use floating-point calculations
    will experience severe code bloat by the floating-point library
    routines linked into the application.

    The functions available allow the specification of microsecond, and
    millisecond delays directly, using the application-supplied macro
    F_CPU as the CPU clock frequency (in Hertz).

*/

 

In particular, note:

    \note As an alternative method, it is possible to pass the
    F_CPU macro down to the compiler from the Makefile.
    Obviously, in that case, no \c \#define statement should be
    used.

That would be the recommended way to do it!

 

 

You can see the default being applied here:

#ifndef F_CPU
/* prevent compiler error by supplying a default */
# warning "F_CPU not defined for <util/delay.h>"
/** \ingroup util_delay
    \def F_CPU
    \brief CPU frequency in Hz

    The macro F_CPU specifies the CPU frequency to be considered by
    the delay macros.  This macro is normally supplied by the
    environment (e.g. from within a project header, or the project's
    Makefile).  The value 1 MHz here is only provided as a "vanilla"
    fallback if no such user-provided definition could be found.

    In terms of the delay functions, the CPU frequency can be given as
    a floating-point constant (e.g. 3.6864E6 for 3.6864 MHz).
    However, the macros in <util/setbaud.h> require it to be an
    integer value.
 */
# define F_CPU 1000000UL
#endif

So you should have got a warning - did you not?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Oct 4, 2019 - 09:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ravoos wrote:

Hello.

 

 

Hmmmmm. I don't need to define F_CPU, because it still works without it....or am I wrong?

 

// Ravoos

I'm guessing the default value isn't working hence your code not working, DHT11 require pretty specific delays to get the data decoded. If your using an Arduino board chances are that's running at 16MHZ, and the default F_CPU is 1MHZ, meaning your delays will be running 16 times faster then They should be

Last Edited: Fri. Oct 4, 2019 - 03:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Ravoos wrote:

I don't need to define F_CPU, because it still works without it....

Kuch wrote:
I'm guessing the default value isn't working hence your code not working

Hmm ... but in that bit which you quoted, he said it works !

 

I guess it's a bit ambiguous about what he actually meant by "it still works" - since the whole point of the OP is that it does not work!!

 

All rather confusing, really ...

 

frown

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...