Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Anyone know about signal on DHT ?

Status
Not open for further replies.
Guys,

I can see a response from DHT11 on scope already, but why can't I read the bytes properly yet ?

That is impossible to know. Do you realize the bits are probably LSB first going left to right? There is a start bit and probably at least one stop bit. What have you done to try to plot it out?

John
 

I used scope and stopped while it's running,
there are 40 bits exactly what I want but the strange is, the 40 bits data voltage is lower dan the voltage from standard MCU,
I can see MCU generating almost 5V but from the sensor it's only about 2V.....why is it happening ?
thanks
 

It sounds like you have a load on the sensor output, if the sensor is putting out 5V. Are you sure of that? What does the voltage have to do with not being able to read the bits? If you see them, you can read them. Do you mean the MCU is not detecting the bits because the voltage is too low? How are you driving the sensor and what is the input circuit to the MCU?

What MCU, what sensor, what communication protocol, software serial or hardware serial, and so forth? Please post your circuit.

John
 

It's single wire protocol, the MCU is atmega128, sensor DHT11....

The sensor is connected to PD4 with 4K7 pull up resistor

Code:
#define DHT11_DDR DDRD
#define DHT11_PORT PORTD
#define DHT11_PIN PIND
#define DHT11_INPUTPIN PD4
I attach the datasheet and the circuit is exactly just the same with data sheet,

Thanks

- - - Updated - - -

the function :
Code:
/*
 * get data from dht11
 */
uint8_t dht11_getdata(uint8_t select) {
	uint8_t bits[5];
	uint8_t i,j = 0;

	memset(bits, 0, sizeof(bits));

	//reset port
	DHT11_DDR |= (1<<DHT11_INPUTPIN); //output
	DHT11_PORT |= (1<<DHT11_INPUTPIN); //high
	_delay_ms(250);

	//send request
	DHT11_PORT &= ~(1<<DHT11_INPUTPIN); //low
	_delay_ms(18);
	DHT11_PORT |= (1<<DHT11_INPUTPIN); //high
	_delay_us(1);
	DHT11_DDR &= ~(1<<DHT11_INPUTPIN); //input
	_delay_us(39);

	//check start condition 1
	usart_pstr("CONDITION 1");
	if((DHT11_PIN & (1<<DHT11_INPUTPIN))) {
		return DHT11_ERROR;
	}
	_delay_us(80);
	//check start condition 2
	usart_pstr("CONDITION 2");
	if(!(DHT11_PIN & (1<<DHT11_INPUTPIN))) {
		return DHT11_ERROR;
	}
	_delay_us(80);

	//read the data
	for (j=0; j<5; j++) { //read 5 byte
		uint8_t result=0;
		for(i=0; i<8; i++) {//read every bit
			while(!(DHT11_PIN & (1<<DHT11_INPUTPIN))); //wait for an high input
			_delay_us(30);
			if(DHT11_PIN & (1<<DHT11_INPUTPIN)) //if input is high after 30 us, get result
				result |= (1<<(7-i));
			while(DHT11_PIN & (1<<DHT11_INPUTPIN)); //wait until input get low
		}
		bits[j] = result;
	}

	//reset port
	DHT11_DDR |= (1<<DHT11_INPUTPIN); //output
	DHT11_PORT |= (1<<DHT11_INPUTPIN); //low
	_delay_ms(100);

	//check checksum
	if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
		if (select == 0) { //return temperature
			return(bits[2]);
		} else if(select == 1){ //return humidity
			return(bits[0]);
		}
	}

	return DHT11_ERROR;
}
 

Attachments

  • DHT11.pdf
    842.3 KB · Views: 59

That circuit does not show a common ground. That may work fine for wireless "one-wire," but for wired serial you need a reference ground.

John
 

Hi,

Its a long time since I wrote assembly code for the dht11 and I don't do C but looking at your code the 1us delay after the 18ms does not seem right.

Looking back at my old assembler code I also found this C code, no idea where its from, but cannot think I would have bothered to save it if it was not good, so it might help you ..?
Code:
#define DHT11_PIN 0      // ADC0

byte read_dht11_dat()
{
	byte i = 0;
	byte result=0;
	for(i=0; i< 8; i++){
		
		
		while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
		delayMicroseconds(30);
		
		if(PINC & _BV(DHT11_PIN)) 
			result |=(1<<(7-i));
              while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
         
		
	}
	return result;
}


void setup()
{
	DDRC |= _BV(DHT11_PIN);
	PORTC |= _BV(DHT11_PIN);
	
	  Serial.begin(19200);
  
Serial.println("Ready");
	}
	
