Single chip automatic watering device

Publisher:琴弦悠扬Latest update time:2019-12-24 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The automatic watering system is designed with 51 single-chip microcomputer + LCD1602 liquid crystal + soil sensor + relay + ADC0832 + water pump.

The three system buttons are: system reset button, setup button, plus button, and minus button.


1. The soil dryness and humidity sensor transmits signals to the microcontroller in real time.

2. Press the button to set the soil dryness and humidity thresholds to achieve dynamic control of soil moisture.

3. The LCD displays soil moisture conditions in real time, and the set thresholds are intuitive and clear.

4. ADC0832 converts the analog signal into a digital signal and transmits it to the microcontroller.


Press the button to adjust the upper and lower limits, LCD1602 displays the soil moisture value in real time and displays the upper and lower limits. When the soil detection sensor detects that the humidity value is lower than the lower limit, the buzzer alarms and drives the water pump to water. When the humidity value is higher than the upper limit, watering stops.

The microcontroller source program is as follows:

#include  

#include "intrins.h"

#define uint unsigned int

#define uchar unsigned char

#define long unsigned long

#define LCDIO P0 //LCD screen data port

//ADC0832 pins

sbit ADCLK =P1^1;  //ADC0832 clock signal

sbit GOODBYE =P1^3; //ADC0832 k in

sbit ADCS =P1^4;  //ADC0832 chip seclect


sbit rs=P1^0; //define 1602 RS

sbit lcden = P1^2; //define 1602 EN

sbit key1=P3^0; //Set

sbit key2=P3^1; //add

sbit key3=P3^2; //subtraction

sbit motor=P3^7; //Relay interface

sbit speak=P1^5; //Buzzer interface

uchar key; //Set pointer

uint RH=400,RL=200; // water level upper and lower limits

float temp_f;

head temp;

fly v;

float count,s1num;

uchar code table[]= " moisture:          ";

uchar code table1[]="RH:  %              ";

uchar getdata; //Get the value converted by ADC

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

void delay(uint z) //delay

{

        uint x,y;

        for(x=z;x>0;x--)

                for(y=110;y>0;y--);

}

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

void write_com(uchar com)

{

        rs=0;

//        rd=0;

        lcden=0;

        P0=com;

        delay(5);

        lcden=1;

        delay(5);

        lcden=0;        

}

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

void write_date(uchar date)

{

        rs=1;

//        rd=0;

        lcden=0;

        P0=date;

        delay(5);

        lcden=1;

        delay(5);

        lcden=0;        

}


void lcdinit()

{

        lcden=0;

        write_com(0x38);

        write_com(0x0c);

        write_com(0x06);

        write_com(0x01);

}

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

void init()

{

        flying number;

        

        for(num=0;num<15;num++)

                {

                        write_date(table[num]);

                        delay(5);

                }

        write_com(0x80+0x40);

        for(num=0;num<15;num++)

                {

                        write_date(table1[num]);

                        delay(5);

                }

        }

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

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

Read ADC0832 function

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

//Collect and return

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

Function: AD conversion subroutine

Entry parameter: CH (if reading CH0, the channel value is 0x01, if reading CH1, the channel value is 0x03)

Export parameter: adval

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

uchar Adc0832() //AD conversion, return result

{

    flying i;

    fly dat=0;


    ADCLK=0;

    GOODBYE=1;

    ADCS=0; //Pull CS end low

    ADCLK=1;                 

    ADCLK=0; //Pull down the CLK end to form a falling edge 1


    ADDIO=1; //Specify whether the conversion channel is CH1 or CH2, specify the value bit and 0x1, and take the value of the last bit

    ADCLK=1;    

    ADCLK=0; //Pull down the CLK end to form a falling edge 2


    ADDIO=0; //Shift the specified value right by one digit, and then take the value of the last digit

    ADCLK=1;

    ADCLK=0; //Pull down the CLK end to form a falling edge 3



    GOODBYE=1;                

        for(i=0;i<8;i++)

    {

        ADCLK=1;

        ADCLK=0; //Form a clock pulse

        if(GOODBYE) 

                   dat|= 0x80>>i; //Receive data

    }



    ADCS=1; //Pull CS terminal low

    ADCLK=1;

    ADDIO=1; //Pull up the data terminal and return to the initial state

    return(that); //return that

}

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



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

