Narashiman P
Published © GPL3+

PSoC 4 Interface with DHT11

POC for interfacing with single wire communication protocol based DHT11 and DHT22 devices for logging temperature and Humidity using PSoC 4.

IntermediateProtip1 hour4,549
PSoC 4 Interface with DHT11

Things used in this project

Hardware components

PSoC 4 Prototyping Kit
Cypress PSoC 4 Prototyping Kit
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Perma-Proto Breadboard Half Size
Perma-Proto Breadboard Half Size
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

PSoC Creator
Cypress PSoC Creator
Arduino IDE
Arduino IDE

Story

Read more

Schematics

PSoC 4 and DHT11 communication Schematic

Code

PSoC and DHT interface

C/C++
/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/
#include "project.h"

static int temperature=99;
static int humidity=99;
int DHTread() 
{     
   int i;
   uint8 Istate;
   IState=CyEnterCriticalSection();  
   uint8 bits[5]; 
	uint8 cnt = 7; 
	uint8 idx = 0; 
   int   calc=0; 
   int   timeout=0; 
   for (i=0; i< 5; i++)  
       bits[i] = 0; 
   DHT_Write(0u); 
   CyDelay(19); 
   DHT_Write(1u); 
   while(DHT_Read()==1) 
   { 
       timeout++; 
       if(timeout>500) 
           goto r99;  //DHT error function 
   } 
   while(DHT_Read()==0) 
   {         
       timeout++; 
       if(timeout>500) 
           goto r99; //DHT error function 
   } 
   calc=timeout; 
   timeout=0; 
   while(DHT_Read()==1); 
   for (i=0; i<40; i++) 
	{ 
       timeout=0; 
       while(DHT_Read()==0); 
       while(DHT_Read()==1) 
           timeout++; 
       //Data acquiring point 
       if ((timeout) > (calc/2)) 
           bits[idx] |= (1 << cnt); 
       if (cnt == 0)   // next byte? 
   	{ 
   		cnt = 7;    // restart at MSB 
   		idx++;      // next byte! 
   	} 
   	else cnt--; 
   } 
   humidity    = bits[0];  
   temperature = bits[2];  
   CyExitCriticalSection(IState); 
   CyDelay(1); 
   return 0; 
   r99:    //Goto label for error in DHT reading 
       humidity    = 99;  
       temperature = 99;  
       CyExitCriticalSection(IState); 
       return 99; 
}
int main(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    
    char outputstring[40];
    UART_Start();
    
    for(;;)
    {
        DHTread();
        sprintf(outputstring,"Temperature=%i Humidity= %i \r\n"
                                                      ,temperature, humidity);
        UART_UartPutString(outputstring);
        CyDelay(5000);  //Delay in milli seconds.        
    }
}

/* [] END OF FILE */

PSoC4 interfacing with DHT11

Credits

Narashiman P

Narashiman P

2 projects • 9 followers
Tinkerer, Mechatronics Engineer and Special Purpose Hardware and Software design Engineer.

Comments