EmbDev.net

Forum: µC & Digital Electronics Humidity Sensor with DHT11 and PIC16F676


von Dominic (oepfel)



Rate this post
useful
not useful
Hello Guys

I am trying to develop a humidity sensor with DHT11 for my bathroom. The 
DHT11 Sensor reads the humidity and sends it to the PIC, if the humidity 
is over 75% a LED should light up. If its under 75% the LED is off.

But I have two problems.

First of all I am programming the PIC16F676 with a PICKIT3 clone - and 
when trying to debug my code the following error appears in MPLAB (1) 
Error when trying to debug:
)

_But now to my project:_

If I upload (2)code without error-search), nothing really happens with 
the LED, when the humidity rises. Even if I change line 46 to "if ( 
int_RH > 0 )". This is probably the case because line 46 never happens. 
Thats because result isn't eaqual to 0.

So i tried to find out, where in the function "uint8_t Read_DHT ( 
uint8_t *data )" the problem happens.

And after changing the code to (3) Code with error-search) i found out, 
that the problem is due to the timeout in line 118
1
    uint16_t timeout = 10000;
2
    while ( SENSOR == 1 )
3
    {
4
        if ( --timeout == 0 )
5
        {
6
            return 0x0F;
7
        }
8
    }

*Interesting:*
If I upload the same code to a programming board with the PIC18F45K22 
and add the DHT11 Sensor, everything works as it should. Sadly I am not 
able to use this programming board for my project. Therefore I would 
like to do it with the PIC16F676.

Have you guys any idea, how i can fix this problem?

If you need to know anything about the circuit, I added the schmetic to 
the attachment.


 + I am new to the forum therefore I apologise if I forgot to add 
something you should know.



1) Error when trying to debug:
1
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
2
make  -f nbproject/Makefile-default.mk dist/default/debug/test.X.debug.elf
3
make[2]: Entering directory 'C:/Users/domin/Desktop/ueK_6/test.X'
4
"C:\Program Files\Microchip\xc8\v2.46\bin\xc8-cc.exe"  -mcpu=16F676 -c  -D__DEBUG=1  -mdebugger=pickit3   -mdfp="C:/Program Files/Microchip/MPLABX/v6.20/packs/Microchip/PIC16Fxxx_DFP/1.6.156/xc8"  -fno-short-double -fno-short-float -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-2 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mdefault-config-bits   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/main.p1 main.c 
5
::: advisory: (2100) using the C99 standard library with the selected device may result in larger code and data usage
6
make[2]: Leaving directory 'C:/Users/domin/Desktop/ueK_6/test.X'
7
make[2]: Entering directory 'C:/Users/domin/Desktop/ueK_6/test.X'
8
"C:\Program Files\Microchip\xc8\v2.46\bin\xc8-cc.exe"  -mcpu=16F676 -c  -D__DEBUG=1  -mdebugger=pickit3   -mdfp="C:/Program Files/Microchip/MPLABX/v6.20/packs/Microchip/PIC16Fxxx_DFP/1.6.156/xc8"  -fno-short-double -fno-short-float -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-2 -Wa,-a -DXPRJ_default=default  -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mdefault-config-bits   -std=c99 -gdwarf-3 -mstack=compiled:auto:auto     -o build/default/debug/configuration_bits.p1 configuration_bits.c 
9
::: advisory: (2100) using the C99 standard library with the selected device may result in larger code and data usage
10
make[2]: Leaving directory 'C:/Users/domin/Desktop/ueK_6/test.X'
11
make[2]: *** [nbproject/Makefile-default.mk:143: dist/default/debug/test.X.debug.elf] Error 1
12
make[1]: *** [nbproject/Makefile-default.mk:85: .build-conf] Error 2
13
make: *** [nbproject/Makefile-impl.mk:39: .build-impl] Error 2
14
make[2]: Entering directory 'C:/Users/domin/Desktop/ueK_6/test.X'
15
"C:\Program Files\Microchip\xc8\v2.46\bin\xc8-cc.exe"  -mcpu=16F676 -Wl,-Map=dist/default/debug/test.X.debug.map  -D__DEBUG=1  -mdebugger=pickit3  -DXPRJ_default=default  -Wl,--defsym=__MPLAB_BUILD=1   -mdfp="C:/Program Files/Microchip/MPLABX/v6.20/packs/Microchip/PIC16Fxxx_DFP/1.6.156/xc8"  -fno-short-double -fno-short-float -fasmfile -maddrqual=ignore -xassembler-with-cpp -mwarn=-2 -Wa,-a -msummary=-psect,-class,+mem,-hex,-file  -ginhx32 -Wl,--data-init -mno-keep-startup -mno-osccal -mno-resetbits -mno-save-resetbits -mno-download -mno-stackcall -mdefault-config-bits -std=c99 -gdwarf-3 -mstack=compiled:auto:auto         -Wl,--memorysummary,dist/default/debug/memoryfile.xml -o dist/default/debug/test.X.debug.elf  build/default/debug/main.p1 build/default/debug/configuration_bits.p1     
16
::: advisory: (2100) using the C99 standard library with the selected device may result in larger code and data usage
17
main.c:177:: error: (800) undefined symbol "btemp"
18
(908) exit status = 1
19
make[2]: Leaving directory 'C:/Users/domin/Desktop/ueK_6/test.X'
20
21
BUILD FAILED (exit value 2, total time: 1s)