void displayRH() //lower limit display

{write_com(0xc0+3);

write_date(RH/100%10+0x30); //upper limit hundreds digit

write_date(RH/10%10+0x30); //Upper limit 10 digits

//write_date('.');

//write_date(RH%10+0x30);

}

void displayRL() //lower limit display

{write_com(0xca);

  write_date('R');

  write_date('L');

  write_date(':');

write_date(RL/100%10+0x30);//lower limit hundreds

write_date(RL/10%10+0x30);//lower limit 10 digits

write_date('%');

}

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

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

void keyscan() //key processing

{bit kk1=0,kk2=0;

if(key1==0)

{delay(30);

  while(key1==0);

   if(key>=2)

   {key=0;

   }

   else

   {key++;

   }

   switch(key)

   {speak=1;kk2=motor;motor=1;

    case 1:{write_com(0x0f);write_com(0xce); // cursor flashes

        while(key1!=0) //Wait for the key to be released

        {

    if(key2==0) //key2 is pressed

   {delay(30); //Key delay to eliminate jitter

   if(key2==0) //Confirm key2 is pressed

   {

    while(key2==0); //Wait for release

        if(RL>=998)

        {RL=999; //The maximum lower limit of RL is set to 99

        }

        else

        {RL+=10;                //RL加1

        }        

   }

   displayRL(); //Call RL lower limit display function

   write_com(0xce);

  }   

   if(key3==0) //key3 is pressed

   {delay(30); //Key delay to eliminate jitter

   if(key3==0) //Confirm key3 is pressed

   {

    while(key3==0); //Wait for key3 to be released

        if(RL<=1) //RL is set to 1 at minimum

        {RL=0;

        }

        else

        {RL-=10; //RL lower limit minus 1

        }

   }

   displayRL(); //Call RL lower limit display function

   write_com(0xce);

  }


   

   }while(key1==0);        

        }

        case 2:{write_com(0x0f);write_com(0xc4); //RH setting data, cursor flashing

   while(key1==1)

   {

    if(key2==0) //key2 is pressed

   {delay(30); //Key delay to eliminate jitter

   if(key2==0) //Confirm key2 is pressed

   {

    while(key2==0); //Wait for release

        if(RH>=998) //RH is set to 99 at most

        {RH=999;

        }

        else

        {RH+=10; //RH加1

        }

        

   }

   displayRH(); //RH upper limit display function

    write_com(0xc4);

  }


   

   

   if(key3==0) //key3 is pressed

   {delay(30); //Key delay to eliminate jitter

   if(key3==0) //Confirm the press

   {

    while(key3==0);//Wait for release

        if(RH<=1) //RH minimum setting is 1

        {RH=0;

        }

        else

        {RH-=10; //RH minus 1

        }


   }

   displayRH(); //Call RH display function

    write_com(0xc4);

  }



   }

   while(key1==0);

   }

        case 0:{write_com(0x0c);

                        motor=kk2;


            break;}

   }

   }

}

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

void Conut(void) //Soil detection data conversion

{          

      v=Adc0832();

          temp=v;

      temp_f=temp*9.90/2.55;

      temp=temp_f;

          temp=1000-temp;         

          write_com(0x80+10);

          write_date(temp/100%10+0x30);//thousands

          write_date(temp/10%10+0x30);//hundreds digit

          write_date('.');

          write_date(temp%10+0x30);

          write_date('%');//display sign bit

        

         }

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

void main(void)

