DHT11 isn't responding.

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

Hello everyone! I try to find a mistake already 3 days, re-read the code and the datasheet, but all for nothing. Please, help me find the error. As for the LCD library, I'm sure, because I use it in other projects.

 

DHT11.c:

#include "DHT11.h"

void DHT11_Init (void)
{
	_delay_ms(1500);
}

void DHT11_ResetPort (void)
{
	DHT11_DDR |= 1 << DHT11_PIN;	//	Output
	DHT11_PORT |= 1 << DHT11_PIN;	//	High
	_delay_ms(77);
}

uint8_t DHT11_ReceiveData (uint8_t* data)
{
	DHT11_ResetPort();

	DHT11_PORT &= ~(1 << DHT11_PIN);
	_delay_ms(18);

	DHT11_PORT |= 1 << DHT11_PIN;
	DHT11_DDR &= ~(1 << DHT11_PIN);
	_delay_us(40);

	if ((DHT11_PINS & (1 << DHT11_PIN)))	return DHT11_ERROR;
	_delay_us(80);
	if (!(DHT11_PINS & (1 << DHT11_PIN)))	return DHT11_ERROR;
	while (DHT11_PINS & (1 << DHT11_PIN));
// 	_delay_us(80);

	for (uint8_t i = 0; i < 5; i++)
	{
		data[i] = 0;
		for (int8_t j = 7; j >= 0; j--)
		{
			cli();

			while (!(DHT11_PINS & (1 << DHT11_PIN)));
			_delay_us(33);
			if (DHT11_PINS & (1 << DHT11_PIN))  data[i] |= 1 << j;
			while (DHT11_PINS & (1 << DHT11_PIN));

			sei();
		}
	}

	if (data[0] + data[1] + data[2] + data[3] == data[5])	return (DHT11_SUCCESS);
	return (DHT11_ERROR);
}

main.c:

#include "main.h"

void IO_Init (void)
{

}

void setup (void)
{
	IO_Init();
	LCD_Init();
	DHT11_Init();

	sei();
}

int main (void)
{
	setup();

	uint8_t meteoData[4];

	while (1)
	{
		LCD_SetPosition(1,1);

		if (DHT11_ReceiveData(meteoData))
		{
			LCD_PutByte((meteoData[2] / 10) + ASCII_NULL);
			LCD_PutByte((meteoData[2] % 10) + ASCII_NULL);
		}
		else	LCD_SendString((int8_t* ) "Error");

		_delay_ms(1000);
	}
}

 

Attachment(s): 

Ruslan

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

D-503 wrote:
As for the LCD library, I'm sure, because I use it in other projects

Don't just assume - make a test to actually prove this!

 

It's more helpful to embed you schematic so that we can actually see it in the post - like this:

 

Instructions to do this are here: https://www.avrfreaks.net/comment...

 

So that looks like a Proteus simulation - is it?

 

If it is, then use the simulator to step through the code & see what's happening.

 

The schematic shows as ATMega328P; it has on-board debug - so, again, use it to step through the code & see what's happening.

 

There's always the possibility that the DHT11 is faulty or damaged - have you checked?

 

Do you have access to a logic analyser or oscilloscope?

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

Ok, I prove, that the LCD is work.

 

#include "main.h"

void IO_Init (void)
{
	
}

void setup (void)
{
	IO_Init();
	LCD_Init();
	DHT11_Init();
	
	sei();
}

int main (void)
{
	setup();
	
	//uint8_t meteoData[4];
	int8_t a = 51;
	LCD_PutByte(a / 10 + ASCII_NULL);
	LCD_PutByte(a % 10 + ASCII_NULL);
	
	while (1)
	{
// 		LCD_SetPosition(1,1);
// 		
// 		if (DHT11_ReceiveData(meteoData))
// 		{
// 			LCD_PutByte((meteoData[2] / 10) + ASCII_NULL);
// 			LCD_PutByte((meteoData[2] % 10) + ASCII_NULL);
// 		}
// 		else	LCD_SendString((int8_t* ) "Error");
// 		
// 		
// 		_delay_ms(1000);
	}
}

 