void loop()
{
	byte dht11_dat[5];
	byte dht11_in;
	byte i;
	// start condition
	// 1. pull-down i/o pin from 18ms     ;     Send 18ms pull down asignal to start dht11 transmission
	PORTC &= ~_BV(DHT11_PIN);
	delay(18);
	PORTC |= _BV(DHT11_PIN);
	delayMicroseconds(40);				;       Wait 40us
	
	DDRC &= ~_BV(DHT11_PIN);			; 		wait another 40us when then test to see if line Low  - respone from dht11
	delayMicroseconds(40);
	
	dht11_in = PINC & _BV(DHT11_PIN);
	
	if(dht11_in){
		Serial.println("dht11 start condition 1 not met");
		return;
	}
	delayMicroseconds(80);
	
	dht11_in = PINC & _BV(DHT11_PIN);
	
	if(!dht11_in){
		Serial.println("dht11 start condition 2 not met");
		return;
	}
	delayMicroseconds(80);
	// now ready for data reception
	for (i=0; i<5; i++)
		dht11_dat[i] = read_dht11_dat();
		
	DDRC |= _BV(DHT11_PIN);
	PORTC |= _BV(DHT11_PIN);
	
        byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
	// check check_sum
	if(dht11_dat[4]!= dht11_check_sum)
	{
		Serial.println("DHT11 checksum error");
	}
	
	Serial.print("Current humdity = ");
	Serial.print(dht11_dat[0], DEC);
	Serial.print(".");
	Serial.print(dht11_dat[1], DEC);
	Serial.print("%  ");
	Serial.print("temperature = ");
	Serial.print(dht11_dat[2], DEC);
	Serial.print(".");
	Serial.print(dht11_dat[3], DEC);
	Serial.println("C  ");
	
	delay(2000);											; delay 2 secs before any re read
}
 

I have changed the delay as suggested but still not giving a right response...
Code:
uint8_t dht11_getdata(uint8_t select) {
	uint8_t bits[5];
	uint8_t i,j = 0;

	memset(bits, 0, sizeof(bits));

	//reset port
	DHT11_DDR |= (1<<DHT11_INPUTPIN); //output
	DHT11_PORT |= (1<<DHT11_INPUTPIN); //high
	_delay_ms(250);

	//send request
	DHT11_PORT &= ~(1<<DHT11_INPUTPIN); //low
	_delay_ms(18);
	DHT11_PORT |= (1<<DHT11_INPUTPIN); //high
	_delay_us(40);
	DHT11_DDR &= ~(1<<DHT11_INPUTPIN); //input
	_delay_us(40);

	//check start condition 1
	//usart_pstr("CONDITION 1");
	if((DHT11_PIN & (1<<DHT11_INPUTPIN))) {
		return DHT11_ERROR;
	}
	_delay_us(80);
	//check start condition 2
	usart_pstr("CONDITION 2");
	if(!(DHT11_PIN & (1<<DHT11_INPUTPIN))) {
		return DHT11_ERROR;
	}
	_delay_us(80);

	//read the data
	usart_pstr("READ DATA");
	for (j=0; j<5; j++) { //read 5 byte
		uint8_t result=0;
		for(i=0; i<8; i++) {//read every bit
			while(!(DHT11_PIN & (1<<DHT11_INPUTPIN))); //wait for an high input
			_delay_us(30);
			if(DHT11_PIN & (1<<DHT11_INPUTPIN)) //if input is high after 30 us, get result
				result |= (1<<(7-i));
			while(DHT11_PIN & (1<<DHT11_INPUTPIN)); //wait until input get low
		}
		bits[j] = result;
	}

	//reset port
	DHT11_DDR |= (1<<DHT11_INPUTPIN); //output
	DHT11_PORT |= (1<<DHT11_INPUTPIN); //low
	_delay_ms(100);

	//check checksum
	if (bits[0] + bits[1] + bits[2] + bits[3] == bits[4]) {
		if (select == 0) { //return temperature
			return(bits[2]);
		} else if(select == 1){ //return humidity
			return(bits[0]);
		}
	}

	return DHT11_ERROR;
}

on main:
Code:
                _delay_ms(11000);
                humidity = dht11_gethumidity();
		temperature = dht11_gettemperature();
		
		usart_pstr("Humidity:");
		usart_pstr(humidity);
        usart_pstr("\n\n");
		usart_pstr("Temperature:");
		usart_pstr(temperature);
		sprintf( &temp_char, " \n\n Temp : %u *C ", temperature );
		usart_pstr( &temp_char );
		usart_pstr("\n\n");
		
		if (humidity != -1)
		{
			usart_pstr("Humidity valid");
			NEXT_LINE;
			break;
		}
		else
		{
			usart_pstr("Humidity invalid");
			NEXT_LINE;
		    break;
		}

response on uart :
DHT TEST

Humidity:Ä

Temperature:Ï

Temp : 65535 *C

Humidity invalid

- - - Updated - - -

Code:
int8_t dht11_gettemperature() {
	uint8_t ret = dht11_getdata(0);
	if(ret == DHT11_ERROR)
		return -1;
	else
		return ret;
}

/*
 * get humidity (20..90%)
 */
int8_t dht11_gethumidity() {
	uint8_t ret = dht11_getdata(1);
	if(ret == DHT11_ERROR)
		return -1;
	else
		return ret;
}

- - - Updated - - -

When I connected to logic analyzer, I got no response ?????
dht11 logic1.jpg

- - - Updated - - -

it's not getting the response from DHT11 properly,

I tested with :
Code:
	DHT11_DDR &= ~(1<<DHT11_INPUTPIN); //input
 if(bit_is_set(DHT11_PIN,DHT11_INPUTPIN))
	{
		usart_pstr("PD4 INPUT = SET");
	}

Even I put PD4 = HIGH, I never got "PD4 INPUT = SET".....any clues ??
Code:
//setup parameters
#define DHT11_DDR DDRD
#define DHT11_PORT PORTD
#define DHT11_PIN PIND
#define DHT11_INPUTPIN PD4

- - - Updated - - -

it's stuck on:
Code:
//check start condition 1
	usart_pstr("CONDITION 1 \n");
	if((DHT11_PIN & (1<<DHT11_INPUTPIN))) {
		return DHT11_ERROR;
	}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top