2) Code without error-search:
1
// -- INCLUDES ----------------------------------------------------------------
2
#include <xc.h>        // Compiler-Include (Port-Defines)
3
#include <stdbool.h>  // Include for data type 'bool' and keywords 'true' and 'false'
4
#include <stdint.h>   // Include for defines the standard integer types per ISO/IEC 9899:1999
5
6
// -- DEFINES -----------------------------------------------------------------
7
#define _XTAL_FREQ 10000000 // Crystal oscillator frequencies of 10Mhz
8
#define LOOP_DELAY 1000  // Loop-Delay in ms 
9
10
#define WARN_LED PORTCbits.RC2
11
12
#define SENSOR PORTAbits.RA2
13
#define SENSOR_TRIS TRISAbits.TRISA2
14
15
// -- FUNCTIONS-PROTOTYPE ----------------------------------------------------
16
uint8_t Read_DHT ( uint8_t *data );
17
18
// -- SETUP -------------------------------------------------------------------
19
20
void setup ( void )
21
{
22
    ANSEL = 0x00;
23
    TRISC = 0x00;
24
25
    SENSOR_TRIS = 1;
26
}
27
28
// -- LOOP ---------------------------------------------------------
29
30
void loop ( void )
31
{
32
33
34
    static uint8_t data[5] = { 0 };
35
36
    uint8_t result = Read_DHT( data );
37
    
38
    if ( result == 0 )
39
    {
40
        uint8_t int_RH = data[0];
41
        uint8_t dec_RH = data[1];
42
        uint8_t int_T = data[2];
43
        uint8_t dec_T = data[3];
44
45
46
        if ( int_RH > 75 )
47
        {
48
            WARN_LED = 1;
49
        }
50
        else
51
        {
52
            WARN_LED = 0;
53
        }
54
    }
55
}
56
57
// -- main -----------------------------------------------------------
58
59
void main ( void )
60
{
61
    setup( );
62
63
    // (Loop)
64
    while ( 1 )
65
    {
66
        loop( );
67
        __delay_ms( LOOP_DELAY );
68
    }
69
}
70
71
// -- FUNCTIONS --------------------------------------------------------------
72
73
uint8_t Read_DHT ( uint8_t *data )
74
{
75
76
77
    // SENSOR as OUTPUT
78
    SENSOR_TRIS = 0;
79
    SENSOR = 0;
80
    //wait 18ms
81
82
    __delay_ms( 18 );
83
84
    SENSOR = 1;
85
86
87
    __delay_us( 40 );
88
89
    // SENSOR as INPUT    
90
    SENSOR = 0;
91
92
    SENSOR_TRIS = 1;
93
94
    uint16_t timeout = 10000;
95
    while ( SENSOR == 1 )
96
    {
97
        if ( --timeout == 0 )
98
        {
99
            return 0x0F;
100
        }
101
    }
102
103
    timeout = 10000;
104
    while ( SENSOR == 0 )
105
    {
106
        if ( --timeout == 0 )
107
        {
108
            return 0xFF;
109
        }
110
    }
111
112
    timeout = 10000;
113
    while ( SENSOR == 1 )
114
    {
115
        if ( --timeout == 0 )
116
        {
117
            return 0xFF;
118
        }
119
    }
120
121
    for ( uint8_t i = 0; i < 5; i++ )
122
    {
123
        for ( int8_t j = 7; j >= 0; j-- )
124
        {
125
126
            timeout = 10000;
127
            while ( SENSOR == 0 )
128
            {
129
                if ( --timeout == 0 )
130
                {
131
                    return 0xFF;
132
                }
133
            }
134
            uint8_t signal_counter = 0;
135
            timeout = 10000;
136
            while ( SENSOR == 1 )
137
            {
138
                signal_counter++;
139
                __delay_us( 1 );
140
                if ( signal_counter > 100 )
141
                {
142
                    return 0xFE;
143
                }
144
            }
145
146
147
148
            if ( signal_counter > 30 )
149
            {
150
                data[i] |= (1 << j);
151
            }
152
        }
153
154
    }
155
    if ( ( uint8_t ) (data[0] + data[1] + data[2] + data[3]) != data[4] )
156
    {
157
        return 0xFF;
158
    }
159
160
    return 0;
161
162
163
}


