MCU + DHT11 temperature and humidity detection program

Publisher:数字思维Latest update time:2019-11-18 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Based on the DHT11 temperature and humidity alarm system LCD1602 display operation instructions

This design system is a DHT11 temperature and humidity alarm system LCD1602 display, which can set the high temperature alarm value and low temperature alarm value of temperature, the high humidity alarm value and low temperature alarm value of humidity. When there is an alarm, the buzzer will emit a dripping alarm sound, and the green light-emitting diode will flash at the same time. The alarm information can also be seen very intuitively on the LCD1602 liquid crystal. It also has a power-off save function. The data is saved in the EEPOM inside the microcontroller. After entering the setting interface, if no key is pressed, the system will automatically exit the setting interface after 30 seconds. The humanized key setting, the key also has the function of continuous addition and subtraction. When the key is pressed, there is a key sound, that is, the buzzer calls. The key sound can be set in the menu, and the key sound can be set to turn on or off. The system also has a function to cancel the alarm return difference value to increase the anti-interference of the system.

Alarm mode: sound and light alarm    
buzzer drips alarm sound, light-emitting diode flashes.
DHT11 temperature and humidity detection

microcontroller circuit schematic diagram:

0.png 

Buzzer Buzzer B1
10uF capacitor C1
20pF capacitor C2, C3
D indicator D1, D2, D3, D4,
LCD1602 LCD1
9012 transistor Q1
10K resistor R3, R4
2K resistor R1
2K resistor R2, R5
SW-PB button S1, S2, S3, S4, S5
sw-gray power switch SW1
U1 MCU U1
DHT11 temperature and humidity sensor U2
12M crystal oscillator Y1

MCU source program is as follows:

#include //Call the MCU header file

#define uchar unsigned char //unsigned character macro definition variable range 0~255

#define uint unsigned int //Unsigned integer macro definition variable range 0~65535


sbit beep = P1^4; //Buzzer


int flag_300ms;


// Definition of IO variables for buttons

uchar key_can; //Key value variable


sbit wh_led = P2^2; // Temperature alarm indicator IO port definition

sbit wl_led = P2^3; // Temperature alarm indicator IO port definition

sbit sh_led = P2^1; //Humidity alarm indicator IO port definition

sbit sl_led = P2^0; //Humidity alarm indicator IO port definition


uchar flag_en = 1;


uchar menu_1; //Menu design variables

uint t_high = 35, t_low = 10; //temperature alarm parameters

uint s_high = 80,s_low = 10; //Humidity alarm parameters


/*******************************************************************

* Name: delay_1ms()

* Function: Delay 1ms function

* Input: q

* Output: None

********************************************************************/

void delay_1ms(uint q)