{

        lcdinit();

        heat();

        displayRH(); //Display upper limit

        displayRL(); //Display lower limit

        delay(50); //Start waiting, wait for LCD to enter working state

        delay(50); //delay for a moment (optional)

        delay(50); //delay

        delay(50);

        Conut(); //Display function

        delay(150);         

        while(1)

        {         

             Conut(); //Display current humidity

                 keyscan();

                 if(temp>RH) //If the humidity is greater than the upper limit, stop watering

                 {motor=1; //Turn off the relay

                 }

                 else if(temp                 {motor=0; //Start relay

                 }

                 if(temp                 {speak=0; //Start the alarm

                  delay(150); //delay

                  speak=1;

                 }

                 keyscan(); //key detection

                 delay(150); //delay 50MS

        }

}



Keywords:MCU Reference address:Single chip automatic watering device

Previous article:STC12C5A60S2 MCU AD sampling program and register explanation
Next article:51 MCU PID motor speed control Proteus simulation and source code

Recommended ReadingLatest update time:2024-11-15 15:15

Application of 2 external interrupts of 51 MCU
;There are 8 LEDs connected to the P1 port of the 51 microcontroller. ;A push button switch K1 is connected to the external interrupt 0 input pin P3.2. ;A push button switch K2 is connected to the external interrupt 1 input pin P3.3. ; When K1 and K2 are not pressed, the 8 LEDs at port P1 will display as running light
[Microcontroller]
Application of 2 external interrupts of 51 MCU
STM32 MCU learning (10) Digital tube output experiment supplement static (common anode) + dynamic (common cathode)
Note: When using the Puzhong Technology Development Board for testing, you need to unplug the Boot1 socket Reference manual, circuit diagram, etc. See STM32 MCU Learning (1) Total Learning Materials + Reference Manual + LED Light  Static digital tube experiment   /* * * Software function: static digital tube ex
[Microcontroller]
Example of sending characters via 51 MCU serial port
#include reg52.h #define jingzhen 11059200UL /*Use 22.1184M crystal*/      #define botelv 9600UL /*Baud rate is defined as 9600*/ unsigned char zifuchuan ="Hello! n"; //Characters to be displayed. volatile unsigned char sending; sbit S4=P3^2; void delay(unsigned char i) {     unsigned char j,k;     for(j=i;j 0;j--)
[Microcontroller]
The difference between STC microcontroller user application area and ISP monitoring program area
User Application Area (AP Area): Refers to the program area written by the user ISP monitoring program area: ISP area refers to a program that has been solidified inside the MCU when the chip leaves the factory. STC MCU can perform ISP serial download program, because the ISP boot code has been solidified inside t
[Microcontroller]
Lock-free atomic read operation on MCU
Atomic read operations are commonly used in MCU concurrent programming. Let's take a simple example to illustrate the problem: When we use RTOS or bare metal state programming, we must have a global clock reference, which is usually implemented by accumulation in a timer interrupt. The simplified code is as follows:
[Microcontroller]
51 MCU frequency calculation
When the timer reload value is 8 bits (traditional 51 MCU models, such as T1 of AT89C51): Y=256-((256-X)*4) When the timer reload value is 16 bits (newer 51 MCU models, such as Timer3 of C8051F series): Y=65536-((65536-X)*4) The following example illustrates the baud rate selection process: Assume that the communi
[Microcontroller]
About PIC microcontroller SLEEP instruction
I never really understood the sleep instruction before. Recently, when I was making a smart water meter product, I used an 8-bit microcontroller PIC16F690. When I saw the sleep instruction in the program, I thought the CPU would continue to work after execution, which led to some misunderstandings. I searched the Inte
[Microcontroller]
51 MCU Design Schemes TOP10 (Part 3)
0 Introduction   All the single-chip microcomputers currently compatible with Intel 8031 ​​instruction system are collectively referred to as 51 single-chip microcomputers. Intel's 8031 ​​single-chip microcomputer is the ancestor of 51 single-chip microcomputers. 8031 ​​single-chip microcomputer is one of the most wid
[Analog Electronics]
51 MCU Design Schemes TOP10 (Part 3)
Latest Microcontroller Articles
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号