3) Code with error-search
1
// -- INCLUDES ----------------------------------------------------------------
2
#include <xc.h>        // Compiler-Include (Port-Defines)
3
#include <stdbool.h>  // Include for data type 'bool' and keywords 'true' and 'false'
4
#include <stdint.h>   // Include for defines the standard integer types per ISO/IEC 9899:1999
5
6
// -- DEFINES -----------------------------------------------------------------
7
#define _XTAL_FREQ 10000000 // Crystal oscillator frequencies of 10Mhz
8
#define LOOP_DELAY 1000  // Loop-Delay in ms 
9
10
#define WARN_LED PORTCbits.RC2
11
12
#define SENSOR PORTAbits.RA2
13
#define SENSOR_TRIS TRISAbits.TRISA2
14
15
// -- FUNCTIONS-PROTOTYPE ----------------------------------------------------
16
uint8_t Read_DHT ( uint8_t *data );
17
18
// -- SETUP -------------------------------------------------------------------
19
20
void setup ( void )
21
{
22
    ANSEL = 0x00;
23
    TRISC = 0x00;
24
25
    SENSOR_TRIS = 1;
26
}
27
28
// --- loop ---------------------------------------------------------
29
30
void loop ( void )
31
{
32
33
34
    static uint8_t data[5] = { 0 };
35
36
    uint8_t result = Read_DHT( data );
37
    // Error 1
38
        if ( result == 0xFF )
39
        {
40
            while ( 1 )
41
            {
42
                    WARN_LED = 1;
43
                    __delay_ms( 2000 );
44
                    WARN_LED = 0;
45
                    __delay_ms( 2000 );
46
            }
47
        }
48
    
49
    // Error 2
50
        if ( result == 0x0F )
51
        {
52
            while ( 1 )
53
            {
54
                    WARN_LED = 1;
55
                    __delay_ms( 200 );
56
                    WARN_LED = 0;
57
                    __delay_ms( 200 );
58
            }
59
        }
60
61
    if ( result == 0 )
62
    {
63
        uint8_t int_RH = data[0];
64
        uint8_t dec_RH = data[1];
65
        uint8_t int_T = data[2];
66
        uint8_t dec_T = data[3];
67
68
69
        if ( int_RH > 75 )
70
        {
71
            WARN_LED = 1;
72
        }
73
        else
74
        {
75
            WARN_LED = 0;
76
        }
77
    }
78
}
79
80
// -- main -----------------------------------------------------------
81
82
void main ( void )
83
{
84
    setup( );
85
86
    // (Loop)
87
    while ( 1 )
88
    {
89
        loop( );
90
        __delay_ms( LOOP_DELAY );
91
    }
92
}
93
94
// -- FUNCTIONS --------------------------------------------------------------
95
96
uint8_t Read_DHT ( uint8_t *data )
97
{
98
99
100
    // SENSOR as OUTPUT
101
    SENSOR_TRIS = 0;
102
    SENSOR = 0;
103
    //wait 18ms
104
105
    __delay_ms( 18 );
106
107
    SENSOR = 1;
108
109
110
    __delay_us( 40 );
111
112
    // SENSOR as INPUT    
113
    SENSOR = 0;
114
115
    SENSOR_TRIS = 1;
116
117
    uint16_t timeout = 10000;
118
    while ( SENSOR == 1 )
119
    {
120
        if ( --timeout == 0 )
121
        {
122
            return 0x0F;
123
        }
124
    }
125
126
    timeout = 10000;
127
    while ( SENSOR == 0 )
128
    {
129
        if ( --timeout == 0 )
130
        {
131
            return 0xFF;
132
        }
133
    }
134
135
    timeout = 10000;
136
    while ( SENSOR == 1 )
137
    {
138
        if ( --timeout == 0 )
139
        {
140
            return 0xFF;
141
        }
142
    }
143
144
    for ( uint8_t i = 0; i < 5; i++ )
145
    {
146
        for ( int8_t j = 7; j >= 0; j-- )
147
        {
148
149
            timeout = 10000;
150
            while ( SENSOR == 0 )
151
            {
152
                if ( --timeout == 0 )
153
                {
154
                    return 0xFF;
155
                }
156
            }
157
            uint8_t signal_counter = 0;
158
            timeout = 10000;
159
            while ( SENSOR == 1 )
160
            {
161
                signal_counter++;
162
                __delay_us( 1 );
163
                if ( signal_counter > 100 )
164
                {
165
                    return 0xFE;
166
                }
167
            }
168
169
170
171
            if ( signal_counter > 30 )
172
            {
173
                data[i] |= (1 << j);
174
            }
175
        }
176
177
    }
178
    if ( ( uint8_t ) (data[0] + data[1] + data[2] + data[3]) != data[4] )
179
    {
180
        return 0xFF;
181
    }
182
183
    return 0;
184
185
186
}

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.