Ruslan

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

Shouldn't the test in DHT11_ReceiveData be:

if (data[0] + data[1] + data[2] + data[3] == data[4])	return (DHT11_SUCCESS);

???

David (aka frog_jr)

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

In one of the first page search hits for "Arduino DHT11 library" I find this: (https://github.com/nicolsc/ardui...)

enum ReadStatus {
    OK,
    ERROR_CHECKSUM,
    ERROR_TIMEOUT,
};

Why not implement these separate error codes so you can get better visibility of where your code fails ?

 

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

I fixed, thank you, but it is still doesn't work.

Ruslan

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

 

A general tip for you.

 

Rather than commenting-out stuff like this:

D-503 wrote:

	while (1)
	{
// 		LCD_SetPosition(1,1);
//
// 		if (DHT11_ReceiveData(meteoData))
// 		{
// 			LCD_PutByte((meteoData[2] / 10) + ASCII_NULL);
// 			LCD_PutByte((meteoData[2] % 10) + ASCII_NULL);
// 		}
// 		else	LCD_SendString((int8_t* ) "Error");
//
//
// 		_delay_ms(1000);
	}
}

 

It's a lot easier to use a #if - like this:

	while (1)
	{
#if 0
 		LCD_SetPosition(1,1);

 		if (DHT11_ReceiveData(meteoData))
 		{
 			LCD_PutByte((meteoData[2] / 10) + ASCII_NULL);
 			LCD_PutByte((meteoData[2] % 10) + ASCII_NULL);
 		}
 		else	LCD_SendString((int8_t* ) "Error");


 		_delay_ms(1000);
#endif
	}

 

Or even

	while (1)
	{
#ifdef DISABLE_DHT11
 		LCD_SetPosition(1,1);

 		if (DHT11_ReceiveData(meteoData))
 		{
 			LCD_PutByte((meteoData[2] / 10) + ASCII_NULL);
 			LCD_PutByte((meteoData[2] % 10) + ASCII_NULL);
 		}
 		else	LCD_SendString((int8_t* ) "Error");


 		_delay_ms(1000);
#endif
	}

 

Which makes it easier to disable code in multiple places; eg

void setup (void)
{
	IO_Init();
	LCD_Init();

#ifdef DISABLE_DHT11
	DHT11_Init();
#endif
	
	sei();
}

int main (void)
{
	setup();
	
	//uint8_t meteoData[4];
	int8_t a = 51;
	LCD_PutByte(a / 10 + ASCII_NULL);
	LCD_PutByte(a % 10 + ASCII_NULL);
	
	while (1)
	{
#ifdef DISABLE_DHT11
 		LCD_SetPosition(1,1);
 		
 		if (DHT11_ReceiveData(meteoData))
 		{
 			LCD_PutByte((meteoData[2] / 10) + ASCII_NULL);
 			LCD_PutByte((meteoData[2] % 10) + ASCII_NULL);
 		}
 		else	LCD_SendString((int8_t* ) "Error");
 		
 		
 		_delay_ms(1000);
#endif
	}
}

 

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: 1

D-503 wrote:
I fixed, thank you, but it is still doesn't work.
That really tells us nothing...

The DHT11_ReceiveData function must not be returning a TRUE value since you are seeing "Error".

We do not know how DHT11_SUCCESS and DHT11_ERROR are defined, and it would be helpful if you used the debugger to step through the code to see the values of data[0], data[1], data[2], data[3] & data[4] when you are making the determination to return SUCCESS or ERROR.

David (aka frog_jr)

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

and see #5 - have your code distinguish different types of error ...

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

Solved one and half month ago. I played with the timings and it all worked. Only the sensor gives the strange values for humidity. And it isn't very precise in principle.

Ruslan