This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

DHT11 1-Wire Interfacing with MSP430G2553 using bit banging

Hi,

 Just after wondering, about how to interface the DHT 11 1 wire humidity sensor with the MSP430 G2xx series, I have done this successfully on G2 LP with the flavor MSP430G2553IN20 using bit banging. I am describing the s/w with the usage here, you can modify it accordingly to suit it for other 1 wire slaves!

Please keep the following in mind:

1. The slave will be powered at the datasheet specified voltage level may be other than  the master. Here, DHT is connected to 5V and the MSP is on the 3V3

2. Connect the pull up resistor of 5K on the Data line

3. Please ensure you have the capacitor connected to avoid any voltage dropouts

4. For DHT11, you have to allow sufficient gaps before you can re initiate another set of data from the sensor. please go through the datasheet of the slave thoroughly before you use it.

HERE IS THE CONNECTIONS (HARD WIRE SIDE):

Here is the small code snippet that you can simply insert inside your code. Need not create a separate header or source file.

//////////////////////////DHT11 handler/////////////////////////////////////////

bool dht11(void)
{
volatile int elapsed_time=0,bit_cnt;
volatile unsigned char temp;
bool data_validity = false;
P1DIR |= DHT11; // Makes the pin OUTput
P1OUT &= ~DHT11;// Start condition generation for DHT11
_delay_cycles(2000); // delays 1 milisecond for each scan
P1OUT |= DHT11;// Start condition generation for DHT11
P1DIR &= ~DHT11; // Makes the pin input
P1IFG &= ~DHT11; // Clears pin flag, if high
while((P1IN&DHT11)==DHT11); // wait till the slave puls the pin low
///////////////// DHT11 has responded /////////////////////////////
do {
elapsed_time++; // check if the elapsed time = 80 uS
} while ((P1IN&DHT11)==0);
if(elapsed_time <=10)
{
elapsed_time = 0;
do {
elapsed_time++; // check if the elapsed time = 80 uS
} while ((P1IN&DHT11)==DHT11);
if(elapsed_time <=10)// check if the elapsed time = 80 uS
{
///////// humidity integer/////////////
for(bit_cnt=0;bit_cnt <8;bit_cnt++) // for humidity first byte
{
while ((P1IN&DHT11)==0); // skip the lower 50 uS part
elapsed_time = 0;
do {
elapsed_time++; // check if the elapsed time = 80 uS
} while ((P1IN&DHT11)==DHT11);
if(elapsed_time >5)
{
hum_int |= 0x01;
hum_int <<= 1;
}
else
{
hum_int &= ~0x01;
hum_int <<= 1;
}
}
///////// humidity decimal/////////////
for(bit_cnt=0;bit_cnt <8;bit_cnt++) // for humidity first byte
{
while ((P1IN&DHT11)==0); // skip the lower 50 uS part
elapsed_time = 0;
do {
elapsed_time++; // check if the elapsed time = 80 uS
} while ((P1IN&DHT11)==DHT11);
if(elapsed_time >5)
{
hum_decimals |= 0x01;
hum_decimals <<= 1;
}
else
{
hum_decimals &= ~0x01;
hum_decimals <<= 1;
}
}
///////// temperature integer/////////////
for(bit_cnt=0;bit_cnt <8;bit_cnt++) // for humidity first byte
{
while ((P1IN&DHT11)==0); // skip the lower 50 uS part
elapsed_time = 0;
do {
elapsed_time++; // check if the elapsed time = 80 uS
} while ((P1IN&DHT11)==DHT11);
if(elapsed_time >5)
{
tmp_int |= 0x01;
tmp_int <<= 1;
}
else
{
tmp_int &= ~0x01;
tmp_int <<= 1;
}
}
///////// temperature decimal/////////////
for(bit_cnt=0;bit_cnt <8;bit_cnt++) // for humidity first byte
{
while ((P1IN&DHT11)==0); // skip the lower 50 uS part
elapsed_time = 0;
do {
elapsed_time++; // check if the elapsed time = 80 uS
} while ((P1IN&DHT11)==DHT11);
if(elapsed_time >5)
{
tmp_decimals |= 0x01;
tmp_decimals <<= 1;
}
else
{
tmp_decimals &= ~0x01;
tmp_decimals <<= 1;
}
}
/////////Parity/////////////
for(bit_cnt=0;bit_cnt <8;bit_cnt++) // for humidity first byte
{
while ((P1IN&DHT11)==0); // skip the lower 50 uS part
elapsed_time = 0;
do {
elapsed_time++; // check if the elapsed time = 80 uS
} while ((P1IN&DHT11)==DHT11);
if(elapsed_time >5)
{
parity |= 0x01;
parity <<= 1;
}
else
{
parity &= ~0x01;
parity <<= 1;
}
}
}

}
parity_calc = hum_int+hum_decimals+tmp_int+tmp_decimals;
// if(hum_int<=0x4C)
// {
// humidity_low= true;
// }
// else
// {
// humidity_high= true;
// }
if(parity_calc == parity)
{
data_validity = true;

}
return data_validity;

}

//////////////////////////////////////////////////////////////////////////////////////////

Declare the following variables as given below:

#define DHT11 BIT3

unsigned char hum_int,hum_decimals,tmp_int,tmp_decimals,parity,parity_calc;

///////////////////////////////////////////////////////////////////////////////////////////

I hope the instructions are quite self explanatory. the return data_validity checks the checksum and returns a "true" when the checksum matches.

  • TURJASU PYNE1,

    Thank you for posting the results of your project, I'm sure it will be helpful for other engineers trying to design 1-wire applications using the MSP430. One quick note: operating a GPIO line at 5 V, even if through a 5 kOhm pull-up resistor, is outside of the MSP430 datasheet's specification. I would recommend operating both devices at 3.3 V since the DHT11s power supply range is 3.3 to 6 V. It would also be helpful if you listed the value of the capacitor you used in your tests and what range you recommend for proper operation.

    Regards,
    Ryan

**Attention** This is a public forum