{

        uint i,j;

        for(i=0;i                for(j=0;j<120;j++);

}


sbit dht11=P1^5; //Temperature sensor IO port definition

uchar table_dht11[5]={1,2,3,4,5};


uchar code table_num[]="0123456789abcdefg";


sbit rs=P1^0; //Register selection signal H: data register L: instruction register

sbit rw=P1^1; //Register selection signal H: data register L: instruction register

sbit e =P1^2; //Chip select signal falling edge trigger


/********************************************************************

* Name: delay_uint()

* Function: small delay.

* Input: None

* Output: None

***************************************************************************/

void delay_uint(uint q)

{

        while(q--);

}


/********************************************************************

* Name: write_com(uchar com)

* Function: 1602 command function

* Input: Input command value

* Output: None

***************************************************************************/

void write_com(uchar com)

{

        e=0;

        rs=0;

        rw=0;

        P0=com;

        delay_uint(25);

        e=1;

        delay_uint(100);

        e=0;

}


/********************************************************************

* Name: write_data(uchar dat)

* Function: 1602 write data function

* Input: Data to be written to 1602

* Output: None

***************************************************************************/

void write_data(uchar dat)

{

        e=0;

        rs=1;

        rw=0;

        P0=dat;

        delay_uint(25);

        e=1;

        delay_uint(100);

        e=0;        

}



/***********************LCD1602 displays specific characters****0XDF************************/

void write_zifu(uchar hang,uchar add,uchar date)

{

        if(hang==1)   

                write_com(0x80+add);

        else

                write_com(0x80+0x40+add);

        write_data(date);        

}


/***********************LCD1602 displays two decimal numbers************************/

void write_sfm2(uchar hang,uchar add,uint date)

{

        if(hang==1)   

                write_com(0x80+add);

        else

                write_com(0x80+0x40+add);

        write_data(0x30+date/10%10);

        write_data(0x30+date%10);        

}


/**************************LCD1602 displays this character function****************************/

void write_string(uchar hang,uchar add,uchar *p)

{

        if(hang==1)   

                write_com(0x80+add);

        else

                write_com(0x80+0x40+add);

                while(1)                                                                                                                 

                {

                        if(*p == '') break;

                        write_data(*p);

                        p++;

                }        

}


/***********************lcd1602 initialization settings****************************/

void init_1602() //lcd1602 initialization

{

        write_com(0x38);        

        write_com(0x0c);

        write_com(0x06);

        delay_uint(1000);

        write_string(1,0,"Wd:00 H00 L00 ");        

        write_string(2,0,"Sd:00% H00% L00% ");        

        write_zifu(1,5,0xdf); //display degree                

        write_zifu(1,10,0xdf); //display degree        

        write_zifu(1,15,0xdf); //display degree        

        write_sfm2(1,3,table_dht11[2]); //Display temperature

        write_sfm2(2,3,table_dht11[0]); //Display humidity        

        write_sfm2(1,7,t_high); //Display temperature

        write_sfm2(2,7,s_high); //Display humidity                

        write_sfm2(1,10,t_low); //Display temperature

        write_sfm2(2,10,s_low); //Display humidity                

}


/************************Independent key program*****************/

uchar key_can; //key value


void key() //Independent key program

{

        static uchar key_new;

        key_can = 20; //key value restoration

        P3 |= 0xf0;

        if((P3 & 0xf0) != 0xf0) //button pressed

        {

                delay_1ms(1); //key debounce

                if(((P3 & 0xf0) != 0xf0) && (key_new == 1))

                { //Confirm that the button was pressed

                        key_new = 0;

                        switch(P3 & 0xf0)

                        {

                                case 0xd0: key_can = 1; break; //Get the k1 key value

                                case 0xb0: key_can = 2; break; //Get the K2 key value

                                case 0x70: key_can = 3; break; //Get the k3 key value

                        }

                }                        

        }

        else 

                key_new = 1;        

}



/********************Button display function***************/

void key_with()

{

        if(key_can == 1) //Set key

        {

                menu_1++;

                if(menu_1 > 2)

                {

                        menu_1 = 0;

                        init_1602(); //lcd1602 initialization                        

                }

        }

        if(menu_1 == 1) //Set high temperature alarm

        {

                if(key_can == 2)

                {

                        t_high ++; //Set the high temperature value plus 1         

                        if(t_high > 99)

                                t_high = 99;

                }

                if(key_can == 3)

                {

                        t_high -- ; //Set the high temperature value minus 1         

                        if(t_high <= t_low)

                                t_high = t_low + 1;

                }

                write_sfm2(1,8,t_high); //Display temperature

                write_com(0x80+8); //Move the cursor to the second position

                write_com(0x0f); //Display the cursor and blink        

        }        

        if(menu_1 == 2) //Set low temperature alarm

        {

                if(key_can == 2)

                {

                        t_low ++; //Set the low temperature value plus 1 

                        if(t_low >= t_high)

                                t_low = t_high - 1;

                }

                if(key_can == 3) //Set the high temperature value minus 1 

                {

                        t_low -- ;                        

                        if(t_low <= 1)

                                t_low = 1;

                }

                write_sfm2(1,13,t_low); //Display humidity        

                write_com(0x80+13); //Move the cursor to the second position

                write_com(0x0f); //Display the cursor and blink        

        }

        if(menu_1 == 3) //Set high humidity alarm

        {

                if(key_can == 2)

                {

                        s_high ++ ; //Set the high humidity value plus 1         

                        if(s_high > 99)

                                s_high = 99;

                }

                if(key_can == 3)

                {

                        s_high -- ; //Set the high humidity value minus 1         

                        if(s_high <= s_low)

                                s_high = s_low + 1;

                }

                write_sfm2(2,8,s_high); //Display humidity

                write_com(0x80+0x40+8); //Move the cursor to the second position

                write_com(0x0f); //Display the cursor and blink        

        }        

        if(menu_1 == 4) //Set low humidity alarm

        {

                if(key_can == 2)

                {

                        s_low ++; //Set the low humidity value plus 1 

[1] [2]
Keywords:MCU Reference address:MCU + DHT11 temperature and humidity detection program

Previous article:Single chip LCD12864 unlimited expansion multi-level menu source code
Next article:51 MCU CS1237 electronic scale source program with detailed comments

Recommended ReadingLatest update time:2024-11-16 16:49

stm32 microcontroller detects 12V circuit
1. First, use the sampling circuit to divide the voltage to get a 3.3V voltage 2. Then use a voltage follower to convert the 3.3V high impedance signal voltage into a 3.3V low impedance drive voltage, which can be directly connected to the ADC pin of stm32 3. Note: The supply voltage of the op amp chip LM358 must be a
[Microcontroller]
stm32 microcontroller detects 12V circuit
How to optimize the memory of 51 microcontroller
Many people have misunderstandings about the memory of 51 microcontrollers. The most common ones are the following two ① Compile in compact mode if the variable exceeds 128 The actual situation is that as long as the memory footprint does not exceed 256.0, you can compile in small mode ② Some addresses above 128 are u
[Microcontroller]
Control design of 4×4 matrix keyboard by AT89S51 microcontroller
1. Experimental tasks As shown in Figure 4.14.2, use the parallel port P1 of AT89S51 to connect to a 4×4 matrix keyboard, use P1.0-P1.3 as the input line, and use P1.4-P1.7 as the output line; it is displayed on the digital tube The "0-F" serial number of each button. The sequence number arrangement of the correspondi
[Microcontroller]
Control design of 4×4 matrix keyboard by AT89S51 microcontroller
The difference between MCU in-circuit emulator and in-circuit programmer
For a long time, I was always confused about these two concepts. Now I will explain my understanding of the two as follows: Online emulator: One end is connected to the relevant communication port of the microcontroller, and the other end is connected to the communication port of the microcomputer. It is a connectin
[Microcontroller]
Single chip microcomputer design electronic piano music box (AT89C51)
1. Circuit Design This circuit consists of AT89C51 minimum system, key module, digital tube display and buzzer. Function: Seven buttons represent Do La Mi So Re La Si and the last button represents switching music. 2. Operation Results 3. Partial Code #include reg52.h #define uchar unsigned char #define uint un
[Microcontroller]
Single chip microcomputer design electronic piano music box (AT89C51)
51 MCU Study Notes——13.2DS1302 Real-time Clock Code Part (1)
Code: Experiment sequence: 11.1 Experimental task: Write the initial calibration time to the DS1302, then read the time from the DS1302 and display it Implementation phenomenon: After the program is running, the dynamic digital tube displays the initial time: 23-59-50, and continuously updates the display Hardware
[Microcontroller]
Design of electronic locker system based on 51 single chip microcomputer
Circuit Description: Security is the most important concern in our daily life. Everyone feels that security is very important, and the door and security at home can be as safe as possible. In order to ensure the safety of the door access Therefore, we intend to improve security by introducing an electronic combinati
[Microcontroller]
Design of electronic locker system based on 51 single chip microcomputer
PIC Microcontroller Quick Start
  PIC16F616 is a 14-pin, 8-bit CMOS microcontroller. It uses a reduced instruction set with only 35 instructions. Due to the use of the Harvard bus structure with separate data bus and instruction bus, most of the instructions are single-cycle instructions except for a few instructions that are not single-cycle. This
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号